Deep linking into DataTables

A question that comes up relatively frequently in the forums is "how do I deep link into a DataTable?" This is usually asked in the context of SEO where you want to ensure that all of the data in the table is indexable, but it can also be appropriate in applications where you want to show a table with a specific search term pre-set.

Typically the answer to such a question is to use the initialisation parameters to set whatever option is required from the search string, but it is such a common thing to want to do, it is sensible that we generalise it. To that end, here I present a short script that can be used to extract parameters from a search string and use them to populate a DataTable.

Let's see it in action first - follow the links below to observe the behaviour on the demonstration table:

Usage

The key element of using this script on your own pages is the $.fn.dataTable.ext.deepLink function. This takes a single argument: an array of the options that you wish to allow the search parameter to specify, and it will return an object that contains these initialisation options which can be passed straight to the DataTable, as is the case above.

A white-list approach is used to limit which options can be used for security. For example, it is unlikely you would wish to allow a user to modify the ajax, serverSide or scrollY parameters. If a parameter is given which isn't in the white-list, it will be ignored. Having said that, if you are using your application in an environment where you do wish any and all parameters to be used, you can specify all as the only argument to the function, which will allow all parameters.

A simple example: allow the search.search parameter to be set from the search string:

Extending defaults

Since the deepLink() function simply returns an object, you can provide defaults which can optionally be overridden by the search string, or vice-versa have an optional parameter that will override any parameter from the search string. This can be done using the jQuery.extend() function with the returned object:

How does it work?

Now that we know how to use it, if you are interested in how it works, read on. Its actually very simple! Let's first step back to my standard response in the forum to say that the search string should be used to set the parameter value. If we assume that the entire search string is to be used to search the table (e.g. /table?mySearchTerm) we might use:

The downside to this is that it wouldn't be possible to set search.search to true since it would be cast to the boolean value by the above code. I feel this is a relatively unlikely situation to run into, but if you do, the above piece of code would need to be modified to suit your needs.

Writing nested values

You might have noticed in the above examples that search.search is used a lot, since that is one of parameters that I expect to be used most commonly with this script. However, the Javascript object version of it is actually { search: { search: ... } } - i.e. a nested object. To convert from the string notation, we can use a function which DataTables has built-in to build nested objects based on a dot delimited string: $.fn.dataTable.ext.internal._fnSetObjectDataFn (this is used for columns.data). This function isn't a documented API, but the next major version will see it promoted to be so (albeit with a more accessible name) as it is exceptionally useful for plug-ins and add-on scripts!