【问题标题】:How to combine two columns in one in Yajra datatable如何在 Yajra 数据表中将两列合二为一
【发布时间】:2021-03-11 08:55:18
【问题描述】:

我想将餐厅名称和位置合并到一个列中,我正在使用 laravel,我不想使用控制器中的 editcolumn 合并它,因为这将不允许搜索工作,我会遇到麻烦我在这里使用雄辩的解释https://github.com/yajra/laravel-datatables/issues/2293 所以我必须返回与来自 elquent 的名称相同的名称,但我需要同时将两个列合并为一个

请注意,我不能在控制器中使用组合并在一列中返回它

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

【问题讨论】:

    标签: javascript php laravel datatable


    【解决方案1】:

    我认为您应该使用addColumn 而不是editColumn 来显示合并名称:

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

    通过这样做,您不会更改现有列restaurant_name,而是添加另一个字段。现在,如果您想保持此列可搜索,您可以做两件事:

    1. 添加自定义filter
    2. 像这样定义新的full_restaurant_name 列:

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

    这样,搜索将对full_restaurant_name 起作用,因为它在您的数据库中被称为restaurant_name 列。

    PS:我还没有测试过,所以如果你遇到任何问题,请告诉我。

    【讨论】:

    • 很高兴我能帮上忙。编码愉快!
    【解决方案2】:

    尝试使用自定义过滤器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;
        })
        ...;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-06
      • 2014-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多