【问题标题】:enable edit/delete in Datatables在数据表中启用编辑/删除
【发布时间】:2020-11-23 06:20:59
【问题描述】:

我有一个以 Cloud Firestore 作为后端的 Web 应用程序。我使用DataTable从cloud firestore导出数据并显示在网页上,表格如下: Table

从 Cloud Firestore 加载“订单”集合并附加到 DataTables 的代码是:


var dataTable;

db.collection("orders").orderBy('timestamp', 'desc')
    .get()
    .then(function (querySnapshot) {
        if (querySnapshot.size) {
            var firestore_source = [];
            querySnapshot.forEach(function (data) {
                var obj = data.data();
                obj.id = data.id;
                firestore_source.push(obj);
            });
            //console.log('data:', firestore_source);
            dataTable.clear();
            dataTable.rows.add(firestore_source);
            dataTable.order([0, 'desc']).draw();
        }
    })
    .catch(function (error) {
        console.log("Error getting documents: ", error);
    });


$(document).ready(function () {
    dataTable = $('#example').DataTable({
        columns: [
            { data: 'Name' },
            { data: "Date" },
            { data: "Ins" },
            { data: "Phone" },
            { data: "Item" },
            { data: "Price"},
            { data: "Commision"},
            { data: "Revenue"},
            {
                data: null,
                className: "center",
                defaultContent: '<a href="" class="editor_edit">Edit</a> / <a href="" class="editor_remove">Delete</a>'
            }
        ],
        "columnDefs": [
            {"className": "dt-center", "targets": "_all"}
          ],
    });

    
    $('#example').on('click', 'a.editor_remove', function (e) {
        e.preventDefault();
        console.log("delete clicked");
        console.log($(this).closest('tr'));
         
         // what I should do here?

    } );
});

HTML 中的数据表:

        <table id="example" class="table table-striped table-bordered" style="width:100%">
            <thead>
                <tr>
                    <th>Customer</th>
                    <th>Order Date</th>
                    <th>Instagram</th>
                    <th>Phone</th>
                    <th>Item</th>
                    <th>Price $</th>
                    <th>Commission</th>
                    <th>Earnings $</th>
                    <th>Edit / Delete</th> 
                </tr>
            </thead>

        </table>

目前,“订单”集合中的整个数据都已加载,显然没有编辑和删除每行数据等功能。 所以,我被困在这里,我不知道单击该行上的编辑/删除按钮时如何识别表中的每一行,以便我可以将其用作查询云 Firestore 的参数?

我看到有内置工具Editor,但我正在寻找本地方法。

【问题讨论】:

  • 您可以查看this post以获取一些线索
  • 谢谢。我想这就是我所追求的
  • 不客气。如果这有帮助,请随时点赞该帖子。

标签: javascript jquery google-cloud-firestore datatables


【解决方案1】:

关于datatable API,您可以通过此代码获取点击/选定行的数据,这意味着您可以获得身份来编辑或删除选定的行。

$('#example tbody').on('click', 'a.editor_remove', function () {
    let row = dataTable.api().row($(this).closest("tr")).data();
    console.log(row);
});

【讨论】:

  • 在我的情况下,使用 dataTable.api() 会导致 Uncaught 错误,所以只需使用 let row = dataTable.row($(this).closest("tr")).data(); 即可
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-27
  • 1970-01-01
  • 2018-07-29
  • 1970-01-01
  • 2010-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多