簡體   English   中英

向數據表添加按鈕-Jquery

[英]Adding Buttons to Datatables - Jquery

如何在每行的數據表中添加按鈕? 用我的代碼,看來有些事情我做得不對。

我怎樣才能做到這一點?

的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>';
      }
         }
    }]
    });
});
$(document).ready(function() {
$('#example').DataTable( {
    dom: 'Bfrtip',
    buttons: [
        'copyHtml5',
        'excelHtml5',
        'csvHtml5',
        'pdfHtml5'
    ]
} );

});

基本上,這就是您要查找的代碼。

輸出將像-

在此處輸入圖片說明

有關更多詳細信息,請檢查此鏈接

您還需要包括這些js文件。

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

我希望這有幫助。

編輯1

對於行編輯,您可以檢查此js小提琴

編輯2

DataTable還提供InTable功能,請檢查此鏈接

$('#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 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'
    } );
} );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM