LesPerras.com

Datatables in Laravel

When I installed this implementation of Laravel, I used a tutorial. It introduced me to Data-tables with jquery. I love them. I decided they would be great for a site I have.

The Datatables are free except for the editor function. I downloaded the free part as a quick test and tried them out on my site. They worked great. I had a few problems with the download section - the meaning of the buttons was unclear to me, but I got them working without too many problems. So far, so good.

Editor

I decided to try them with the editor. In fact, I loved Datatables so much that I did not deliberate on the point very long. I thought about it for a good … well fifteen minutes or so before I reached for the credit card. Then I was in downloading heaven.

Installation

Now things slowed down. While there is lots of documentation and it looks clear and simple, I found the reality to be otherwise. I did what they said, and then… error. Search. Implement… error. After several iterations of this, (I am not really a JavaScript master),I decided to copy and paste the code from their source in the working examples.

It worked! Well, sort of. My routes were set up, and the methods, returning json. But I kept getting errors. I decided on Datatables partly becuase I thought I would avoid a lot of learning about ajax. I was wrong.

Packages

I went sniffing around https://github.com/yajra/laravel-datatables to see what I could find. This package seemd like an adaptation for the server-side processing which I don’t need, and I could not find any useful links to information on how to use this package for the datatables editor. I decided I could find out myself and learn more, so I left this package although it looked good for server-side processing. Very nice, in fact.

Learning how to debug ajax.

I started to learn all sorts of things, like where to find errors in the chrome developer’s tools, and how to read the returned ajax responses. I have become somewhat of a master at this now. (recently I found out about postman and that would have helped a lot).

Restful responses

I dug deeper when the responses I thought should work did not. I found a page ClientServer Data that told me what I should have looked for at the start. Now I had something concrete to work on. I knew what I needed to output. Next, I had to find a way to get it that way.

{
"data": [
{
"DT_RowId": "row_29",
"first_name": "Fiona",
"last_name": "Green",
"position": "Chief Operating Officer (COO)",
"office": "San Francisco",
"extn": "2947",
"salary": "850000",
"start_date": "2010-03-11"
}]
}
so the part that I was missing was the :

{
"data": [
and the :

 ]
}

at the end. I tried a simple

$var = '{ "data":['.$var.']}';

but I got array to string conversion or object to string conversion errors. Finally, I used the ->toJson () for the collections, and json_encode($var) ; in one instance where I had a stdClass object.

Learning!!

Phew! It was hard but fun and rewarding. I feel like a really learned a lot. Now I want to go back and take on a node based system with socket.io again. Sometime.