Start a Project

How to Create Datagrid in Bagisto

Creating a custom datagrid in Bagisto is a great way to manage and show backend data effectively.

A special setup allows you to meet unique store requirements while keeping the admin interface tidy.

Regular backend tables usually don’t have the flexibility to manage complicated database joins or specific business rules.

This can make it hard to handle large amounts of data and lead to a frustrating experience for store admins.

Therefore, this guide provides a modern workflow to build a high-performance grid from scratch.

Steps to Create a Custom Datagrid in Bagisto

Let’s explore the steps involved in building a custom Datagrid in Bagisto.

Step 1: Set Up the Directory

First, ensure your custom package (e.g.,bagisto\TestPackage)is set up.

Then, create a Datagrid folder inside the package’s src directory to hold all grid logic.

Step 2: Create the Datagrid Class

Next, create HelloWorldDatagrid.php in the Datagrid folder.

Extend the core DataGrid abstract class, making sure the class name matches the file name.

Step 3: Configure the Query Builder

In Webkul\Ui\DataGrid\DataGrid abstract class, two main methods are used: prepareQueryBuilder() and addColumns().

Let’s use a customer database table as an example to fetch our records.

This method pulls the exact data you need from your database and prepares the records for the datagrid.

Understanding the Methods:

1. prepareQueryBuilder() – This method is used to fetch data from the database or collections based on a database query.

When the records are retrieved, the $this->setQueryBuilder($queryBuilder)  method is called.

Inside the prepareQueryBuilder(), we use the following function to retrieve the data:

2. setQueryBuilder($queryBuilder) – This method is defined in Webkul\Ui\DataGrid\DataGrid.

This method is used to set the $queryBuilder.

3. addFilter() –This lets you set up search filters so users can easily find specific rows based on their needs.

Step 4: Add Columns to the Datagrid

Now, we need to tell the datagrid which columns to display.

We do this using the addColumns() function, passing our settings as key-value pairs.

These settings define how each column behaves, including sorting, searching, and filtering.

Essential Keys Used in the Datagrid

  • index – Assign a unique value to this key so you can uniquely identify data, and operations can run based on the index value, similar to an id.
  • label – Use this key to set the datagrid column header name.
  • type – It defines the type of data in the column, such as number, boolean, etc.
  • searchable – This accepts boolean values true or false to make the column searchable.
  • sortable – This accepts boolean values true or false to make the column sortable.
  • filterable – This accepts boolean values true or false to make the column filterable.
  • wrapper – Performs actions based on conditions or customization of the value.

Step 5: Render the Grid in Your Blade File

To show the datagrid on the frontend, you need to call the datagrid-plus Vue component inside your Blade view file.

Then, just pass the admin route name to it so the datagrid can load the data.

Step 6: Define the Route

Your Blade file needs a valid route to fetch the grid data.

Open your package’s routing file and set up a new endpoint protected by the admin middleware.

This ensures that only authorized users can access the datagrid.

Step 7: Create the Controller

Finally, create the controller that handles the request.

Then, it returns the JSON datagrid response if called via AJAX; otherwise, it loads the Blade view for standard page visits.

Here is the front view of the created datagrid.

Adding Actions and Mass Actions

Some additional actions can also be performed in the datagrid using prepareActions() and prepareMassActions():

prepareMassActions() – Use this method to apply actions to multiple records of data, such as edit or delete.

prepareActions() – It is used to perform a single action like update or delete. In this method, addAction() defines a particular action.

addAction() – Use this method to add actions like edit or delete to each row of the datagrid.

Final datagrid looks like this.

Note:  I explored this while contributing to the Laravel-based project Bagisto.

Additionally, you can learn a lot from the enterprise-level open-source Bagisto and contribute to gain practical experience.

Final Thoughts

Creating a custom datagrid in Bagisto is an important step in managing and displaying your store data in a clear, structured way.

This setup helps improve data handling, searching, and overall admin efficiency as your business and data grow.

Additionally, you can hire Laravel developers to meet your project requirements and ensure successful implementation.

Thank you for reading this guide. If you have any questions, feel free to reach out to our support team.

Exit mobile version