8
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

htmlで入力したり行を追加したり削除したりできるtableを作る

Posted at

htmlで入力したり行を追加したり削除したりできるtableを作る

htmlでtableに入力したいってことがよくあるので作った
ここにソース置いてます

#demo
table.gif

requirement

  • 行を削除したりできる
  • 行を追加できる
  • bootstrapとかに依存しない
  • コピペで汎用的に使える
  • 入力ができる。

#ソース全体

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.0/css/all.css"
          integrity="sha384-aOkxzJ5uQz7WBObEZcHvV5JvRW3TUc2rNPA7pe3AwnsUohiw1Vj2Rgx2KSOkF5+h" crossorigin="anonymous">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
          integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
            integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
            crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"
            integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
            crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"
            integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
            crossorigin="anonymous"></script>

    <script>
        function get_data_from_table() {
            let data = []
            Array.prototype.forEach.call(document.querySelectorAll("table#target>tbody>tr"), (e) => {
                if (e.classList.contains("hide"))
                    return;
                if (e.classList.contains("add_row"))
                    return;

                let row = []
                Array.prototype.forEach.call(e.querySelectorAll("td>input"), (txt) => {
                    row.push(txt.value)
                })
                data.push(row)
            })
            return data
        }
    </script>
    <style>
        .hide {
            display: none;
        }
    </style>
</head>
<body>
<table class="table table-striped" id="target">
    <thead>
    <tr>
        <th scope="row">key</th>
        <th>value</th>
        <th>delete</th>
    </tr>
    </thead>
    <tbody id="params">

    <tr class="add_row"
        onclick="let node=this.nextElementSibling.cloneNode(true);node.removeAttribute('id');node.classList.remove('hide');this.parentNode.insertBefore(node,this)">
        <td colspan="3" style="text-align: center"><i class="fas fa-plus"></i></td>
    </tr>

    <!--template-->
    <tr class="hide">
        <td><input style="width: 100%;outline: 0;border: 0px;background: transparent;"></td>
        <td><input style="width: 100%;outline: 0;border: 0px;background: transparent;"></td>
        <td onclick="this.parentNode.outerHTML = ''"><i class="fas fa-ban"></i></td>
    </tr>
    </tbody>
</table>
</body>
</html>

#最低限必要なもの
get_data_from_table()を呼び出すとテーブルからデータを読み込んで配列で返します

function get_data_from_table() {
    let data = []
    Array.prototype.forEach.call(document.querySelectorAll("table#target>tbody>tr"), (e) => {
        if (e.classList.contains("hide"))
            return;
        if (e.classList.contains("add_row"))
            return;

        let row = []
        Array.prototype.forEach.call(e.querySelectorAll("td>input"), (txt) => {
            row.push(txt.value)
        })
        data.push(row)
    })
    return data
}
    .hide {
       display: none;
    }
<table id="target">
    <thead>
    <tr>
        <th scope="row">key</th>
        <th>value</th>
        <th>delete</th>
    </tr>
    </thead>
    <tbody id="params">

    <tr class="add_row"
        onclick="let node=this.nextElementSibling.cloneNode(true);node.removeAttribute('id');node.classList.remove('hide');this.parentNode.insertBefore(node,this)">
        <td colspan="3" style="text-align: center"><i class="fas fa-plus"></i></td>
    </tr>

    <!--template-->
    <tr class="hide">
        <td><input style="width: 100%;outline: 0;border: 0px;background: transparent;"></td>
        <td><input style="width: 100%;outline: 0;border: 0px;background: transparent;"></td>
        <td onclick="this.parentNode.outerHTML = ''"><i class="fas fa-ban"></i></td>
    </tr>
    </tbody>
</table>
8
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?