Created
March 30, 2014 19:09
-
-
Save leniency/9878023 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;// | |
// Grid extension. | |
// Adds default grid state perservation via localStorage. | |
// The grid container can specify a storage location by | |
// setting the data-store attribute to either: | |
// - local [default] | |
// - session | |
// - false Disables storage for this grid. | |
// | |
(function ($, kendo) { | |
var | |
_init = kendo.ui.Grid.fn.init; | |
var handleStorage = function (element, options) { | |
var store = $(element).data('store'); | |
if (store !== false) { | |
// Check if the grid has an overriding storage location. | |
// Default is local, but can override to session. | |
var storage = $.localStorage; | |
if (typeof store === 'string' && | |
(store === 'local' || store === 'session')) { | |
storage = $[store + "Storage"]; | |
} | |
var dataBound = options.dataBound; | |
var key = window.location.pathname + "/#" + element.id; | |
// Override the default grid.dataBound event. | |
options.dataBound = function (e) { | |
var dataSource = e.sender.dataSource; | |
// Grab the grid state. | |
var state = { | |
page: dataSource.page(), | |
pageSize: dataSource.pageSize(), | |
sort: dataSource.sort(), | |
group: dataSource.group(), | |
filter: dataSource.filter() | |
}; | |
// Store to local. | |
storage.set(key, state); | |
// Call original databound, if any. | |
if (dataBound != null) { | |
dataBound(e); | |
} | |
}; | |
// | |
// Set initial dataSource options, if any state exists. | |
var state = storage.get(key); | |
// Set initial state if an stored instance exists. | |
if (state) { | |
$.extend(options.dataSource, state); | |
} | |
} | |
}; | |
var Grid = kendo.ui.Grid.extend({ | |
init: function (element, options) { | |
// Handle storage options. | |
handleStorage.call(this, element, options); | |
// Call the base constructor. | |
var r = _init.call(this, element, options); | |
return r; | |
} | |
}); | |
kendo.ui.plugin(Grid); | |
}(window.kendo.jQuery, window.kendo)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment