首页 > 解决方案 > 如何在 laravel 数据表中呈现列?

问题描述

我被困了 2 天,我很困惑如何使用 yajra/laravel-datatable 渲染列

postgre用作数据库和yajra/laravel-datatables包。

我有这样的查询生成器

$data = DB::select("SELECT * FROM get_list_count_amount_transaction('chat', 'done', '2019-03-01', '2021-12-31' )")[0];

生成对象的值:(我var_dump()用来查看值)

{ ["list_data"]=> string(2171) 
    "[{"id":"44649ccd-9195-4964-b48c-ed2098077dc5","kd_join":1,"status":"done","booking_date":"2021-04-18","transaction_type":"chat","price":4000.00,"date_create":"2021-04-18T19:56:57"},
      {"id":"e500d2c1-99ae-4436-8ecc-8073f4f05bba","kd_join":1,"status":"done","booking_date":"2021-03-20","transaction_type":"chat","price":10000.00,"date_create":"2021-03-19T21:41:41"}
     ]"
  ["count_transaction"]=> int(12) 
  ["total_amount_transaction"]=> string(9) "160500.00" 
}

我很困惑,如何list_data使用 datatble 渲染成表格

这是我的 html 构建器功能:

    public function html()
    {
        return $this->builder()
            ->columns($this->getColumns())

            ->minifiedAjax()
            ->addAction(['width' => '200px'])
            ->addColumnBefore([
                'defaultContent' => '',
                'data' => 'DT_Row_Index',
                'name' => 'DT_Row_Index',
                'title' => 'No',
                'render' => function () {
                    return 'function(data,type,fullData,meta){return meta.settings._iDisplayStart+meta.row+1;}';
                },
                'orderable' => false,
                'searchable' => false,
                'exportable' => false,
                'printable' => true,
                'footer' => '',
            ]);
    }

    protected function getColumns()
    {
        return [
            ['data' => 'transaction_type', 'name' => 'transaction_type', 'title' => 'Tipe Transaksi', 'orderable' => false],
            ['data' => 'status', 'name' => 'status', 'title' => 'Status', 'orderable' => false],
            ['data' => 'booking_date', 'name' => 'booking_date', 'title' => 'Tanggal Booking', 'orderable' => false],
            ['data' => 'price', 'name' => 'price', 'title' => 'Jumlah', 'orderable' => false],
        ];
    }
}

标签: phplaraveldatatable

解决方案


只需获取 list_data,使用 json_decode 和 laravel 集合来制作您需要的结构。尝试使用map,filtermapWithKeys任何合适的。例如:

collect(json_decode($listData))->mapWithKeys(function($item)){

      //
} 

推荐阅读