Chumper / Datatable

This is a laravel 4 package for the server and client side of datatables at http://datatables.net/
https://github.com/Chumper/Datatable
388 stars 154 forks source link

CollectionEngine does not support sorting by multiple columns at the same time #332

Open timgws opened 9 years ago

timgws commented 9 years ago

CollectionEngine currently does not support ordering by multiple columns.

I tried extending the Collection class inside Datatable, and adding a new function (sortByMulti), but never got too far into adding this functionality.


    public function sortByMutli($callback, $fields, $options = SORT_REGULAR, $descending = false)
    {
        $required_keys = array_keys($fields);

        $items = [];

        // First we will loop through the items and get the comparator from a callback
        // function which we were given. Then, we will sort the returned values and
        // and grab the corresponding values for the sorted keys from this array.
        foreach ($this->items as $key => $value) {
            print_r($key);
            $results[$key] = $callback($value, $key);

            foreach($required_keys as $_key) {
                print_r($this->items);
                if (!isset($items[$_key]))
                    $items[$_key] = [];

                $items[$_key][$key] = $results[$key][$_key];
            }
        }

        // Then we need to go through and finally sort all the items by the
        // requested direction.
        $column_sort = ['$results'];
        foreach($fields as $key => $item) {
            print_r($key);
            $column_sort[] = "\$$key";
            $column_sort[] = $this->sortByOrder($direction);
        }

        call_user_func_array('array_multisort', $column_sort);

    }