(function($){ // Private function function getColumnsForScaffolding(data) { if ((typeof data.length !== 'number') || data.length === 0) { return []; } var columns = []; for (var propertyName in data[0]) { columns.push({ headerText: propertyName, rowText: propertyName }); } return columns; } ko.simpleGrid = { // Defines a view model class you can use to populate a grid viewModel: function (configuration) { this.data = configuration.data; this.currentPageIndex = ko.observable(0); this.pageSize = configuration.pageSize || 5; this.pagePos = configuration.pagePos || 'top'; // If you don't specify columns configuration, we'll use scaffolding this.columns = configuration.columns || getColumnsForScaffolding(ko.utils.unwrapObservable(this.data)); this.itemsOnCurrentPage = ko.computed(function () { var startIndex = this.pageSize * this.currentPageIndex(); return this.data().slice(startIndex, startIndex + this.pageSize); }, this); var self = this; this.data.subscribe(function(val){ /*if(self.currentPageIndex() >= val.length){ if(val.length -1 >= 0){ self.currentPageIndex(val.length -1); } }*/ self.currentPageIndex(0); }); this.maxPageIndex = ko.computed(function () { return Math.ceil(ko.utils.unwrapObservable(this.data).length / this.pageSize) - 1; }, this); this.gridPrev = function(data){ if(data.currentPageIndex()) data.currentPageIndex(data.currentPageIndex()-1) } this.gridNext = function(data){ if(data.currentPageIndex() < data.maxPageIndex()) data.currentPageIndex(data.currentPageIndex()+1) } } }; // Templates used to render the grid var templateEngine = new ko.nativeTemplateEngine(); templateEngine.addTemplate = function(templateName, templateMarkup) { $('body').append("