function GridDS(options) { this.data = []; this.enablePaging = false; this.pageIndex = 0; this.pageSize = 10; this.sortBy = null; this.sortDirection = null; this.count = GridDS__count; this.numberOfPages = GridDS__numberOfPages; this.pageNumber = GridDS__pageNumber; this.gotoPage = GridDS__gotoPage; this.nextPage = GridDS__nextPage; this.previousPage = GridDS__previousPage; this.hasNextPage = GridDS__hasNextPage; this.hasPreviousPage = GridDS__hasPreviousPage; this.getData = GridDS__getData; this.sort = GridDS__sort; this.pages = GridDS__pages; if (options) { if (options.data) { this.data = options.data; } if (options.enablePaging) { this.enablePaging = options.enablePaging; } if (options.pageSize) { this.pageSize = options.pageSize; } } } function GridDS__count () { return this.data.length; } function GridDS__numberOfPages() { if (this.enablePaging == false) { return 1; } var p1 = this.data.length / this.pageSize; var p2 = Math.floor(p1); if (p1 == p2) { return p2; } return p2 + 1; } function GridDS__pageNumber() { if (this.enablePaging == false) { return 1; } return this.pageIndex + 1; } function GridDS__gotoPage(pageNumber) { if (this.enablePaging == false) { return; } if (pageNumber > 0 && pageNumber <= this.numberOfPages()) { this.pageIndex = pageNumber - 1; } } function GridDS__nextPage() { if (this.enablePaging == false) { return; } if (this.pageIndex < (this.numberOfPages() - 1)) { this.pageIndex++; } } function GridDS__previousPage() { if (this.enablePaging == false) { return; } if (this.pageIndex > 0) { this.pageIndex--; } } function GridDS__hasNextPage () { if (this.enablePaging == false) { return false; } if (this.pageIndex < (this.numberOfPages() - 1)) { return true; } return false; } function GridDS__hasPreviousPage () { if (this.enablePaging == false) { return false; } if (this.pageIndex > 0) { return true; } return false; } function GridDS__getData () { if (this.enablePaging == false) { return this.data; } var startIndex = this.pageIndex * this.pageSize; var endIndex = ((this.pageIndex + 1) * this.pageSize) - 1; if (endIndex > (this.data.length - 1)) { endIndex = this.data.length - 1; } var list = []; for (var i = startIndex; i <= endIndex; i++) { list.push(this.data[i]); } var pgs = []; for (var i = 1; i <= this.numberOfPages() ; i++) { pgs.push({ pageNumber: i }); } return list; } function GridDS__sort (comparer) { this.data.sort(comparer); this.pageIndex = 0; } function GridDS__pages() { if (this.enablePaging == false) { return { pageNumber: 1 }; } var list = []; for (var i = 1; i <= this.numberOfPages() ; i++) { list.push({ pageNumber: i }); } return list; }