user7094
user7094

Reputation:

How do I do table sorting using CodeIgniter?

I've been developing a site over the past few weeks using CodeIgniter as the framework. I've been thinking of the best way to accomplish something, which in a lot of other frameworks in other languages is relatively simple: sortable tables. CodeIgniter switches off query strings by default, because your URLs contain method parameters. So a URL might look like:

/controller/method/param1/param2

You might think that you could just add in sortBy and sortOrder as two additional parameters to the controller method. I don't particularly want to do that, mainly because I want to have a re-usable controller. When you use query string parameters, PHP can easily tell you whether there is a parameter called sortBy. However, when you're using URL based parameters, it will vary with each controller.

I was wondering what my options were. As far as I can see they are something like:

I just can't quite believe such a simple task would present such a problem! Am I missing something? Does anyone have any recommendations?

While I love jQuery, and I'm already using it on the site, so TableSorter is a good option. However, I would like to do server-side sorting as there are some pages with potentially large numbers of results, including pagination.

Upvotes: 8

Views: 15175

Answers (3)

JayTee
JayTee

Reputation: 2806

I ran into this with a fairly complex table. The hard part was that the table could grow/shrink depending on certain variables!! Big pain :(

Here's how I handled it..

Adjusted system/application/config/config.php to allow the comma character in the URI:

$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-,';

Adjust my controller with a sorting function:

function sorter() {
  //get the sort params
  $sort = explode(",",$this->uri->segment(3)); //the 3rd segment is the column/order
  //pass the params to the model
  $data = $this->model_name->get_the_data($sort[0],$sort[1]);
  $this->_show($data);
}
function _show($data) {
  //all the code for displaying your table
}

I've oversimplified, but you get the idea. The purpose is to have a url like this:

/controller/sorter/columnname,sortorder

The sorter function calls another internal function to deal with the display/template/view logic - it's job is to deal with the sorting call and get the appropriate data from the model.

Of course, this could be reduced to just your current function:

function showGrid() {
  $sort = $this->uri->segment(3);
  if ($sort) {
    //get the data sorted
  } else {
    //get the data the default way
  }
  //rest of your view logic
}

That way, you don't even need a separate function - and can use the third segment to define your sorting.

Upvotes: 1

Nathan Long
Nathan Long

Reputation: 126042

If you're OK with sorting on the client side, the Tablesorter plugin for jQuery is pretty nice.

Upvotes: 2

Mark Biek
Mark Biek

Reputation: 150829

I recently added this Table sorter (which uses Prototype) to a bunch of my pages. It's fast and pretty easy to implement.

Upvotes: 0

Related Questions