简体   繁体   中英

How to combine two columns in one in Yajra datatable

I want to merge restaurant name and location to be in one column, Im uisng laravel and I don't want to merge it using the editcolumn from the controller because this will not allow search to work and I will get in trouble as Im using eloquent as explain here https://github.com/yajra/laravel-datatables/issues/2293 so I have to return the same name as it comes from the elquent, but I need in the same time to combine tow columns in one

Please note, I cant use combination in controller and return it in one column

         ->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);

I think you should use addColumn instead of editColumn , to display merged name:

->addColumn('full_restaurant_name', function ($data) {
           $data->restaurant_name.' - '.$data->restaurant_city
       });

By doing this, you are not changing the existing column restaurant_name , but adding another field. Now, if you want to keep this column searchable, you could do two things:

  1. Add custom filter
  2. Define the new full_restaurant_name column like this:

{data: 'full_restaurant_name', name: 'restaurant_name'}

This way, search will work on the full_restaurant_name , because it's referred as restaurant_name column in your database.

PS: I haven't tested this, so if you occur any problems, let me know.

try to use custom filter 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;
    })
    ...;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM