Here we are going to discuss how to implement multiple Datagrid in Bagisto. I am assuming that you are already familiar with the default Datagrid creation. If not then you can check it from here,
https://devdocs.bagisto.com/1.x/advanced/datagrid.html#datagrid
Here we are going to discuss implementing multiple Datagrid in Bagisto.
Now, let’s get started,
As the default datagrid will handle only a single request at a time which means filtration, sorting and many other operations will get conflicted when you try to implement the multiple datagrids. To overcome this we have provided a trait ProvideDataGridPlus
in the namespace Webkul\Ui\DataGrid\Traits
.
When you add this trait to your database, then you have access to the toJson()
method which provides the JSON format data from the collection. The whole idea behind this is to add the component in the view file and provide the data source which will hit the controller via. ajax and in the controller, this will hit the toJson()
method which will provide the JSON in response.
Let’s take an example of the product datagrid,
- Below is the
ProductDatagrid
class present in theWebkul\Admin\DataGrids
,
12345678namespace Webkul\Admin\DataGrids;class ProductDataGrid extends DataGrid{...} - Import the
ProvideDataGridPlus
trait in the mentioned class,
123456789101112namespace Webkul\Admin\DataGrids;use Webkul\Ui\DataGrid\Traits\ProvideDataGridPlus;class ProductDataGrid extends DataGrid{use ProvideDataGridPlus;...} - After that
toJson()
method will be available in the datagrid instance which will provide data to the component. - Now you need to create one route and method from which the response will come. Let us take an example of the product listing page. Now go to
ProductController
in the namespaceWebkul\Product\Http\Controllers, there is an index method, just use the
toJson().
123456789101112131415161718192021use Webkul\Admin\DataGrids\ProductDataGrid;class ProductController extends Controller{/*** Display a listing of the resource.** @return \Illuminate\View\View*/public function index(){if (request()->ajax()) {return app(ProductDataGrid::class)->toJson();}return view($this->_config['view']);}...} - Now, in the view portion use this component i.e. datagrid-plus` and add the URL from where it will load the JSON data,
1234567<div class="page-content">...<datagrid-plus src="{{ route('admin.catalog.products.index') }}"></datagrid-plus>...</div> - Similarly, you can add multiple datagrid as per your need.