shuhelohelo’s blog

Xamarin.Forms多めです.

jQueryでテーブル生成

codeforfun.jp

とてもわかりやすい。

メモ

jQuery UIというものがあることを初めて知った。

tbody要素に対してsortable()とすると行をドラッグ・アンド・ドロップで入れ替えられるようになる。

たった、これだけで。

テーブルの生成

それはそれとして、ボタンが押されるたびにtrをtbodyにappendしていく。

tr要素の生成は、htmlを文字列として変数に入れてやるだけでよい。非常にシンプル。

        var html = '<tr><td><input type="text" name="name"></td><td><button class="remove">-</button></td></tr>';
        $('tbody').append(html);

jQueryによる要素の生成は以下の記事がわかりやすかった。

qiita.com

以下のように要素単位で分割して組み立ててみた。

                let tr = $('<tr></tr>');
                let td1 = $('<td></td>');
                let input = $('<input type="text" name="name">');
                td1.append(input);
                let td2 = $('<td></td>');
                let button = $('<button class="remove">-</button>');
                td2.append(button);
                
                tr.append(td1);
                tr.append(td2);
                $('tbody').append(tr);

属性なども個別に指定できるとのことなのでやってみる。

                let tr = $('<tr></tr>');
                
                let td1 = $('<td></td>');
                let input = $('<input>',{
                    type:"text",
                    name:"name"
                });
                td1.append(input);

                let td2 = $('<td></td>');
                let button = $('<button></button>',{
                    "class":"remove",
                    "text":"-"
                });
                td2.append(button);

                tr.append(td1);
                tr.append(td2);
                $('tbody').append(tr);

動的に生成された要素の削除

動的に生成された要素にはイベントハンドラがセットされていないので、生成時にセットする必要がある。

ページ読み込み後に生成された要素にイベントハンドラを追加する場合はdocumentを使用する。

            $(document).on('click', '.remove', function () {
                $(this).parents('tr').remove();
            });

これが現在のスマートな方法かどうかをおいておいて、やり方はわかったぞ。

<!DOCTYPE html>
<html lang="ja">

<head>
    <meta charset="utf-8">
    <title>テーブル追加</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <style>
        .container {
            width: 500px;
            margin: 100px auto;
        }

        input {
            width: 300px;
            font-size: 18px;
            margin: 10px;
            padding: 10px;
        }

        .remove {
            width: 30px;
            height: 30px;
            font-size: 20px;
            background-color: tomato;
            color: white;
            border: none;
            border-radius: 15px;
        }

        #addRow,
        #getValues {
            width: 130px;
            height: 40px;
            font-size: 16px;
            background-color: lightseagreen;
            color: white;
            border: none;
            margin: 20px;
        }

        button:hover {
            cursor: pointer;
        }

        tr:hover {
            cursor: move;
        }
    </style>
</head>

<body>
    <div class="container">
        <table>
            <tbody>
                <tr>
                    <td>
                        <input type="text" name="name">
                    </td>
                    <td>
                        <button class="remove">-</button>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="text" name="name">
                    </td>
                    <td>
                        <button class="remove">-</button>
                    </td>
                </tr>
            </tbody>
        </table>
        <button id="addRow">+ 追加</button>
        <button id="getValues">値を取得</button>
    </div>
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

    <script>
        $(function () {
            $('tbody').sortable();
        });

        $(function () {
            $('#addRow').click(function () {
                let tr = $('<tr></tr>');

                let td1 = $('<td></td>');
                let input = $('<input>', {
                    type: "text",
                    name: "name"
                });
                td1.append(input);

                let td2 = $('<td></td>');
                let button = $('<button></button>', {
                    "class": "remove",
                    "text": "-"
                });
                td2.append(button);

                tr.append(td1);
                tr.append(td2);
                $('tbody').append(tr);
            });

            $(document).on('click', '.remove', function () {
                $(this).parents('tr').remove();
            });

            $('#getValues').click(function () {
                var values = [];
                $('input[name="name"]').each(function (i, elem) {
                    values.push($(elem).val());
                });
                alert(values.join(','));
            });
        });
    </script>
</body>

</html>