Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add button on each row in datatable?

I am newbie for DataTables. I want to add button on each row for edit and delete(like below image)

enter image description here

I have tried with code:

Test.cfm

<table id="myDataTable" class="table table-striped table-bordered"> <thead>     <tr>         <th>UserID</th>         <th>Name</th>         <th>UserName</th>         <th>Passowrd</th>         <th>Email</th>          <th>IsActive</th>          <th>Command</th>     </tr> </thead> <tbody>  </tbody> 

$(document).ready(function () {     var oTable = $('#myDataTable').dataTable({        // "bServerSide": true,         "sAjaxSource": "fetchUserData.cfm",         "bProcessing": true,         "sPaginationType": "full_numbers",         "aoColumns": [             { "mData": null },             { "mData": "Name", "sTitle": "Name" , "sWidth": "20%"},             { "mData": "UserName", "sTitle": "UserName", "sWidth": "20%" },             { "mData": "Passowrd","sTitle": "Passowrd", "sWidth": "20%"  },             { "mData": "Email","sTitle": "Email"  , "sWidth": "20%"},             { "mData": "IsActive","sTitle": "IsActive" , "sWidth": "20%" },             {                 "mData": null,                 "bSortable": false,                "mRender": function (o) { return '<a href=#/' + o.userid + '>' + 'Edit' + '</a>'; }             }         ]     });  } ); 

fetchUserData.cfm

{ "aaData": [     [         "1",         "sameek",         "sam",         "sam",         "[email protected]",         "1",         ""     ],     [         "2",         "arun singh",         "arun",         "arun",         "[email protected]",         "0",         ""     ],     [         "9",         "s s",         "sam3",         "sam3",         "[email protected]",         "0",         ""     ],     [         "10",         "sameek mishra",         "sam56",         "sam",         "[email protected]",         "0",         ""     ],     [         "11",         "narendra kumar",         "narendra",         "nav",         "[email protected]",         "1",         ""     ],     [         "12",         "test test",         "test",         "test",         "[email protected]",         "1",         ""     ] ]  } 
like image 214
Sameek Mishra Avatar asked Mar 18 '14 06:03

Sameek Mishra


People also ask

How add Colspan to DataTable?

DataTables fully supports colspan and rowspan in the table's header, assigning the required order listeners to the TH element suitable for that column. Each column must have one TH cell which is unique to it for the listeners to be added.

How do I add data to a row in a table?

New rows can be added to a DataTable using the row. add() API method. Simply call the API function with the data for the new row (be it an array or object). Multiple rows can be added using the rows.

How add edit and delete button in DataTable using HTML?

You can use DataTable createdRow attribute to add edit /delete buttons in an empty action column (include the empty column in columnDef). This way you can add row UID in buttons, which will make it easier to trigger click actions.


2 Answers

Basically your code is okay, thats the right way to do this. Anyhow, there are some misunderstandings:

  1. fetchUserData.cfm does not contain key/value pairs. So it doesn't make sense to address keys in mData. Just use mData[index]

  2. dataTables expects some more info from your serverside. At least you should tell datatables how many items in total are on your serverside and how many are filtered. I just hardcoded this info to your data. You should get the right values from counts in your server sided script.

    {  "iTotalRecords":"6",  "iTotalDisplayRecords":"6",   "aaData": [ [     "1",     "sameek",     "sam",     "sam",     "[email protected]",     "1",     "" ],... 
  3. If you have the column names already set in the html part, you don't need to add sTitle.

  4. The mRender Function takes three parameters:

    • data = The data for this cell, as defined in mData
    • type = The datatype (can be ignored mostly)
    • full = The full data array for this row.

So your mRender function should look like this:

  "mRender": function(data, type, full) {     return '<a class="btn btn-info btn-sm" href=#/' + full[0] + '>' + 'Edit' + '</a>';   } 

Find a working Plunker here

like image 110
mainguy Avatar answered Sep 22 '22 13:09

mainguy


var table =$('#example').DataTable( {     data: yourdata ,     columns: [         { data: "id" },         { data: "name" },         { data: "parent" },         { data: "date" },          {data: "id" , render : function ( data, type, row, meta ) {               return type === 'display'  ?                 '<a href="<?php echo $delete_url;?>'+ data +'" ><i class="fe fe-delete"></i></a>' :                 data;             }},     ],     } } 
like image 44
Majid Ramzani Avatar answered Sep 20 '22 13:09

Majid Ramzani