如何仅将特定列从 Laravel Blade table 视图导出到 Excel 文件?
How Export only specific columns from Laravel Blade table view to Ecxel file?
所以我在 blade 视图文件上有一个 table,我已经成功地将它导出到 excel 文件中。现在我想导出而不导出“操作”列。我该怎么做?
我在下面附上了我的代码和 blade 文件,它运行良好。我只想导出除操作列之外的所有内容,因为它包含用于数据库操作的按钮
这是我的导出 CLASS 代码:
public function view(): View
{
return view ('ims.view_inventory_history_table', [
'data' => inbound_history::all()
]);
}
public function headings(): array
{
return [
'ID',
'Warehouse',
'SKU',
'Child SKU',
'Units',
'Invoice No.',
'Container No.',
'Entry Date'
];}
/**
* @return array
*/
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$cellRange = 'A1:W1'; // All headers
$event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(14);
},
];
}}
这是我的控制器功能:
public function export_view()
{
return Excel::download(new inboundHistory(), 'inboundHistory.xlsx');
}
这是我的路线:
Route::Get('inbound_history/export_view' ,'imsController@export_view')->name('inbound_history.export_view');
这是我的 BLADE TABLE 视图:
<table id="datatables" class="table table-striped table-no-bordered table-hover" cellspacing="0" width="100%" style="width:100%">
<thead>
<tr>
<th style="width:5%">ID</th>
<th>Warehouse</th>
<th>SKU</th>
<th>Child SKU</th>
<th>Cases</th>
<th>Units</th>
<th>Invoice No.</th>
<th>Container No.</th>
<th>Entry Date</th>
<th class="disabled-sorting text-right" style="width:12%">Actions</th>
</tr>
</thead>
<tbody>
@foreach ($data as $row)
<tr>
<td>{{$row['id']}}</td>
<td>{{$row['warehouse']}}</td>
<td>{{$row['sku_parent']}}</td>
<td>{{$row['sku_child']}}</td>
<td>{{$row['total_cases']}}</td>
<td>{{$row['total_units']}}</td>
<td>{{$row['invoice_no']}}</td>
<td>{{$row['container_no']}}</td>
<td>{{$row['date_rec']}}</td>
<td class="td-actions text-right">
{{-- <a rel="tooltip" class="btn btn-success btn-link" href="{{action('imsController@edit',$row['id'])}}">
<i class="material-icons">edit</i></a> --}}
<a rel="tooltip" class="btn btn-danger btn-link" href="{{action('imsController@destroy',$row['id'])}}" onclick = "if (! confirm('Confirm: Press OK to delete the Entry.')) { return false; }"style="color: red;">
<i class="material-icons">close</i></a>
</td>
</tr>
@endforeach
</tbody>
我不知道我是否理解正确,但你为什么不这样做:
当您想为用户创建视图时,将一个额外的变量传递给您的 blade 模板,例如 $isView
。
然后在您的 blade.php 模板中执行如下操作:
@isset($isView)
<th class="disabled-sorting text-right" style="width:12%">Actions</th>
@endisset
// do the same @isset test for the corresponding <td> element
当您想将其呈现为 excel 时,您只需不传递此变量并且不会呈现该列。
所以我在 blade 视图文件上有一个 table,我已经成功地将它导出到 excel 文件中。现在我想导出而不导出“操作”列。我该怎么做?
我在下面附上了我的代码和 blade 文件,它运行良好。我只想导出除操作列之外的所有内容,因为它包含用于数据库操作的按钮
这是我的导出 CLASS 代码:
public function view(): View
{
return view ('ims.view_inventory_history_table', [
'data' => inbound_history::all()
]);
}
public function headings(): array
{
return [
'ID',
'Warehouse',
'SKU',
'Child SKU',
'Units',
'Invoice No.',
'Container No.',
'Entry Date'
];}
/**
* @return array
*/
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$cellRange = 'A1:W1'; // All headers
$event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(14);
},
];
}}
这是我的控制器功能:
public function export_view()
{
return Excel::download(new inboundHistory(), 'inboundHistory.xlsx');
}
这是我的路线:
Route::Get('inbound_history/export_view' ,'imsController@export_view')->name('inbound_history.export_view');
这是我的 BLADE TABLE 视图:
<table id="datatables" class="table table-striped table-no-bordered table-hover" cellspacing="0" width="100%" style="width:100%">
<thead>
<tr>
<th style="width:5%">ID</th>
<th>Warehouse</th>
<th>SKU</th>
<th>Child SKU</th>
<th>Cases</th>
<th>Units</th>
<th>Invoice No.</th>
<th>Container No.</th>
<th>Entry Date</th>
<th class="disabled-sorting text-right" style="width:12%">Actions</th>
</tr>
</thead>
<tbody>
@foreach ($data as $row)
<tr>
<td>{{$row['id']}}</td>
<td>{{$row['warehouse']}}</td>
<td>{{$row['sku_parent']}}</td>
<td>{{$row['sku_child']}}</td>
<td>{{$row['total_cases']}}</td>
<td>{{$row['total_units']}}</td>
<td>{{$row['invoice_no']}}</td>
<td>{{$row['container_no']}}</td>
<td>{{$row['date_rec']}}</td>
<td class="td-actions text-right">
{{-- <a rel="tooltip" class="btn btn-success btn-link" href="{{action('imsController@edit',$row['id'])}}">
<i class="material-icons">edit</i></a> --}}
<a rel="tooltip" class="btn btn-danger btn-link" href="{{action('imsController@destroy',$row['id'])}}" onclick = "if (! confirm('Confirm: Press OK to delete the Entry.')) { return false; }"style="color: red;">
<i class="material-icons">close</i></a>
</td>
</tr>
@endforeach
</tbody>
我不知道我是否理解正确,但你为什么不这样做:
当您想为用户创建视图时,将一个额外的变量传递给您的 blade 模板,例如 $isView
。
然后在您的 blade.php 模板中执行如下操作:
@isset($isView)
<th class="disabled-sorting text-right" style="width:12%">Actions</th>
@endisset
// do the same @isset test for the corresponding <td> element
当您想将其呈现为 excel 时,您只需不传递此变量并且不会呈现该列。