如何使用Laravel Yajra插件为数据表添加CSV,PDF按钮?

时间:2017-05-02 11:10:27

标签: laravel

我正在使用Laravel的Yajra插件创建数据表。我正在使用查询构建器表单。(像这样Click here

我想在数据表中添加CSV,PDF按钮。

我知道根据文档可以完成Like this

问题是我已经使用查询构建器完成了。现在我无法改变我的代码。

请帮帮我。

我的jquery代码如下:

<script type="text/javascript">
  $(document).ready(function(){
    $('body').addClass('sidebar-collapse');
    var cat = "{{$cat}}";
    $('#unreconcil_datatable').DataTable({
      processing: true,
      serverSide: true,
      ajax: '{!! route('get_datatable',array('cat'=>$cat)) !!}',
     columns: [
            { data: 'unrelines_uniq_num', name: 'unrelines_uniq_num' },
            { data: 'unrelines_bank_accno', name: 'unrelines_bank_accno' },
            { data:'unrelines_rficreated',name:'unrelines_rficreated'},
            { data: 'unrelines_roicreated', name: 'unrelines_roicreated' },
            { data: 'unrelines_bank_name', name: 'unrelines_bank_name' },
            { data: 'unrelines_line_number', name: 'unrelines_line_number' },
            { data: 'unrelines_state_date', name: 'unrelines_state_date' },
            { data: 'unrelines_trans_date', name: 'unrelines_trans_date' },
            { data: 'unrelines_trans_amount', name: 'unrelines_trans_amount' },
            { data: 'unrelines_unrec_amt', name: 'unrelines_unrec_amt' },
            { data: 'unrelines_desc', name: 'unrelines_desc' },
            { data: 'unrelines_variance', name: 'unrelines_variance' },
            { data: 'unrelines_cstatus', name: 'unrelines_cstatus' },
            { data: 'unrelines_assigned', name: 'unrelines_assigned' },
            { data: 'unrelines_created_date', name: 'unrelines_created_date' },
            {data: 'tat', name: 'tat', orderable: false, searchable: false},
            {data: 'action', name: 'action', orderable: false, searchable: false}
        ]
    });

  });
</script>

我的控制器方法如下:

public function getdatatable($cat){
            $list = AvailableStatementLines::select([DB::raw(" '$cat' AS cat"),'unrelines_id','unrelines_uniq_num','unrelines_bank_accno','unrelines_rficreated','unrelines_roicreated','unrelines_bank_name','unrelines_line_number','unrelines_state_date','unrelines_trans_date','unrelines_trans_amount','unrelines_unrec_amt','unrelines_desc','unrelines_variance','unrelines_cstatus','unrelines_assigned','unrelines_created_date','unrelines_trans_type','unrelines_currency','unrelines_created_by_name','unrelines_ustatus',DB::raw("IF(unrelines_cstatus='closed', '',
                ROUND(ABS(TIMESTAMPDIFF(MINUTE, date(unrelines_lastupdate), curdate()))/1440 - ABS(DATEDIFF(ADDDATE(curdate(), INTERVAL 1 -DAYOFWEEK(curdate()) DAY), ADDDATE(date(unrelines_lastupdate), INTERVAL 1 -DAYOFWEEK(date(unrelines_lastupdate)) DAY))) / 7 * 2 - (DAYOFWEEK(IF(date(unrelines_lastupdate) < curdate(), date(unrelines_lastupdate), curdate())) = 1) - (DAYOFWEEK(IF(date(unrelines_lastupdate) > curdate(), date(unrelines_lastupdate), curdate())) = 7),0)
                ) AS tat")])->where('unrelines_ucountry',Session::get('country'))->where('unrelines_display',1);

            switch($cat){
                case 'rfi':
                    $list1 = $list->where('unrelines_cstatus','Assigned');
                break;
                case 'roi':
                    $list1 = $list->where('unrelines_cstatus','Solution Provided');
                break;
                case 'closed':
                    $list1 = $list->where('unrelines_cstatus','Closed');
                break;
                default:
                    $list1 = $list->whereNotIn('unrelines_cstatus',['Closed','Assigned','Solution Provided']);
                break;
            }

            return Datatables::of($list1)
            ->addColumn('unrelines_uniq_num',function($list1){
                return "<input type='checkbox' class='uniqnums' name='uniquenum[]' value='".$list1->unrelines_id."'>".$list1->unrelines_uniq_num;
            })
            ->addColumn('action',function($list1){
                return "<button type='button' class='btn btn-xs btn-info viewLine'  category=".$list1->cat." unrelines=".$list1->unrelines_id.">View</button>";
            })
            ->setRowClass(function ($list1) {
                return $list1->tat > 7 ? 'orange' : ($list1->tat > 5 ? 'red' : ' ');
            })
            ->make(true);
        }

1 个答案:

答案 0 :(得分:0)

您应该在控制器中应用此方法。

public function html()
 {
      return $this->builder()
        ->columns([
            'id',
            'name',
            'email',
           'created_at',
           'updated_at',
        ])
        ->parameters([
          'dom' => 'Bfrtip',
          'buttons' => ['csv', 'excel', 'pdf', 'print', 'reset', 'reload'],
      ]);
}