Search code examples
javascriptjquerykendo-uikendo-gridkendo-dropdown

Kendo DropDownList in Grid not binding until after selection


I have an editable Kendo Grid, with a field that is a dropdownlist. I have a U_UserId and U_Name field that need to be used in that column. The name obviously displays, while the ID should be what is used for binding. I've removed my datasource URLs in the example below, but the DropDownList data displays just fine, with a list of Names and ID values.

I've been looking at this for a while so it's possible I'm missing something obvious. I'm having an identical problem to this question (the dropdown doesn't bind to the user displayed in that row until I click on the dropdown to expand it), but I think my model and fields are correct so I'm not sure why I still can't get the dropdown to bind correctly.

Here is my JS for both the grid and the editor:

$(document).ready(function () {
    var grid = $("#grid").kendoGrid({
        dataSource: {
            type: "json-ajax",
            transport: {
                read: {
                    url: myUrl,
                    type: "GET"
                }
            },
            batch: true,
            pageSize: 20,
            schema: {
                data: "Data",
                total: "Total",
                model: {
                    id: "OrderId",
                    fields: {
                        O_OrderNumber: {
                            editable: false
                        },      
                        O_Date: {
                            editable: false, type: "date"
                        },
                        O_InvoiceNumber: {
                            editable: false
                        },
                        O_Status: {
                            editable: false
                        },
                        O_DueDate: {
                            editable: false, type: "date"
                        },
                        U_UserId: {
                            editable: true
                        },
                        U_Name: {
                            editable: false
                        },
                        O_VendorId: {
                            editable: false
                        },
                        O_TrackingNumber: {
                            editable: false
                        }  
                    }
                }
            },
        },
        scrollable: false,
        editable: true,
        pageable: true,
        columns: [
                    {
                        field: "O_OrderNumber",
                        title: "Order #",
                        hidden: false
                    },
                    {
                        field: "O_Date",
                        title: "Pull Date",
                        hidden: false,
                        type: "date",
                        format: "{0:MM/dd/yyyy}"
                    },
                    {
                        field: "O_InvoiceNumber",
                        title: "Invoice #",
                        hidden: false
                    },
                    {
                        field: "O_Status",
                        title: "Status",
                        hidden: false
                    },
                    {
                        field: "O_DueDate",
                        title: "Due Date",
                        hidden: false,
                        type: "date", 
                        format: "{0:MM/dd/yyyy}"
                    },
                    {
                        field: "U_UserId",
                        title: "Owner",
                        hidden: false,
                        width: 130,
                        editor: ownerDropDownEditor, 
                        template: "#=U_Name#"
                    },
                    {
                        field: "O_VendorId",
                        title: "Vendor",
                        hidden: false
                    },
                    {
                        field: "O_TrackingNumber",
                        title: "Tracking #",
                        hidden: false
                    }
        ]
    }).data("kendoGrid");
});

function ownerDropDownEditor(container, options) {
    $('<input required name="' + options.field + '" />')
        .appendTo(container)
        .kendoDropDownList({
            autoBind: false,
            dataTextField: "Name",
            dataValueField: "UserId",
            dataSource: {
                type: "json",
                transport: {
                    read: {
                        url: myOtherUrl,
                        type: "GET"
                    }
                }
            }
        });
} 

Edit: Out of curiosity, I tried changing my dropdownlist to have both the DataTextField and the DataValueField be UserId, and the selection was made right away, but with the ID (int) value being shown instead of the Name (string).


Solution

  • So investigating a little further into my edit above, I found this Telerik post, which made it sound like the dropdown actually binds by an object, and not by dropdown value. There is a data-value-primitive attribute that can be added so that it binds by value. I updated my editor, and it is now behaving as expected:

    function ownerDropDownEditor(container, options) {
        $('<input required name="' + options.field + '" data-value-primitive="true" />')
            .appendTo(container)
            .kendoDropDownList({
                autoBind: false,
                dataTextField: "Name",
                dataValueField: "UserId",
                dataSource: {
                    type: "json",
                    transport: {
                        read: {
                            url: myOtherUrl,
                            type: "GET"
                        }
                    }
                }
            });
    }