2017-12-14 12 views
1

各行のデータテーブルにボタンを追加するにはどうすればよいですか?私のコードでは、私が正しいことをしていないものがあるように見えます。データ型にボタンを追加する - 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>'; 
     } 
     } 
    }] 
    }); 
}); 
+0

これをチェックしてくださいhttps://datatables.net/blog/2011-06-19。なぜあなたはmRenderを使用していますか? –

+0

@VijayRathore、私はあなたが与えた参照上のボタンを見つけることができません。私は実際にはデータセットの新しさです – LearnLaravel

+0

あなたのajaxソースがボタンのHTMLも返す場合は、Datatableの初期化で何も書く必要はありません。アヤックスソースのDatatablesをここで確認してください。https://www.datatables.net/examples/data_sources/ajax.html –

答えて

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

})。

これは基本的にあなたが探しているコードです。あなたは同様にこれらのjsファイルをインクルードする必要があり

enter image description here

for more details check this link

-

出力は次のようになります。

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 

これが役立ちます。行編集のため

EDIT 1

あなたはコードは次のようになります削除このjs fiddle

編集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 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

いいえ、それは私が探しているものではありません...ボタンは各行にあるべきです。 – LearnLaravel

+0

ああk待って私もあなたの詳細を教えてください –

+0

@ LearnLaravel –

関連する問題