jQuery DataTables: hide 'delete' button for the last row

后端 未结 1 1762
醉酒成梦
醉酒成梦 2021-01-27 17:55

I have a DataTable which can return multiple pages in some cases. Each row returned, displays a delete button but what i need it to hide this button on the very las

1条回答
  •  误落风尘
    2021-01-27 18:30

    May I recommend not to hide 'delete' button, when you got the last entry in your table (which will look awkward, from user standpoint), but rather to disable that?

    Here's the example of my point (I'm sure, you'll grasp the idea):

    //table data
    const srcData = [
      {name: 'apple', category: 'fruit'},
      {name: 'banana', category: 'fruit'},
      {name: 'carrot', category: 'vegie'},
      {name: 'pineapple', category: 'fruit'},
      {name: 'kiwi', category: 'fruit'},
    ];
    //table initialization
    const dataTable = $('#mytable').DataTable({
      sDom: 'tp',
      data: srcData,
      ordering: false,
      pageLength:3,
      drawCallback: () => {
        const table = $('#mytable').DataTable();
        $(table.row(table.rows(':last')).node()).find('button').remove();
      },
      columns: [
        {title: 'Name', data: 'name'},
        {
          title: 'Category', 
          data: 'category', 
          render: data => data+''},
      ]
    });
    //'delete' button callback
    $('#mytable').on('click', 'td button', function(){
      dataTable.row($(this).closest('tr')).remove().draw();
    });
    tbody td button {float:right}
    
    
    
      
      
      
    
    
      

    0 讨论(0)
提交回复
热议问题