[英]How to combine two columns in one in Yajra datatable
我想將餐廳名稱和位置合並到一列中,我正在使用 laravel,我不想使用 controller 中的編輯列合並它,因為這將不允許搜索工作,我會遇到麻煩,因為我使用 controller正如此處解釋的https://github.com/yajra/laravel-datatables/issues/2293所以我必須返回與來自 elquent 的名稱相同的名稱,但我需要同時將兩列合並為一個
請注意,我不能在 controller 中使用組合並將其返回一列
->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},
],
Controller
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
,而是添加另一個字段。 現在,如果您想保持此列可搜索,您可以做兩件事:
full_restaurant_name
列: {data: 'full_restaurant_name', name: 'restaurant_name'}
這樣,搜索將對full_restaurant_name
,因為它在您的數據庫中被稱為restaurant_name
列。
PS:我沒有測試過,所以如果您遇到任何問題,請告訴我。
嘗試使用自定義過濾器https://datatables.yajrabox.com/collection/custom-filter
這可能會幫助您:
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;
})
...;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.