Search code examples
javascriptknockout.jskogrid

kogrid with server-side paging+filtering: self.sortedData is not a function


I'm experiencing an issue with knockout and kogrid. After several tests, I managed to reproduce it in this fiddle:

http://jsfiddle.net/MVuUp/

In short, I have a sample VM (ItemsViewModel) which should provide data to a kogrid, with server-implemented filtering and paging. The vm exposes a gridOptions object with all the data required by kogrid, and a refresh function called to get data from the server. In the fiddle I replaced the ajax call to get real data with some dummy inline data.

Now, when loading the page I constantly get this error in the kogrid source:

Error: TypeError: self.sortedData is not a function Source File: http://ericmbarnard.github.com/KoGrid/lib/KoGrid.debug.js Line: 1473

If instead I just use the observable array named dummyData with just a div for the grid, as in the basic kogrid sample (https://github.com/Knockout-Contrib/KoGrid), this error does not come out; so I'm supposing there is something wrong in my usage of the library.

Here is the relevant part of my VM:

function ItemsViewModel() {
    var self = this;
    self.dummyArray = ko.observableArray([{
        name: "Goofy",
        age: 27
    }, {
        name: "Mickey",
        age: 33
    }]);
    self.items = ko.observableArray([]);
    self.filterOptions = {
        filterText: ko.observable(""),
        useExternalFilter: true
    };
    self.pagingOptions = {
        pageSizes: ko.observableArray([5, 10]),
        pageSize: ko.observable(20),
        totalServerItems: ko.observable(0),
        currentPage: ko.observable(1)
    };
    self.gridOptions = {
        data: self.items,
        enablePaging: true,
        multiSelect: false,
        filterOptions: self.filterOptions,
        pagingOptions: self.pagingOptions,
        columnDefs: [{
            field: "id",
            displayName: "ID"
        }, {
            field: "title",
            displayName: "Title"
        }]
    };
    self.refresh();
}

This function is used to get data (at present I left filtering out):

ItemsViewModel.prototype.refresh = function () {
var params = {
    page: this.pagingOptions.currentPage(),
    pageSize: this.pagingOptions.pageSize(),
    sortBy: "datemodified",
    isSortDesc: true
};
// dummy data (2 pages x 5)
var data = {
    page: 1,
    totalPages: 2,
    totalRecords: 10,
    records: []
};
for (var i = 0; i < 10; i++) {
    data.records.push({
        id: i,
        title: "title " + i
    });
}
ko.utils.arrayPushAll(this.items, data.records);
this.pagingOptions.totalServerItems(data.totalRecords);
}

My HTML is just a div with data-bind set to

koGrid: { data: gridOptions }

Solution

  • In your html, change

    <div class="datagrid" data-bind="koGrid: { data: gridOptions }"></div>

    to

    <div class="datagrid" data-bind="koGrid: gridOptions "></div>