gpt4 book ai didi

javascript - 拉维尔 : Data-table search option not working using relationship table field

转载 作者:行者123 更新时间:2023-11-29 20:31:38 25 4
gpt4 key购买 nike

我在连接表字段中搜索记录时遇到问题。我需要搜索所有列,包括连接表列。

这是我的 All State with Country Controller 函数:

public function allStates()
{
$states = State::select(['id', 'country_id', 'state_type', 'state_code', 'state_name', 'status'])->orderBy('country_id','Asc');
return Datatables::of($states)
->addColumn('checkes', function ($states) {

$data = $states;
return view('partials.datatable.table_first_column_checkbox', compact('data'))->render();
})
->editColumn('country_id', function ($states) {
return $states->country ? $states->country->country_name : "N/A";
})
->editColumn('status', function ($states) {

$data = $states;
$statusChangeRoute = route('state.change.status');
return view('partials.datatable.status-switch', compact('data','statusChangeRoute'))->render();
})
->addColumn('action', function ($states) {

$editRoute = route('states.edit', $states->id);
$viewRoute = route('states.show', $states->id);
$controlKeyword = 'state';
return view('partials.datatable.table_edit_delete_action', compact('editRoute','viewRoute','controlKeyword'))->render();
})
->addColumn('DT_RowId', function ($states) {

return "tr_" . $states->id;
})
->rawColumns(['checkes', 'status', 'action'])
->make(true);
}

在这个函数中,我刚刚编辑了 country_id 列并返回了 $states->country->country_name

这是我的 js 函数:

<script type="text/javascript">
$(document).ready(function () {
table = $('#tblState').DataTable({
processing: true,
serverSide: true,
pageLength: 10,
ajax: {
url: "{{ route('admin.states.list') }}",
type: "POST",
data: {_token: "{{csrf_token()}}"}
},
columns: [
{data: 'checkes', name: 'checkes', orderable: false, searchable: false},
{data: 'country_id', name: 'country_id'},
{data: 'state_type', name: 'state_type'},
{data: 'state_code', name: 'state_code'},
{data: 'state_name', name: 'state_name'},
{data: 'status', name: 'status'},
{data: 'action', name: 'action', orderable: false, searchable: false}
],
"bStateSave": true,
initComplete: function (settings, json) {
// called on first time initialization
},
drawCallback: function (settings) {
// called on every server request
// below function is compulsory put here with table id param
initDTCheckBox('tblState');
}
});
});
</script>

这是我的 View

enter image description here

最佳答案

您可以直接在查询中使用:

$states = State::select(['id', 'country_id', 'state_type', 
'state_code', 'state_name', 'status'])
->orderBy('country_id', 'Asc')
->with('country');

然后直接在你的JS中使用:

{data: 'country.country_name', name: 'country.country_name'},

addColumn 只影响列的 View ,而不影响查询。所以当你想排序,或者在你的数据表中搜索时,插件找不到值,你必须在查询中有它

关于javascript - 拉维尔 : Data-table search option not working using relationship table field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57841188/

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