最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Adding Buttons to Datatables - Jquery - Stack Overflow

programmeradmin3浏览0评论

How can i add buttons to my datatables on each row? With my code, it looks like there is something i am not doing right.

How can i achieve this?

HTML

<table id="users-table" class="table table-hover table-condensed" style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Grade</th>
            <th>Action</th>
        </tr>
    </thead>
  </table>

JS

$(document).ready(function() {

    oTable = $('#users-table').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": "{{ route('datatable.getpost') }}",
        "columns": [
            {data: 'name', name: 'name'},
            {data: 'description', name: 'description'},
            {data: 'no_of_items', name: 'no_of_items'},

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

How can i add buttons to my datatables on each row? With my code, it looks like there is something i am not doing right.

How can i achieve this?

HTML

<table id="users-table" class="table table-hover table-condensed" style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Grade</th>
            <th>Action</th>
        </tr>
    </thead>
  </table>

JS

$(document).ready(function() {

    oTable = $('#users-table').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": "{{ route('datatable.getpost') }}",
        "columns": [
            {data: 'name', name: 'name'},
            {data: 'description', name: 'description'},
            {data: 'no_of_items', name: 'no_of_items'},

        ],
        "aoColumns": [{
         {
      "mRender": function(data, type, full) {
        return '<a class="btn btn-info btn-sm" href=#>' + 'Edit' + '</a>';
      }
         }
    }]
    });
});
Share Improve this question edited Dec 14, 2017 at 14:06 agrm 3,8624 gold badges27 silver badges36 bronze badges asked Dec 14, 2017 at 13:59 LearnLaravelLearnLaravel 4033 gold badges9 silver badges24 bronze badges 3
  • check this out datatables/blog/2011-06-19. Why are you using mRender? – Vijay Rathore Commented Dec 14, 2017 at 14:09
  • @VijayRathore, i can't find any button on the reference you gave. I am actually new to datatables – LearnLaravel Commented Dec 14, 2017 at 14:12
  • You do not have to write anything in Datatable initialization if your ajax source returns HTML for button also. Check ajax sourced Datatables here datatables/examples/data_sources/ajax.html – Vijay Rathore Commented Dec 14, 2017 at 14:17
Add a ment  | 

1 Answer 1

Reset to default 3
$(document).ready(function() {
$('#example').DataTable( {
    dom: 'Bfrtip',
    buttons: [
        'copyHtml5',
        'excelHtml5',
        'csvHtml5',
        'pdfHtml5'
    ]
} );

} );

Basically this is the code, which you are looking for.

Output will be like -

for more details check this link

You need to include these js files as well.

https://cdn.datatables/buttons/1.5.0/js/dataTables.buttons.min.js
https://cdnjs.cloudflare./ajax/libs/jszip/3.1.3/jszip.min.js
https://cdnjs.cloudflare./ajax/libs/pdfmake/0.1.32/pdfmake.min.js
https://cdnjs.cloudflare./ajax/libs/pdfmake/0.1.32/vfs_fonts.js
https://cdn.datatables/buttons/1.5.0/js/buttons.html5.min.js

I hope this helps.

EDIT 1

For Row Editing you can check this js fiddle

Edit 2

DataTable also provide InTable feature check this link

$('#example').DataTable( {
    ajax: "../php/staff.php",
    columns: [
        { data: null, render: function ( data, type, row ) {
            // Combine the first and last names into a single table field
            return data.first_name+' '+data.last_name;
        } },
        { data: "position" },
        { data: "office" },
        { data: "extn" },
        { data: "start_date" },
        { data: "salary", render: $.fn.dataTable.render.number( ',', '.', 0, '$' ) },
        {
            data: null,
            className: "center",
            defaultContent: '<a href="" class="editor_edit">Edit</a> / <a href="" class="editor_remove">Delete</a>'
        }
    ]
} );

Edit & Delete Code will be like :

// Edit record
$('#example').on('click', 'a.editor_edit', function (e) {
    e.preventDefault();

    editor.edit( $(this).closest('tr'), {
        title: 'Edit record',
        buttons: 'Update'
    } );
} );

// Delete a record
$('#example').on('click', 'a.editor_remove', function (e) {
    e.preventDefault();

    editor.remove( $(this).closest('tr'), {
        title: 'Delete record',
        message: 'Are you sure you wish to remove this record?',
        buttons: 'Delete'
    } );
} );
发布评论

评论列表(0)

  1. 暂无评论