You are not logged in.

Important Note: This forum is being archived and will be completely locked down on the 7th of February. The new forum can be found at http://laravel.io.

Announcement

    Laravel 4 Advanced Architecture: Book Now On Sale

#1 2013-09-07 17:21:38

Chumper
Artisan
Registered: 2013-07-23
Posts: 48

[Package] Datatables for Laravel 4

Datatable

This is a laravel 4 package for the server and client side of datatables at http://datatables.net/
I developed this package because i was not happy with the only existing package at https://github.com/bllim/laravel4-datatables-package so i developed this package with in my opinion is superior.

Features

This package supports:

  • Support for Collections and Query strings (only Collections atm, not finished and tested for queries)

  • Easy to add and order columns

  • Includes a simple helper for the HTML side

  • Use your own functions and presenters in your columns

  • Search in your custom defined columns ( Collection only!!! )

  • Tested! (Ok, maybe not fully, but I did my best smile )

You can find it at: https://github.com/Chumper/Datatable

Please let me know any issues and features you want to have

Offline

#2 2013-09-11 08:03:46

Kostik
Artisan
From: Moscow, Russia
Registered: 2013-05-24
Posts: 125

Re: [Package] Datatables for Laravel 4

thanks

Offline

#3 2013-11-10 02:21:34

Chumper
Artisan
Registered: 2013-07-23
Posts: 48

Re: [Package] Datatables for Laravel 4

I have finally updated the package to include the query builder part.

I have also added the functionality to limit the serching and ordering columns.
A video should follow soon too.

Thanks again for the contributions, especially to jijoel for his ideas.

Offline

#4 2013-11-10 11:36:04

panda720
Artisan
From: Vietnam
Registered: 2013-05-13
Posts: 7

Re: [Package] Datatables for Laravel 4

it is great, I'll try it now smile

Last edited by panda720 (2013-11-10 11:36:47)

Offline

#5 2013-11-15 19:11:06

teddycarebears
Apprentice
Registered: 2013-09-13
Posts: 4

Re: [Package] Datatables for Laravel 4

Hello,

I am not being able to show any data in the tables and when I try to do a dd(User::all()); I am getting :

DataTables warning (table id = 'DataTables_Table_0'): DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error.

Any idea why this could happen ?

Also another interesting thing would be how do you add themes to it ? (of course it is interesting if I can bring data into it first big_smile )

Thanks,
Teddy

Offline

#6 2013-11-16 11:44:17

Chumper
Artisan
Registered: 2013-07-23
Posts: 48

Re: [Package] Datatables for Laravel 4

What does your setup looks like?

If you dd the answer it is not a valid json response and it won't work.
You can theme the table with the given template. Just customise it and provide the path to it in the render method.

Offline

#7 2013-11-16 12:46:12

teddycarebears
Apprentice
Registered: 2013-09-13
Posts: 4

Re: [Package] Datatables for Laravel 4

The setup is exactly as it is described in the github project's site. But the answer shouldn't it be a valid JSON already ? It is the standard call to model to bring data out of the database. Does it need to be prepared before it is sent to datatable ?

Update 1 :

After asking a little on IRC I found out that it would be wise to use User::all()->toJson() but that does not bring any data into the datatable. Doing a var_dump shows everything nicely but nothing is passed to the datatable. Perhaps the order of javascript files is not ok or not enough ? Is something missing from the setup details ?

Update 2 :

After digging a little more I am having a feeling that the problem might be because of the URL but I am not sure 100% and I have no idea how to fix it ...

"sAjaxSource": "http:\/\/localhost:80\/myLaravelApp\/public\/admin\/users\/datatable"

Last edited by teddycarebears (2013-11-18 18:37:02)

Offline

#8 2013-11-21 22:16:55

drpudding
Artisan
Registered: 2013-09-03
Posts: 24

Re: [Package] Datatables for Laravel 4

This looks like a good alternative to the bllim datatables package. As a newbie, it would be nice to have some more extensive examples that demonstrate the full feature set. I see you have a video planned. Look forward to that!

Offline

#9 2013-12-05 11:37:25

puberia
Apprentice
Registered: 2013-03-27
Posts: 4

Re: [Package] Datatables for Laravel 4

I'm using this package but after last update if i dump the query results all its ok, but after passing the query to datatables all fields are being returned and the selected fields are being ignored. Since i have several joins all is messed up.

Offline

#10 2013-12-09 07:34:39

sprim
Apprentice
Registered: 2013-12-09
Posts: 1

Re: [Package] Datatables for Laravel 4

Hi, is this compatible with datatables version 1.9.4?
I can't get it to work. No error message but, there are no data shown in my table. In firebug, this is what it returns in the api call.
{"aaData":[["Agency"],["Country manager"],["Marketing"],["Regional manager"],["Sprim"]],"sEcho":0,"iTotalRecords":5,"iTotalDisplayRecords":5}

I notice that sEcho is always 0 when in the datatabales doc, it says it should be same with the request parameter.

Offline

#11 2013-12-10 16:34:52

Evozi
Apprentice
Registered: 2013-06-01
Posts: 4

Re: [Package] Datatables for Laravel 4

sprim wrote:

Hi, is this compatible with datatables version 1.9.4?
I can't get it to work. No error message but, there are no data shown in my table. In firebug, this is what it returns in the api call.
{"aaData":[["Agency"],["Country manager"],["Marketing"],["Regional manager"],["Sprim"]],"sEcho":0,"iTotalRecords":5,"iTotalDisplayRecords":5}

I notice that sEcho is always 0 when in the datatabales doc, it says it should be same with the request parameter.

It's compatible, I'm using it with version 1.9.4

Offline

#12 2013-12-12 20:31:10

WuceBrillis
Artisan
Registered: 2012-07-10
Posts: 19

Re: [Package] Datatables for Laravel 4

Hi Chumper,

Using your package, working brilliantly.
But not 4.1 compatible yet, any plans to update your package?

Thanks a lot for the effort

Offline

#13 2013-12-13 04:02:08

Chumper
Artisan
Registered: 2013-07-23
Posts: 48

Re: [Package] Datatables for Laravel 4

Ups, didn't get any notifications about answers here, so i missed most answers, sorry for that.

Just updated the composer.json to include 4.1 as i did not encounter any poroblems and all test passed.
So just grab anything from 1.3.3+ and you should good to go.

@teddycarebears: If you still have problems, drop me a PM and i will see what i can do for you to make it work.

@sprim As long as they do not change the api it should work for the latest versions, if it does not, just open an issue on github.

@puberia: If you still have problems, please let me know via mail or pm. That way i can help faster than writing in the forum.

Last edited by Chumper (2013-12-13 04:05:32)

Offline

#14 2013-12-15 19:18:31

Delavor
Artisan
Registered: 2013-11-24
Posts: 15

Re: [Package] Datatables for Laravel 4

Hello,

I'm trying to use this but i can't.

My JS config

var UsersTable = function () {

    return {

        //main function to initiate the module
        init: function () {
            
            if (!jQuery().dataTable) {
                return;
            }

            // begin first table
            $('#users').dataTable({
                "sDom" : "<'row'<'col-md-6 col-sm-12'l><'col-md-6 col-sm-12'f>r>t<'row'<'col-md-5 col-sm-12'i><'col-md-7 col-sm-12'p>>", //default layout without horizontal scroll(remove this setting to enable horizontal scroll for the table)
                "aLengthMenu": [
                    [10, 25, 50, 100],
                    [10, 25, 50, 100] // change per page values here
                ],
                "bProcessing": true,
                "bServerSide": true,
                "sAjaxSource": "http://hereismydomain.com/dashboard/ajax/getusers",
                // set the initial value
                "iDisplayLength": 10,
                "sPaginationType": "bootstrap_full_number",
                "oLanguage": {
                    "sProcessing": '<i class="fa fa-coffee"></i>&nbsp;Proszę czekać...',
                    "sLengthMenu": "_MENU_ rekordów",
                    "oPaginate": {
                        "sPrevious": "Poprzednia",
                        "sNext": "Następna"
                    }
                },
                "aoColumnDefs": [{
                        'bSortable': false,
                        'aTargets': [0]
                    }
                ]
            });

            jQuery('#users_wrapper .dataTables_filter input').addClass("form-control input-medium"); // modify table search input
            jQuery('#users_wrapper .dataTables_length select').addClass("form-control input-small"); // modify table per page dropdown

            // handle record edit/remove
            $('body').on('click', '#users_wrapper .btn-editable', function() {
                alert('Edit record with id:' + $(this).attr("data-id"));
            });

            $('body').on('click', '#users_wrapper .btn-removable', function() {
                alert('Remove record with id:' + $(this).attr("data-id"));
            });

        }

    };

}();
<?php

class AjaxController extends BaseController {

	public function ajaxGetUsers()
	{
		$users = User::select(array('id', 'username', 'activated'));
		return Datatables::of($users)->make();
	}
}

And always there is stiil "Please wait"

					<table class="table table-striped table-bordered table-hover" id="users">
						<thead>
							<tr>
								<th>
									ID
								</th>
								<th>
									Nazwa użytkownika
								</th>
								<th>
									Punkty
								</th>
								<th>
									Status
								</th>
								<th>
									Kowalski Opcje!
								</th>
							</tr>
						</thead>
						<tbody>
						</tbody>
					</table>

Offline

#15 2013-12-15 19:44:11

Chumper
Artisan
Registered: 2013-07-23
Posts: 48

Re: [Package] Datatables for Laravel 4

The first thing i see is that you do not tell Datatable which colum you want to show.
You have five columns in the table, so you need to tell Datatable to show five columns, like this:

public function ajaxGetUsers()
	{
		$users = User::select(array('id', 'username', 'activated'));
		return Datatables::of($users)->
                           ->showColumns('id', 'username', 'points', 'status', 'foobar')
                           ->make();
	}

You should then be able to show your table in the frontend.

You can use the simple helper for the frontend if you want, you can also take a look at the sample view.
If that does not work, please provide me with the json response you get when you call the ajax source.

Last edited by Chumper (2013-12-15 19:44:36)

Offline

#16 2013-12-15 19:59:07

Delavor
Artisan
Registered: 2013-11-24
Posts: 15

Re: [Package] Datatables for Laravel 4

Hi again,

Okey, now i have error

Call to undefined method Bllim\Datatables\Datatables::showColumns()

I have added plugin to providers and alieses.

Offline

#17 2013-12-15 20:29:35

Delavor
Artisan
Registered: 2013-11-24
Posts: 15

Re: [Package] Datatables for Laravel 4

Okey, i checked manual and change code to

<?php

class AjaxController extends BaseController {

	public function ajaxGetUsers()
	{
		$users = User::select(array('id', 'username', 'email', 'activated'));

		return Datatables::of($users)
			->add_column('operations', 'Test')
			->edit_column('activated', 'Status')
			->remove_column('id')
			->make();
	}
}

Here is a json output

{"sEcho":0,"iTotalRecords":"2","iTotalDisplayRecords":"2","aaData":[["Delavor","delavor@dfdfd.com","Status","Test"],["Szybki","fdfdff@gmail.com","Status","Test"]],"sColumns":["id","username","email","activated","operations"]}

But still there is only "Please wait". I don't know what is wrong now.

Last edited by Delavor (2013-12-15 20:30:54)

Offline

#18 2013-12-15 20:56:43

Chumper
Artisan
Registered: 2013-07-23
Posts: 48

Re: [Package] Datatables for Laravel 4

I see, you are using the wrong package.

You are using https://github.com/bllim/laravel4-datatables-package

My package is https://github.com/Chumper/Datatable

I cant help you with that package, may you can open a issue on github?

Offline

#19 2013-12-15 21:49:03

Delavor
Artisan
Registered: 2013-11-24
Posts: 15

Re: [Package] Datatables for Laravel 4

Oh! tongue Okey, i try use your package tongue

Offline

#20 2013-12-15 22:46:25

Delavor
Artisan
Registered: 2013-11-24
Posts: 15

Re: [Package] Datatables for Laravel 4

Okey, i changed to your package and modify code and still does not work sad

<?php

class AjaxController extends BaseController {

	public function ajaxGetUsers()
	{
		$users = User::select(array('id', 'username', 'email', 'activated'));

		return Datatable::from($users)
			->showColumns('id', 'username', 'email', 'activated')
			->addColumn('options', 'Test')
			->make();
	}
}

Returns:

{"aaData":[["1","Delavor","delavor@dsdsd.com",true,"Test"],["2","Szybki","ssdsdba@gmail.com",true,"Test"]],"sEcho":0,"iTotalRecords":"2","iTotalDisplayRecords":"2"}

Last edited by Delavor (2013-12-15 23:13:51)

Offline

#21 2013-12-15 23:15:33

Chumper
Artisan
Registered: 2013-07-23
Posts: 48

Re: [Package] Datatables for Laravel 4

The response looks correct, do you have any javascript errors?

Offline

#22 2013-12-15 23:26:23

Delavor
Artisan
Registered: 2013-11-24
Posts: 15

Re: [Package] Datatables for Laravel 4

Warning: event.returnValue is deprecated. Please use the standard event.preventDefault() instead. jquery-1.10.2.js:5374
Error: Denying load of chrome-extension://mcobjbefejmnadahpjbfibgkgchhmjke/jquery.min.map. Resources must be listed in the web_accessible_resources manifest key in order to be loaded by pages outside the extension.

I don't know that is important but javascript generating this link

http://mydomain.com/dashboard/ajax/getusers?sEcho=1&iColumns=5&sColumns=&iDisplayStart=0&iDisplayLength=10&mDataProp_0=0&mDataProp_1=1&mDataProp_2=2&mDataProp_3=3&mDataProp_4=4&sSearch=&bRegex=false&sSearch_0=&bRegex_0=false&bSearchable_0=true&sSearch_1=&bRegex_1=false&bSearchable_1=true&sSearch_2=&bRegex_2=false&bSearchable_2=true&sSearch_3=&bRegex_3=false&bSearchable_3=true&sSearch_4=&bRegex_4=false&bSearchable_4=true&iSortCol_0=0&sSortDir_0=asc&iSortingCols=1&bSortable_0=false&bSortable_1=true&bSortable_2=true&bSortable_3=true&bSortable_4=true&_=1387146339347

I'm using already this layout http://themeforest.net/item/metronic-re … te/4021469

Last edited by Delavor (2013-12-15 23:47:53)

Offline

#23 2013-12-15 23:59:10

Chumper
Artisan
Registered: 2013-07-23
Posts: 48

Re: [Package] Datatables for Laravel 4

Can you strip down the datatable javascript call to a minimum? it looks like it is a javascript problem on your site.

Offline

#24 2013-12-16 00:21:27

Delavor
Artisan
Registered: 2013-11-24
Posts: 15

Re: [Package] Datatables for Laravel 4

I'm using themeforest layout so i don't know how to do this tongue

Here is demo .php file for ajax table from my layout (Themeforest Metronic 1.5.5)

<?php
  /* 
   * Paging
   */
  $iTotalRecords = 200;
  $iDisplayLength = intval($_GET['iDisplayLength']);
  $iDisplayLength = $iDisplayLength < 0 ? $iTotalRecords : $iDisplayLength; 
  $iDisplayStart = intval($_GET['iDisplayStart']);
  $sEcho = intval($_GET['sEcho']);
  
  $records = array();
  $records["aaData"] = array(); 

  $end = $iDisplayStart + $iDisplayLength;

  $status_list = array(
    array("success" => "Approved"),
    array("warning" => "Pending"),
    array("danger" => "Rejected")
  );

  for($i = $iDisplayStart; $i < $end; $i++) {
    $status = $status_list[rand(0, 2)];
    $id = ($i + 1);
    $records["aaData"][] = array(
      $id,
      'metrouser',
      ($id * 12295),
      '<span class="label label-sm label-'.(key($status)).'">'.(current($status)).'</span>',
      '<a href="javascript:;" data-id="'.$id.'" class="btn btn-xs blue btn-editable"><i class="fa fa-pencil"></i> Edit</a> <a href="javascript:;" data-id="'.$id.'" class="btn btn-xs red btn-removable"><i class="fa fa-times"></i> Delete</a> <div class="btn-group"><button class="btn default btn-xs dropdown-toggle" type="button" data-toggle="dropdown">More <i class="fa fa-angle-down"></i></button><ul class="dropdown-menu pull-right" role="menu"><li><a href="#">Action</a></li><li><a href="#">Another action</a></li></ul></div>',
    );
  }

  $records["sEcho"] = $sEcho;
  $records["iTotalRecords"] = $iTotalRecords;
  $records["iTotalDisplayRecords"] = $iTotalRecords;
  
  echo json_encode($records);
?>

Last edited by Delavor (2013-12-16 00:25:30)

Offline

#25 2013-12-16 00:28:12

Chumper
Artisan
Registered: 2013-07-23
Posts: 48

Re: [Package] Datatables for Laravel 4

I meant the javascript call in the html view, can you just do this:
$('#users').dataTable({
"bServerSide": true,
                "sAjaxSource": "http://hereismydomain.com/dashboard/ajax/getusers",
                // set the initial value
                "iDisplayLength": 10,
});

Offline

Board footer

Powered by FluxBB

');