1
votes

The pagination documentation has good info on how to add sorting to the paginated links for your data:

<?php echo $orders->appends(array('sort' => 'name', 'sort_dir'=>'asc'))->links();

to generate links like this:

http://example.com/something?page=2&sort=name&sort_dir=asc

So now you have a table with headers and values and a series of paginated links you can click. Let's say I wanted to change the headers to be clickable links that allowed you to sort by that header. How would I generate that URL?

For example, lets say our current URL is what I said above:

http://example.com/something?page=2&sort=name&sort_dir=asc

And I wanted to click the "NAME" table header in order to change the sorting direction. The URL I would want is:

http://example.com/something?page=2&sort=name&sort_dir=desc

I have a controller called : AdminController.php this is the method used:

    public function getAdmins() {

 // CACHE SORTING INPUTS
$allowed = array('first_name', 'last_name', 'email', 'activated', 'crated_at'); // add allowable columns to search on
$sort = in_array(Input::get('sort'), $allowed) ? Input::get('sort') : 'first_name'; // if user type in the url a column that doesnt exist app will default to first_name
$order = Input::get('order') === 'asc' ? 'asc' : 'desc'; // default desc
$action = null;
// select all admins Group = 1
$admins = DB::table('users')
     ->join('users_roles', 'users.id', '=', 'users_roles.user_id')
     ->where('users_roles.role_id', '=' ,0)
    ->orderBy($sort, $order)
    ->paginate($this->perpage);

// check for actions
if (!is_null(Input::get('action'))) 
    {

            $action = Input::get('action');

            if ($action == "add")
            {
                $this->layout->content = View::make('admin.admins-add');
            }

    }
    else
    {

        // get current counter admin counts
        $counter = $admins->getFrom();
        View::share('counter', $counter);
        View::share('sort', $sort);
        View::share('order', $order);

        // share admin with template
        View::share('admins', $admins);

        $this->layout->content = View::make('admin.admins');
    }
    }

1). how could I display the columns headers links in Laravel inside my view page?

Thanks

1
use javascript ofcourse.... this has nothing to do with server side.itachi
I need to generate header links , when you click on first name , it will change sort = first_name and so onuser3150060
i got what you are trying to do here.... but this is client side coding. not server side. what did you do in client side to achieve it?itachi
in the client side I have a table with headers first name last name email etc, I want to make those links , where if you click on first_name it will take you to : example.com/… and Last Name header will go to : example.com/… , and for the sort_dir to change if it was asc it will change to descuser3150060
Of course you can do it server side... check the answerJarek Tkaczyk

1 Answers

0
votes

This is how you get the params:

$params = Request::except(['sort','sort_dir']);
$sort_dir = (Request::get('sort_dir') == 'asc') ? 'desc' : 'asc';
$sort = 'whataverYourColumnIs';

$attributes = array_merge(['sort' => $sort, 'sort_dir' => $sort_dir], $params);

// and for the link for example:
link_to_action('AdminsController@getIndex', 'columnName', $attributes);