|
Line 0
Link Here
|
| 0 |
- |
1 |
/*! Deep linking options parsing support for DataTables |
|
|
2 |
* 2017 SpryMedia Ltd - datatables.net/license |
| 3 |
*/ |
| 4 |
|
| 5 |
/** |
| 6 |
* @summary LengthLinks |
| 7 |
* @description Deep linking options parsing support for DataTables |
| 8 |
* @version 1.0.0 |
| 9 |
* @file dataTables.deepLink.js |
| 10 |
* @author SpryMedia Ltd (www.sprymedia.co.uk) |
| 11 |
* @copyright Copyright 2017 SpryMedia Ltd. |
| 12 |
* |
| 13 |
* License MIT - http://datatables.net/license/mit |
| 14 |
* |
| 15 |
* This feature plug-in for DataTables provides a function which will |
| 16 |
* take DataTables options from the browser's URL search string and |
| 17 |
* return an object that can be used to construct a DataTable. This |
| 18 |
* allows deep linking to be easily implemented with DataTables - for |
| 19 |
* example a URL might be `myTable?displayStart=10` which will |
| 20 |
* automatically cause the second page of the DataTable to be displayed. |
| 21 |
* |
| 22 |
* This plug-in works on a whitelist basis - you must specify which |
| 23 |
* [initialisation parameters](//datatables.net/reference/option) you |
| 24 |
* want the URL search string to specify. Any parameter given in the |
| 25 |
* URL which is not listed will be ignored (e.g. you are unlikely to |
| 26 |
* want to let the URL search string specify the `ajax` option). |
| 27 |
* |
| 28 |
* This specification is done by passing an array of property names |
| 29 |
* to the `$.fn.dataTable.ext.deepLink` function. If you do which to |
| 30 |
* allow _every_ parameter (I wouldn't recommend it) you can use `all` |
| 31 |
* instead of an array. |
| 32 |
* |
| 33 |
* @example |
| 34 |
* // Allow a display start point and search string to be specified |
| 35 |
* $('#myTable').DataTable( |
| 36 |
* $.fn.dataTable.ext.deepLink( [ 'displayStart', 'search.search' ] ) |
| 37 |
* ); |
| 38 |
* |
| 39 |
* @example |
| 40 |
* // As above, but with a default search |
| 41 |
* var options = $.fn.dataTable.ext.deepLink(['displayStart', 'search.search']); |
| 42 |
* |
| 43 |
* $('#myTable').DataTable( |
| 44 |
* $.extend( true, { |
| 45 |
* search: { search: 'Initial search value' } |
| 46 |
* }, options ) |
| 47 |
* ); |
| 48 |
*/ |
| 49 |
(function(window, document, $, undefined) { |
| 50 |
// Use DataTables' object builder so strings can be used to represent |
| 51 |
// nested objects |
| 52 |
var setBuilder = $.fn.dataTable.ext.internal._fnSetObjectDataFn; |
| 53 |
|
| 54 |
$.fn.dataTable.ext.deepLink = function(whitelist) { |
| 55 |
var search = location.search.replace(/^\?/, '').split('&'); |
| 56 |
var out = {}; |
| 57 |
|
| 58 |
for (var i = 0, ien = search.length; i < ien; i++) { |
| 59 |
var pair = search[i].split('='); |
| 60 |
var key = decodeURIComponent(pair[0]); |
| 61 |
var value = decodeURIComponent(pair[1]); |
| 62 |
|
| 63 |
// "Casting" |
| 64 |
if (value === 'true') { |
| 65 |
value = true; |
| 66 |
} else if (value === 'false') { |
| 67 |
value = false; |
| 68 |
} else if (!value.match(/[^\d]/) && key !== 'search.search') { |
| 69 |
// don't convert if searching or it'll break the search |
| 70 |
value = value * 1; |
| 71 |
} else if (value.indexOf('{') === 0 || value.indexOf('[') === 0) { |
| 72 |
// Try to JSON parse for arrays and obejcts |
| 73 |
try { |
| 74 |
value = $.parseJSON(value); |
| 75 |
} catch (e) {console.log("ERR");console.log(e);} |
| 76 |
} |
| 77 |
|
| 78 |
if (whitelist === 'all' || $.inArray(key, whitelist) !== -1) { |
| 79 |
var setter = setBuilder(key); |
| 80 |
setter(out, value); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
console.log(out); |
| 85 |
return out; |
| 86 |
}; |
| 87 |
})(window, document, jQuery); |