This is a migrated thread and some comments may be shown as answers.

Grid Datasource, explicit calling .read() containing parameters

0 Answers 233 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeroen
Top achievements
Rank 1
Jeroen asked on 12 Sep 2012, 07:20 AM
I try to make a single page web app. It will contain some grids and forms. Some of the form controls and grids are based on the same datasource. Sometimes I need to get a specific item from the datasource (identified by a parameter) or sometimes I just need every available item from the datasource.

I've solved this problem by adding a parameterMap to the transport of the datasource. Now I'm able to exactly retrieve all items from the datasource, how and when I want. For code example, see rootDatasource below.

In the widgets which I'm using, like Grid, I want to call the read method of the datasource with a parameter specified to retrieve all items from the back-end which match the query 'active' == true. See code example:

// create the grind containing the update elements
$("#root_grid").kendoGrid({
    dataSource: rootDatasource.read({ params: { active: true } }),
    columns: [{
            field: "id",
            title: "id",
            width: 80,
        }, {
            field: "name",
            title: "naam",
        }, {
            field: "value",
            title: "weging",
            width: 100,
        }, {
            field: "active",
            title: "actief",
            width: 100,
        }],
    toolbar: [{
            name: "create",
            text: "voeg nieuwe rij toe",
        }, {
            name: "save",
            text: "opslaan",
        }, {
            name : "cancel",
            text: "annuleren",
        }],
    sortable: true,
    pageable: {
        refresh: true,
        pageSizes: true,
        messages: i18n.grid.message
    },
    sortable: {
        mode: "multiple",
        allowUnsort: true
    },
    editable: true,
});

And now my problem, this won' work!! I'm really confused, if I just add a reference to the datasource object without directly specifying the .read() method, it just works. (Retrieving all items, including 'active' == false though.) In another form I want to specify some other parameters, so just adding the parameters to my (PHP) back-end just doesn't work.

I've been reading the docs en forums quite extensively to find an answer, didn't find it and I find myself quite stuck right now. Any help will be much welcome. 

var rootDatasource = new kendo.data.DataSource({
 
    schema: {
        data: "data",
        model: Root,
        total: function(response) {
            return $(response.data).length;
        }
    },
     
    pageSize: 10,
 
    transport: {
         
        read: "/vta/root/all.json",
         
        update: {
            url: "/vta/root/update.json",
            type: "POST"
        },
         
        create: {
            url: "/vta/root/create.json",
            type: "PUT"
        },
 
        parameterMap: function (data, operation) {
 
            if (operation == "read") {
 
                if (data.params != undefined) {
 
                    var params = new Array();
                     
                    for (var n in data.params) {
                        params.push(n +"="+ data.params[n]);
                    }
                     
                    return params.join('&');
                     
                }
                 
            }
             
        }
                                         
    },
     
    error: function(e) {
        alert(e.responseText);
    },
     
    change: function(data) {
//      console.log(data);
    },
     
    sort: {
        field: 'id',
        dir: 'asc'
    }
             
});

var Root = kendo.data.Model.define({


    id: "id",
    
    fields: {
        
        id: {
            editable: false
        },
        
        name: { 
            type: "string",
            validation: { 
                required: true
            } 
        },
        
        'value': {
            type: "number",  
        },
        
        active: { 
            type: "boolean",
        }
    },




});

No answers yet. Maybe you can help?

Tags
Grid
Asked by
Jeroen
Top achievements
Rank 1
Share this question
or