Yajra Datatable搜索栏在laravel中不工作

wlzqhblo  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(134)

Yajra数据表搜索栏在两个文本类型数据表显示空x1c 0d1x后不工作
搜索后

Jquery ->

$(function () {

    var table = $('#projectcashflow_data').DataTable({
      processing: true,
        serverSide: true,
        lengthMenu: [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
         dom: 'Blfrtip',
                    "buttons": [
                        {
                            "extend": 'excelHtml5',
                             "title": 'Expense_Manage',
                            "text": '<button class="btn"><i class="fa fa-file-excel-o" style="color:    green;"></i>  Excel</button>',
                            "titleAttr": 'Excel',
                            exportOptions: {
                         modifier : {
                              page : 'all',
                         },
                          rows: { order:'current', search: 'none' },
                        columns: [1, 2, 3, 4, 5],
                      
                    }
                        },
                    ],
        ajax: "{{ route('projectcashflow.load-projects-cashflow') }}",
        columns: [
            {data: 'DT_RowIndex', name: 'DT_RowIndex'},
            {data: 'edit.projects_name', name: 'projects_name'},
        
            {
                data: 'action', name: 'action', 
                orderable: true, 
                searchable: true
            },
        ],
     
        
    });

  });

服务器端代码(Laravel)My DataTable查询函数如下。->

public function load_projects_cashflow(Request $request){
        if($request->ajax()){
            $data = ProjectCashflow::select('project_cashflows.*','projects.project_name')
                    ->leftjoin('projects', 'projects.id', 'project_cashflows.project_id')
                    ->get();

             return Datatables::of($data)
                    ->addIndexColumn()
                    ->addColumn('action', function($row){
                        $dataedit =  route('projectcashflow.edit-projectscashflow',$row->id);
                        $actionBtn = "<a class='' style='margin-right:15px' href='{$dataedit}'
                        ><i class='bx bx-edit-alt me-1'></i></a>";
                        $viewButton = "<a style='margin-right:15px' class='projectcashflowdetails' data-id='{$row->id}' data-toggle='modal' data-target='#projectcashflowdetails' style='cursor: pointer;' ><i class='fa-solid fa-eye me-1'></i></a>";
                        $deletebutton =  "<a class='projectcashflow_delete' id='projectcashflow_delete' style='cursor: pointer;' data-id='$row->id'><i class='bx bx-trash-alt me-1'></i></a>";
                        return $actionBtn.' '.$viewButton.' '.$deletebutton;
                    })
                    ->editColumn('edit', function($row){

                            $projectname = "<a class='projectcashflowdetails' data-id='{$row->id}' data-toggle='modal' data-target='#projectcashflowdetails' style='cursor: pointer;' >$row->project_name</a>";

                        return [
                           
                            'projects_name' => $projectname,
                        ];
                    })
                    ->escapeColumns('edit')
                    ->rawColumns(['action','edit'])
                    ->make();     
        }
    }

我使用了左连接查询。现在的问题是Yajra DataTable搜索栏不工作。它既没有给出任何错误,也没有搜索结果。
需要一个解决方案,每件事都在代码中很好,但我不知道为什么不工作,

tjvv9vkg

tjvv9vkg1#

您可以使用filter()方法进行搜索。例如:

->filter(function ($query) {
    if ($keyword = $this->request->input('search.value')) {
        return $query->whereHas('projects', function ($subQuery) use ($keyword) {
            return $subQuery->where('project_name', 'LIKE', '%' . $keyword . '%');
        });
    }
    return $query;
})

自定义过滤。参见:https://yajrabox.com/docs/laravel-datatables/master/filter-column
下面是我自己的项目的一个例子:

->filter(function ($query) {
    if ($keyword = $this->request->input('search.value')) {
        return $query
            ->whereHas('address', function ($subQuery) use ($keyword) {
                return $subQuery
                    ->where('name', 'LIKE', '%' . $keyword . '%')
                    ->orWhere('email', 'LIKE', '%' . $keyword . '%')
                    ->orWhere('phone', 'LIKE', '%' . $keyword . '%');
            })
            ->orWhereHas('user', function ($subQuery) use ($keyword) {
                return $subQuery
                    ->where('name', 'LIKE', '%' . $keyword . '%')
                    ->orWhere('email', 'LIKE', '%' . $keyword . '%')
                    ->orWhere('phone', 'LIKE', '%' . $keyword . '%');
            })
            ->orWhere('code', 'LIKE', '%' . $keyword . '%');
    }

    return $query;
});

相关问题