gpt4 book ai didi

javascript - 如何解决过滤数据表时出现错误 "oTable.draw is not a function"?

转载 作者:行者123 更新时间:2023-11-28 06:03:11 25 4
gpt4 key购买 nike

我在数据表上方有一个搜索表单。当我尝试搜索时,我得到的数据以 JSON 格式显示,而不是过滤数据表。错误是

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

这是 Controller :

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);

}

这是 JSON:
enter image description here

最佳答案

在使用draw之前,需要调用一个操作,例如searchorder等。并且draw只是重绘表格,并不从服务器端更新数据。检查代码后,您的目的是从服务器端重新获取数据。因此,您需要使用oTable.ajax.reload();从服务器重新获取数据。

了解更多信息:

Ajax 重新加载: https://datatables.net/reference/api/ajax.reload()

绘制: https://datatables.net/reference/api/draw()

关于javascript - 如何解决过滤数据表时出现错误 "oTable.draw is not a function"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37107038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com