如何在 Yajra 数据表中将两列合二为一
How to combine two columns in one in Yajra datatable
我想将餐厅名称和位置合并到一列中,Im uisng laravel 我不想使用控制器中的 editcolumn 将其合并,因为这将不允许搜索工作并且我会遇到麻烦,因为我正在使用 eloquent,如此处解释 https://github.com/yajra/laravel-datatables/issues/2293
所以我必须 return 与来自 elquent 的名称相同,但我需要同时将两列合并为一个
请注意,我不能在控制器中使用组合 return 它在一列中
->editColumn('restaurant_name', function ($data) {
return $data->restaurant_name.' - '.$data->restaurant_city;
}) //this one cant be work because it will give me error message when do search
Javascript:
columns: [
{data: 'id', name: 'id', sClass: 'text-center'},
{data: 'user_name', name: 'users.name', sClass: 'text-center'},
{data: 'restaurant_name', name: 'restaurants.name', sClass: 'text-center'},
{data: 'restaurant_city', name: 'locations.city', sClass: 'text-center'},
{data: 'action', name: 'action', sClass: 'text-center',orderable: false, searchable: false},
],
控制器
return $this->select('orders.*', 'users.name')
->join("food_orders", "orders.id", "=", "food_orders.order_id")
->join("foods", "foods.id", "=", "food_orders.food_id")
->join("restaurant_locations", "restaurant_locations.id", "=", "orders.location_id")
->join("restaurants", "restaurants.id", "=", "foods.restaurant_id")
->join('users', 'orders.user_id', '=', 'users.id')
->select('orders.*',"users.name as user_name","restaurants.name as restaurant_name","restaurant_locations.city as restaurant_city")
->groupBy('orders.id');
return DataTables::of(Order::GetAllOrders())
->addIndexColumn()
->editColumn('created_at', function ($data) {
return date('d-m-Y', strtotime($data->updated_at));
})
->addColumn('action', function($row){
return view('admin.orders.all_orders.action-buttons',compact('row'))->render();
})
->rawColumns(['action'])
->make(true);
我认为你应该使用 addColumn
而不是 editColumn
,以显示合并后的名称:
->addColumn('full_restaurant_name', function ($data) {
$data->restaurant_name.' - '.$data->restaurant_city
});
通过这样做,您不会更改现有列 restaurant_name
,而是添加另一个字段。现在,如果您想让该列保持可搜索状态,您可以做两件事:
- 添加自定义filter
- 像这样定义新的
full_restaurant_name
列:
{data: 'full_restaurant_name', name: 'restaurant_name'}
这样,搜索将在 full_restaurant_name
上进行,因为它在您的数据库中被称为 restaurant_name
列。
PS: 我还没有测试过,所以如果你有任何问题,请告诉我。
尝试使用自定义过滤器https://datatables.yajrabox.com/collection/custom-filter
this may help you:
DataTables::of(Order::GetAllOrders())
->addIndexColumn()
->filterColumn('restaurant_name', function($query, $keyword) {
$query->where('restaurant_name', 'like', "%{$keyword}%")
->orWhere('restaurant_city', 'like', "%{$keyword}%"); // you can implement any logic you want here
})
->editColumn('restaurant_name', function ($data) {
return $data->restaurant_name.' - '.$data->restaurant_city;
})
...;
我想将餐厅名称和位置合并到一列中,Im uisng laravel 我不想使用控制器中的 editcolumn 将其合并,因为这将不允许搜索工作并且我会遇到麻烦,因为我正在使用 eloquent,如此处解释 https://github.com/yajra/laravel-datatables/issues/2293 所以我必须 return 与来自 elquent 的名称相同,但我需要同时将两列合并为一个
请注意,我不能在控制器中使用组合 return 它在一列中
->editColumn('restaurant_name', function ($data) {
return $data->restaurant_name.' - '.$data->restaurant_city;
}) //this one cant be work because it will give me error message when do search
Javascript:
columns: [
{data: 'id', name: 'id', sClass: 'text-center'},
{data: 'user_name', name: 'users.name', sClass: 'text-center'},
{data: 'restaurant_name', name: 'restaurants.name', sClass: 'text-center'},
{data: 'restaurant_city', name: 'locations.city', sClass: 'text-center'},
{data: 'action', name: 'action', sClass: 'text-center',orderable: false, searchable: false},
],
控制器
return $this->select('orders.*', 'users.name')
->join("food_orders", "orders.id", "=", "food_orders.order_id")
->join("foods", "foods.id", "=", "food_orders.food_id")
->join("restaurant_locations", "restaurant_locations.id", "=", "orders.location_id")
->join("restaurants", "restaurants.id", "=", "foods.restaurant_id")
->join('users', 'orders.user_id', '=', 'users.id')
->select('orders.*',"users.name as user_name","restaurants.name as restaurant_name","restaurant_locations.city as restaurant_city")
->groupBy('orders.id');
return DataTables::of(Order::GetAllOrders())
->addIndexColumn()
->editColumn('created_at', function ($data) {
return date('d-m-Y', strtotime($data->updated_at));
})
->addColumn('action', function($row){
return view('admin.orders.all_orders.action-buttons',compact('row'))->render();
})
->rawColumns(['action'])
->make(true);
我认为你应该使用 addColumn
而不是 editColumn
,以显示合并后的名称:
->addColumn('full_restaurant_name', function ($data) {
$data->restaurant_name.' - '.$data->restaurant_city
});
通过这样做,您不会更改现有列 restaurant_name
,而是添加另一个字段。现在,如果您想让该列保持可搜索状态,您可以做两件事:
- 添加自定义filter
- 像这样定义新的
full_restaurant_name
列:
{data: 'full_restaurant_name', name: 'restaurant_name'}
这样,搜索将在 full_restaurant_name
上进行,因为它在您的数据库中被称为 restaurant_name
列。
PS: 我还没有测试过,所以如果你有任何问题,请告诉我。
尝试使用自定义过滤器https://datatables.yajrabox.com/collection/custom-filter
this may help you:
DataTables::of(Order::GetAllOrders())
->addIndexColumn()
->filterColumn('restaurant_name', function($query, $keyword) {
$query->where('restaurant_name', 'like', "%{$keyword}%")
->orWhere('restaurant_city', 'like', "%{$keyword}%"); // you can implement any logic you want here
})
->editColumn('restaurant_name', function ($data) {
return $data->restaurant_name.' - '.$data->restaurant_city;
})
...;