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

标签 javascript php laravel datatables

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

这是我的 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/

相关文章:

javascript - $观察 Angular 指令中的数据变化

javascript - onClick 将箭头移动到选定的项目符号点 - 最佳方法?

javascript - typescript :是否可以只为导出类型中的一个键设置状态?

php - PDO 使用 OR 数组选择查询

php - ajax加载内容中的CSS

php - 为什么 127.0.0.1 在 Laravel .env 和 database.php 文件中不起作用,但 'localhost' 可以?

javascript - mongodb 注入(inject)的可能性

php - 为什么 PHP 数组示例会留下逗号?

php - Laravel 嵌套资源和可重用 Controller

php - Laravel Observers - 更新会无休止地触发自身并产生 bcrypt 错误