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

cannot create new row in grid whose datasource has filter on foreign key

3 Answers 193 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Remco
Top achievements
Rank 1
Remco asked on 17 Sep 2012, 01:49 PM
I have a grid whose datasource has a filter set on a foreign key. It's a remote datasource to a SharePoint 2010 list odata service. The create command in the grid's toolbar does not do anything. As soon as I remove the filter from the datasource, the create command works again. How can I get the create command to work with the filter set? I assume I need to find a way to set the foreign key on the newly created object to the same value as the filter and only then will the new object show up in the grid.

3 Answers, 1 is accepted

Sort by
0
Remco
Top achievements
Rank 1
answered on 17 Sep 2012, 03:39 PM
right, managed to find a workaround. Instead of setting a filter on the datasource like so

filter: { field: "RiskMatrixId", operator: "eq", value: e.model.Id },

I now set the filter in the url of the read operation directly and then set the foreign key value in the edit event handler on the grid, like so
var crudServiceBaseUrl = "/_vti_bin/listdata.svc/Categories",
    dataSource = new kendo.data.DataSource({
        type: "odata",
        //filter: { field: "RiskMatrixId", operator: "eq", value: e.model.Id },
        transport: {
            read: {
                url: kendo.format("{0}?$filter=RiskMatrixId eq {1}", crudServiceBaseUrl, e.model.Id),
                dataType: "json"
            },
            update: {
                // type must be POST instead of PUT when using MERGE.
                //type: "POST",
                url: function (options) {
                    return options.__metadata.uri;
                },
                dataType: "json",
                beforeSend: function (jqXHR, settings) {
                    var options = JSON.parse(settings.data);
                    jqXHR.setRequestHeader("If-Match", options.__metadata.etag);
                    // Using MERGE so that the entire entity doesn't need to be sent over the wire.
                    //jqXHR.setRequestHeader("X-HTTP-Method", 'MERGE');
                }
            },
            destroy: {
                url: function (options) {
                    return options.__metadata.uri;
                },
                dataType: "json"//,
                //beforeSend: function (jqXHR, settings) {
                //    var options = JSON.parse(settings.data);
                //    jqXHR.setRequestHeader("If-Match", options.__metadata.etag);                       
                //}
            },
            create: {
                url: crudServiceBaseUrl,
                dataType: "json",
            }
        },
        schema: {
            model: {
                id: "Id",
                fields: {
                    Id: { type: "number", editable: false, nullable: true },
                    Title: { type: "string", validation: { required: true } },
                    Created: { type: "date" },
                    Modified: { type: "date" },
                    CreatedById: { type: "number" },
                    ModifiedById: { type: "number" },
                    RiskMatrixId: { type: "number", validation: { required: true } },
                    Description: { type: "string" }
                }
            }
        },
        //batch: true,
        error: onError
    });
 
e.container.find("#CategoriesGrid").kendoGrid({
    dataSource: dataSource,
    scrollable: false,
    toolbar: ["create"],
    columns: [
        "Title",
        "Description",
        { command: ["edit", "destroy"] }
    ],
    editable: {
        mode: "inline"
    },
    edit: function (args) {
        if (args.model && args.model.RiskMatrixId == 0) {
            args.model.RiskMatrixId = e.model.Id;
        }
    }
});

Now the create command in the grid toolbar works again
0
David
Top achievements
Rank 1
answered on 18 Sep 2012, 06:35 PM
Another method is to set your filter to: 
logic: "or"

and add a filter clause with:
value: null

I assume the reason that this works is because an added record will have a 
null id and obviously that is not equal to the value you are filtering on.

0
Remco
Top achievements
Rank 1
answered on 19 Sep 2012, 09:21 AM
Thanks for your reply David. That works as well. I had to add a filter for 0, not null:
filter: {
    logic: "or",
    filters: [
        { field: "RiskMatrixId", operator: "eq", value: e.model.Id },
        { field: "RiskMatrixId", operator: "eq", value: 0 }
    ]
}

The only thing is that with serverFiltering: true this filter also gets sent to the server, when it is only required client-side to get the create command in the grid toolbar to work.
Tags
Grid
Asked by
Remco
Top achievements
Rank 1
Answers by
Remco
Top achievements
Rank 1
David
Top achievements
Rank 1
Share this question
or