数据表使用Yajra重新排序Laravel中的列

时间:2017-05-19 07:37:18

标签: laravel datatable

我正在使用Yajra处理Laravel Datatables。 我试图通过传递数组值来填充表。

我的表格结构如下:

<table id="daily_datatable" class="table table-striped table-bordered">
<thead>
    <tr>
        <th rowspan="2">Region</th>
        <th rowspan="2">Country</th>
        <th rowspan="2">No of accounts</th>
        <th rowspan="2">Bank Account Pending</th>
    </tr>
</thead>

我的jquery调用如下:

$('#daily_datatable').DataTable({
    processing:true,
    serverSide:true,
    rowReorder: true,
    ajax:
    {
        url:"{!! URL::to('dailyreportDatatable') !!}"
    },
    dom: 'Bfrtip',
    buttons: ['pageLength','csv','excel', 'pdf'],
    columns:[
        {data:'region',name:'region'},
        {data:'country',name:'country'},
        {data:'noofaccounts',name:'noofaccounts'},
        {data:'accountpending',name:'accountpending'},  
    ]
});

我的控制器方法如下:

public function dailydatatable(){
    $final = new Collection;
    $from_date = date("Y-m-d");
    $to_date = date("Y-m-d");
    $pastfrom_date  = date("Y-m-d", strtotime("-2 days"));
    $pastto_date    = date("Y-m-d", strtotime("-2 days"));
    $country = AccMaster::select('ms_region','ms_country','ms_bacc_num')->groupby('ms_country')->get()->toArray();
    foreach($country as $countryCode){
        $pending = AccMaster::select('ms_bacc_num')->where('ms_country',$countryCode['ms_country'])->get()->toArray();
        $bankacc = $this->getBankAcc($pending);

        $final->push([
            'region'=>$countryCode['ms_region'],
            'country'=>$countryCode['ms_country'],
            'noofaccounts'=>$this->getAccounts($countryCode['ms_country']),
            'accountpending'=>$this->getAccPending($countryCode['ms_country'],$bankacc),
        ]);
    }
    return Datatables::of($final)->make(true);
}

我无法绘制数据表。我收到了错误

  

jquery.dataTables.min.js:31未捕获的TypeError:无法设置属性   未定义的'0'

1 个答案:

答案 0 :(得分:0)

你需要重写你的html表,因为你忘了结束tr标签

<table id="daily_datatable" class="table table-striped table-bordered">
<thead>
    <tr>
        <th rowspan="2">Region</th>
        <th rowspan="2">Country</th>
        <th rowspan="2">No of accounts</th>
        <th rowspan="2">Bank Account Pending</th>
    </tr> 
</thead>