2016-09-21 57 views
1

我堅持我的addColumn語句添加if聲明:Laravel Yajra DataTable的if語句上addColumn

$inquiries = Inquiry::with('customer')->get(); 

return Datatables::of($inquiries) 
    ->editColumn('created_at', function ($inquiry) { 
     return $inquiry->created_at->format('d-m-Y'); 
    }) 
    ->editColumn('due_date', function ($inquiry) { 
     return Carbon::parse($inquiry->due_date)->format('d-m-Y'); 
    }) 
    ->editColumn('status', '@if($status == 0) Pending @elseif($status == 1) Accept @else Cancel @endif') 
    ->addColumn('action', function ($inquiry) { 
     return ' 
     <a href="/inquiries/' . $inquiry->id . '" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> View</a> 
     <a href="/inquiries/' . $inquiry->id . '/edit" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> Edit</a> 
     <a href="#" id="delete-button" data-id="' . $inquiry->id . '" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> Delete</a> 
     '; 
    }) 
    ->make(true); 

我試圖把@if @endif但現在看來它打印出來,而不是成爲一個功能

我想根據狀態顯示操作按鈕。

任何解決方案?

謝謝

回答

0

你能做下面的事嗎?

->editColumn('status', function ($inquiry) { 
    if ($inquiry->status == 0) return 'Pending'; 
    if ($inquiry->status == 1) return 'Accept'; 
    return 'Cancel'; 
}) 
0

只是櫃面,任何人都還在找:)

您可以在JavaScript中的觀點類似這樣的內做到這一點定義數據表的列後:

"createdRow": function (row, data) { 

      $('td', row).eq('COLUMN_INDEX_HERE').html($('<label>').attr({ 
       class: (data.status == 0) ? 'label label-warning' : ((data.status == 1) ? 'label label-success': 'label label-danger') 
      }).html((data.status == 0) ? 'Pending' : ((data.status == 1) ? 'Approved': 'Canceled'))); 

      }