我正在尝试使用Yajra数据表将数据表集成到我的网站中。我能够渲染表格,但是遇到了问题。在我放置删除选项的addColumn字段中,ID仅返回单个记录,而不返回每列的特定ID。
这是我的控制器代码
public function fetchData($manifest_no){
$hwb_data = DB::table('hwb')
->join('manifest_records', 'hwb.id', '=', 'manifest_records.hwb_id')
->join('consignees', 'consignees.id', '=', 'hwb.client_id')
->where('manifest_records.manifest_no', $manifest_no)
->get();
return Datatables::of($hwb_data)
->addIndexColumn()
->addColumn('action', function($row){
$idx = $row->id;
$btn = "<a href='javascript:void(0)' id='delete' data-id='$idx' class='edit btn btn-danger btn-sm'>$idx</a>";
return $btn;
})
->rawColumns(['action'])
->make(true);
}
这是Datatable渲染
$(function () {
let table = $('#tableSample').DataTable({
processing: true,
serverSide: true,
ajax: "{{ url("/fetchData/$manifest_no") }}",
columns: [
{data: 'id', name: 'id', 'visible': false},
{data: 'created_at', name: 'created_at'},
{data: 'hwb_no', name: 'hwb_no'},
{data: 's_name', name: 's_name'},
{data: 'c_name', name: 'c_name'},
{data: 'destination', name: 'destination'},
{data: 'dr_no', name: 'dr_no'},
{data: 'commodity', name: 'commodity'},
{data: 'actual_weight', name: 'actual_weight'},
{data: 'tp_cbm', name: 'tp_cbm'},
{data: 'action', name: 'action', orderable: false, searchable: false},
]
});
$("#tableReload").delegate('#delete', 'click', function (e) {
e.preventDefault();
let id = $(this).data('id');
console.log(id);
})
});
I每当我渲染数据时,我总是遇到这个问题。 “操作”按钮的值始终为2
答案 0 :(得分:0)
我想您必须像这样使用Render函数,而不是后端函数
还尝试在呈现视图之前转储$row
和dd,只是检查$ row是否具有您期望的正确值!
$(function () {
let table = $('#tableSample').DataTable({
processing: true,
serverSide: true,
ajax: "{{ url("/fetchData/$manifest_no") }}",
columns: [
{data: 'id', name: 'id', 'visible': false},
{data: 'created_at', name: 'created_at'},
{data: 'hwb_no', name: 'hwb_no'},
{data: 's_name', name: 's_name'},
{data: 'c_name', name: 'c_name'},
{data: 'destination', name: 'destination'},
{data: 'dr_no', name: 'dr_no'},
{data: 'commodity', name: 'commodity'},
{data: 'actual_weight', name: 'actual_weight'},
{data: 'tp_cbm', name: 'tp_cbm'},
{ data: 'id',
name: 'action',
orderable: false,
searchable: false,
"render": function ( data, type, row, meta ) {
return '<button class="delete btn btn-danger" data-id="'+ data +'"> Delete </button>'
}},
]
});
});