将复选框添加到数据表 - Jquery & Laravel(英语:Jquery & Laravel)



我正在尝试向我的数据表添加一个复选框,但是当我运行项目时,它会返回控制器中显示的文本中的复选框。我该如何从控制器中完成此操作?

运行项目时,我会在模板中获得复选框,而是显示原始文本。发生了什么事?

public function getItem()
{
  $items = Item::all();
  return Datatables::of($items)->addColumn('checkbox', function ($item) {
    return '<input type="checkbox" id="'.item->id.'" name="someCheckbox" />';
  })->make(true);     
}

查看

 oTable = $('#users-table').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": "{{ route('datatable.getitems') }}",
        "columns": [
            {data: 'checkbox', name: 'checkbox', orderable: false, searchable: false},      
            {data: 'name', name: 'name'},
            {data: 'action', name: 'action', orderable: false, searchable: false}        
        ],
    });

您的代码中有错误:

return '<input type="checkbox" id="'.item->id.'" // should be $item

Laravel DataTables软件包默认情况下逃脱了所有列的内容 - action列除外。要排除您的checkbox列逃脱,您有两个选择:

  1. config/datatables.php中搜索action并将checkbox添加到该数组中。注意:这将在全球范围内排除本列

  2. ->rawColumns(['action', 'checkbox'])添加到您的定义

->editColumn('select_orders', static function ($row) {
            return '<input type="checkbox" name="registrations[]" value="'.$row->id.'"/>';
        })->rawColumns(['select_orders'])

Laravel DataTables软件包默认情况下逃脱了所有列的内容 - Action列除外。为了将复选框排除在逃脱中,您有两个选择:

config/datatables.php中搜索action,然后将复选框添加到该数组。注意:这将在全球范围内排除此列

'raw' => ['action', 'checkbox'],

在您的datatable.js文件中

{data: 'select_orders', name: 'select_orders', searchable: false, orderable: false},

无需修改config/datatables.php文件,您可以在返回数据提示指令中直接修改它,添加:

->rawColumns(['checkbox','name_column'])

您可以这样说:

public function getItem()
{
  $items = Item::all();
  return Datatables::of($items)->addColumn('checkbox', function ($item) {
    return '<input type="checkbox" id="'.item->id.'" name="someCheckbox" />';
  })
  ->rawColumns(['checkbox'])
  ->make(true);     
}

最新更新