我如何在Laravel的一个页面上有多个Yajra数据表?



问题:我需要使用" Product datatable ";现金支付页其中现金数据表;此页已存在

我已经搜索了这个问题,但我很惊讶以前没有人问过这个问题,所以当我找到这个问题的解决方案时,我决定与其他程序员分享我的解决方案。我希望它对你有用。

让我先说一下我在做什么版本。

  • "laravel/framework":"^ 8.40">
  • "yajra/laravel-datatables-buttons":"^ 4.0",
  • "yajra/laravel-datatables-html":"^ 4.0",
  • "yajra/laravel-datatables-oracle":"~ 9.0">

现在我显示我的项目结构

I have route:list

GET|HEAD | cash | cash.index| AppHttpControllersCashController@index
GET|HEAD | product | product.index| AppHttpControllersProductController@index

CashController.php

public function index(CashDataTable $cashDataTable)
{
return $cashDataTable->render('cash.index');
}

Cash.index.blade.php

{!! $dataTableCash->table(['id'=>'dataTable']) !!}
{!! $dataTableCash->scripts() !!}

CashDataTable.php

使用App 模型现金;

class CashDataTable extends DataTable
public function dataTable($query)
{
$dataTable = new EloquentDataTable($query);
return $dataTable->addColumn('action', 'cash.datatables_actions');
}

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

ProductDataTable.php

use AppModelsProduct;
class ProductDataTable extends DataTable
public function dataTable($query)
{
$dataTable = new EloquentDataTable($query);
return $dataTable->addColumn('action', 'product.datatables_actions');
}

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

我想"Yajra DataTables Services;"设计只显示一个表,但我找到了一个解决方案:

首先,我们需要重写和更改CashDataTable和ProductDataTable中的默认数据表变量,其次,我们需要直接在HTML minifiedAjax中设置路由URL。

CashDataTable.php

use AppModelsCash;
protected $dataTableVariable = 'dataTableCash';
class CashDataTable extends DataTable
public function dataTable($query)
{
$dataTable = new EloquentDataTable($query);
return $dataTable->addColumn('action', 'cash.datatables_actions');
}

public function html()
{
return $this->builder()
->columns($this->getColumns())
->minifiedAjax('/cash')
...
}
...
}

ProductDataTable.php

use AppModelsProduct;
class ProductDataTable extends DataTable
protected $dataTableVariable = 'dataTableProduct';
public function dataTable($query)
{
$dataTable = new EloquentDataTable($query);
return $dataTable->addColumn('action', 'product.datatables_actions');
}

public function html()
{
return $this->builder()
->columns($this->getColumns())
->minifiedAjax('/product')
...
}
...
}
现在在CashController中,我们需要将变量设置为HTML构建器。

CashController.php

public function index(CashDataTable $cashDataTable, ProductDataTable $productDataTable)
{
$dataTableProduct = $productDataTable->html();
return $cashDataTable->render('cash.index', compact('dataTableProduct'));
}

Cash.index.blade.php

{!! $dataTableCash->table(['id'=>'dataTableCash']) !!}
{!! $dataTableCash->scripts() !!}
{!! $dataTableProduct->table(['id'=>'dataTableProduct']) !!}
{!! $dataTableProduct->scripts() !!}

相关内容

  • 没有找到相关文章

最新更新