我在數據表上方有一個搜索表單。當我嘗試搜索時,我以JSON格式顯示數據,而不是過濾數據表。該錯誤是如何解決過濾數據表中的錯誤「oTable.draw不是函數」?
Uncaught TypeError: oTable.draw is not a function
這裏是JavaScript:
$(document).ready(function(){
var oTable = $('#surat').DataTable({
processing: true,
serverSide: true,
ajax: {
url: '{!! route("surat") !!}',
data: function (d) {
d.jenis_surat = $('input[name=jenis_surat]').val();
d.nomor_surat = $('input[name=nomor_surat]').val();
d.perihal = $('input[name=perihal]').val();
}
},
columns: [
{data: 'no', name: 'no'},
{data: 'jenis_surat', name: 'jenis_surat'},
{data: 'nomor_surat', name: 'nomor_surat'},
{data: 'perihal', name: 'perihal'},
{data: 'status', name: 'status'}
]
});
$('#search_form').on('submit', function(e) {
oTable.draw();
e.preventDefault();
});
});
這是控制器:
public function carisuratdata(Request $request)
{
$surats = Surat::select(['id_surat', 'id_jenis_surat', 'nomor_surat','perihal', 'tanggal_surat', 'masa_berlaku_from', 'masa_berlaku_to', 'ringkasan', 'tag', 'id_publish', 'tanggal_publish'])->get();
return Datatables::of($surats)
->filter(function ($instance) use ($request) {
if ($request->has('jenis_surat')) {
$instance->collection = $instance->collection->filter(function ($row) use ($request) {
return Str::contains($row['jenis_surat'], $request->get('jenis_surat')) ? true : false;
});
}
if ($request->has('nomor_surat')) {
$instance->collection = $instance->collection->filter(function ($row) use ($request) {
return Str::contains($row['nomor_surat'], $request->get('nomor_surat')) ? true : false;
});
}
if ($request->has('perihal')) {
$instance->collection = $instance->collection->filter(function ($row) use ($request) {
return Str::contains($row['perihal'], $request->get('perihal')) ? true : false;
});
}
})
->make(true);
}
嘗試'oTable.ajax.reload()' –