Hello readers, am back with another post to let you know how to implement datatables plugin in laravel. Datatables is jQuery plugin for table extender, it provides the functionalities like search, sort, pagination on a table of information to handle the data more effectively. Earlier we implemented search, pagination, pagination on search results individually without using any plugins, here we will do it using datatables plugin.
Step 1 : Initializing datatables
Lets include the required bootstrap, datatables js and css files along with jquery,
We show the data in a table so lets create the table we require,
Note that the table above has an id
id="table" we use that id to initiate datatables,
Step 2 : Fetching data from database
We collect some fake data from mockaroo.com and store it in our database, the table consists of id, first_name, last_name, email, gender, country and salary fields. The sql file can be found here.
Now we set up the database connection details in /.env file or in /config/database.php with appropriate details. for reference post on database set up look here.
Now we create a model file for accessing the data in the database, running the following command will create a model file with the given name at /app/Data.php.
place the following code in it,
now our model is ready, we can now fetch the data from it and pass that data to the view created above,
So in routes/web.php file, place the following code to fetch all the data from the table,
Step 3 : Showing the data
In the previous step we pass a variable
$data to the view, it contains all the data we needed, so the view file where we created a table, we show this data in those
edit the view file as follows,
We looped the
$data variable and displayed all the results in the table.
Step 4 : Deleting and Editing (the laravel part)
As we see in the table the last column ‘Actions’ with edit and delete options..
We have two buttons for edit and delete, when clicked on those a modal will pop up showing the details and asking us to edit and delete the details in that row.
We use only one modal for editing and deleting purpose, as we did in previous tutorial about ajax crud operations, almost the same code we use here too, but with some extra fields in the edit modal form.
Step 4.1 : Edit Modal
So when edit button in action column is clicked, we will adjust the only modal with appropriate buttons and actions like name of the button in the modal, content in the modal.
following is the code executed when edit button is clicked,
Here we used a function
fillmodalData() function to get the details of that particular row, the button’s data attribute contains all the info about that row so we use this inf and set them in the modal,
After the modal is shown, we can edit the data, then the data is saved after passing all the validation rules.
Basically when update button is clicked, it is routed to ajax call
/editdata, it checks for validations, throws errors if validator fails or it saves the data in database. So in the success function of the ajax call first check for errors, if any we display them appropriately or replace the row with new data.
Edit action :
Step 4.2 : Delete Modal
When delete button in actions column is clicked a modal pops up, asking for delete conformation.
When delete is clicked in the modal it is routed to ajax call
/deleteData, which deletes the data from the database and updates the view.
Delete action :
Server side implementation of datatables : http://justlaravel.com/implement-datatables-server-side-laravel/