bgultekin / laravel4-datatables-package

Server-side handler of DataTables Jquery Plugin for Laravel 4
267 stars 108 forks source link

Undefined offset: 4 #119

Closed aaronflorey closed 10 years ago

aaronflorey commented 10 years ago

I'm getting undefined offset when using search. I'm using 1.4 of your package and 1.11 of DT. The issue is that, i'm adding a 'operartions' column and datatables is sending that column back, but because i'm not selecting it your package doesn't know what column 3 is.

            $products = Product::select(array('id', 'code2', 'name', 'quantity', 'shopifyId', 'code3', 'ebayId'));

            return Datatables::of($products)
                             ->add_column('operations', $buttons)
                             ->remove_column('code3')
                             ->remove_column('shopifyId')
                             ->remove_column('ebayId')
                             ->make();

My bandaid solution was to remove the 4th column from the input array like so.

            $request = Input::all();
            unset($request['columns'][4]);
            Input::merge($request);

Not sure if there's a proper way of doing this?

thanks

ktunkiewicz commented 10 years ago

Please paste here the var_dump of Input::get() so I can reproduce that.

aaronflorey commented 10 years ago

I added another column, so now the offset is 4 (I updated my post to show this).

Array
(
    [draw] => 2
    [columns] => Array
        (
            [0] => Array
                (
                    [data] => 0
                    [name] => 
                    [searchable] => true
                    [orderable] => true
                    [search] => Array
                        (
                            [value] => 
                            [regex] => false
                        )

                )

            [1] => Array
                (
                    [data] => 1
                    [name] => 
                    [searchable] => true
                    [orderable] => true
                    [search] => Array
                        (
                            [value] => 
                            [regex] => false
                        )

                )

            [2] => Array
                (
                    [data] => 2
                    [name] => 
                    [searchable] => true
                    [orderable] => true
                    [search] => Array
                        (
                            [value] => 
                            [regex] => false
                        )

                )

            [3] => Array
                (
                    [data] => 3
                    [name] => 
                    [searchable] => true
                    [orderable] => true
                    [search] => Array
                        (
                            [value] => 
                            [regex] => false
                        )

                )

            [4] => Array
                (
                    [data] => 4
                    [name] => 
                    [searchable] => true
                    [orderable] => true
                    [search] => Array
                        (
                            [value] => 
                            [regex] => false
                        )

                )

        )

    [order] => Array
        (
            [0] => Array
                (
                    [column] => 0
                    [dir] => desc
                )

        )

    [start] => 0
    [length] => 10
    [search] => Array
        (
            [value] => tes
            [regex] => false
        )

    [_] => 1406261721534
)
ktunkiewicz commented 10 years ago

I took a while for my to understand exactly what you are trying to do, but think I understand it now. Your DT is requesting some action on column that does not exists in SQL query. I this case it is obvious for me that this column should be set orderable=false in columns definition for datatables.js.

This is because this package works in this way:

  1. make count of rows for the query without filtering
  2. add filtering
  3. make count of rows for the query with filtering
  4. add/remove/edit columns
  5. output result

So if you try to filter on added column the filtering function cannot see it.

To avoid this kind of situation I will add a code that is checking for existence of column before trying to filter it. It is possible that this kind of checking existed previosly, but when I rewrote all this functions to use new format I didn't notice that someone can try to filter on non existing column.

ktunkiewicz commented 10 years ago

I commited new Datatables.php, please check if the issues are solved.