【问题标题】:How to add edit and delete buttons in datatable如何在数据表中添加编辑和删除按钮
【发布时间】:2020-02-26 09:14:31
【问题描述】:

我对数据表相当陌生,我正在尝试在每一行的数据表中添加编辑和删除按钮。单击后,编辑和删除按钮应执行我创建的一些功能。我已经尝试阅读文档和其他 StackOverflow 问题,但我似乎没有得到它们。这是我的代码:

 <table class="table table-striped table-bordered" style="width:fit-content; " id="mydatatable">
   <thead>
     <tr>
       <th data-field="Document_id" scope="col">Document id</th>
       <th scope="col">title</th>
       <th scope="col">details</th>
       <th scope="col">timestamp</th>
       <th scope="col">name</th>
     </tr>
   </thead>
   <tfoot>
     <tr>
       <th data-field="Document_id" scope="col">Document id</th>
       <th scope="col">title</th>
       <th scope="col">details</th>
       <th scope="col">timestamp</th>
       <th scope="col">name</th>
     </tr>
   </tfoot>
 </table>

db.collection("Emergency_Feeds").orderBy("timestamp", "desc").onSnapshot(function(querySnapshot) {

  querySnapshot.docChanges().forEach(function(change) {

    console.log('documents retrieved successfully');
    console.log(`is here: ${change.doc.id} => ${change.doc.data().name}`);

    var documentId = change.doc.id;
    var username = change.doc.data().name;
    var emTitle = change.doc.data().title;
    var emDets = change.doc.data().details;
    var emTimeDate = change.doc.data().timestamp.toDate();

    if (change.type === "added") {
      tableData.push(
        [
          documentId,
          emTitle,
          emDets,
          emTimeDate,
          username
        ]);
    }

    if (change.type === "modified") {
      //..... 
      //Here update the table element
      // Note that the DocumentChange contains the old and new index
      tableData.push(
        [
          documentId,
          emTitle,
          emDets,
          emTimeDate,
          username
        ]);
    }

    if (change.type === "removed") {
      //..... 
      //Here remove the table element
      tableData.pop(
        [
          documentId,
          emTitle,
          emDets,
          emTimeDate,
          username
        ]);
    }
  });

  $('#mydatatable').DataTable({
    retrieve: true,
    data: tableData,
    pagingType: 'full_numbers',
    lengthMenu: [
      [5, 10, 25, 50, -1],
      [5, 10, 25, 50, "All"]
    ],


  });
  $('#mydatatable tfoot th').each(function() {

    var title = $(this).text();
    $(this).html('<input type="text" placeholder="Search ' + title + '" />')
  });
  var datTable = $('#mydatatable').DataTable();
  datTable.columns().every(function() {

    var that = this;
    $('input', this.footer()).on('keyup change', function() {

      if (that.search() !== this.value) {

        that.search(this.value).draw();
      }
    });
  });
});  

【问题讨论】:

  • 您能否提供一个链接,说明您所说的“数据表”是什么意思?一个 API 可能很容易点击(根据您的问题)而不进行自己的研究?如果帮助您进行研究,即使“数据表”很容易搜索,这种 API/接口/框架的版本也有很大帮助。研究通常会得出您可能没有使用的最新版本。

标签: javascript jquery datatable datatables


【解决方案1】:
$('#mydatatable').DataTable({
retrieve: true,
data: tableData,
pagingType: 'full_numbers',
lengthMenu: [
  [5, 10, 25, 50, -1],
  [5, 10, 25, 50, "All"]
],
 columns: [
        {
            data: "ID",
            render:function (data, type, row) {
             return `<button class='add' >add</button>`;        
        },
        {
            data: "ID",
            render:function (data, type, row) {
                    return `<button class='edit' >edit</button>`;
        },
        {
            data: "ID",
            render:function (data, type, row) {
                    return `<button class='delete' >delete</button>`;
        }
        ,
            //..... your remaining columns need to mention here...

  });



 $('#mydatatable .add').on('click',function(){ 
//.. your logic for add button click 
})

    $('#mydatatable .edit').on('click',function(){ 
//.. your logic for edit button click 
})

    $('#mydatatable .delete').on('click',function(){ 
//.. your logic for delete button click 
})

【讨论】:

  • data:"ID" 应该保持原样吗?
  • ID 是特定行中的唯一值,请使用 json 数据中的该节点
  • 我没有使用 json ,我使用的是数组,而且列已经在 html 脚本中指定了
  • 是否也意味着我必须在 html ids 中给出列?
  • 你能分享你的'tableData'吗?\
猜你喜欢
  • 1970-01-01
  • 2021-02-08
  • 2018-04-26
  • 2018-10-14
  • 1970-01-01
  • 2015-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多