Diego_DX
Diego_DX

Reputation: 1071

foreign key in a grid using kendoui

so in my view I have this

<div id="grid"></div>


<script>
    $(document).ready(function () {
        dataSource = new kendo.data.DataSource({
            transport: {
                read: {
                    url: '@Url.Action("GetCityList", "City")',
                    dataType: "json",
                    type: "GET",
                    contentType: "application/json; charset=utf-8"
                },
                create: {
                url: '@Url.Action("create", "City")',
                type: "Post"
            },
                parameterMap: function (data, operation) {
                    if (operation != "read") {
                        var result = {};

                        for (var i = 0; i < data.models.length; i++) {
                            var ciudad = data.models[i];

                            for (var member in ciudad) {
                                result["ciudades[" + i + "]." + member] =                        ciudad[member];
                            }
                        }
                        return result;
                    }
                }


            },
            batch: true,
            pageSize: 30,
            schema: {
                model: {
                    id: "id",
                    fields: {
                        id: { editable: false, nullable: true },
                        descripcion: { validation: { required: true} },
                        DepartamentName: { field: "DepartamentName" },
                    }
                }
            }
        });

        $("#grid").kendoGrid({
            dataSource: dataSource,
            navigatable: true,
            pageable: true,
            height: 400,
            toolbar: ["create", "save", "cancel"],
            columns: [
                "descripcion",
            { field: "DepartamentName", title: "Name Departament", editor: categoryDropDownEditor, width: 150 }, 
                { command: "destroy", title: "&nbsp;", width: 110}],
            editable: true
        });
    });

</script>

<script>

    function categoryDropDownEditor(container, options) {

        $('<input data-text-field="name" data-value-field="name"  data-bind="value:' + options.field + '"/>')
                        .appendTo(container)
                        .kendoDropDownList({
                            autoBind: false,
                            dataSource: {
                                type: "json",
                                transport: {
                                    read: {
                                        url: '@Url.Action("GetDepartament", "city")
                                            }
                                }
                            }
                        });
    }
            </script>

it show me ok but my problem is that when It hit to my action create it give me the name of the departament not the id

and when i change my function this part to this

 $('<input data-text-field="name" data-value-field="name"  data-bind="value:' + options.field + '"/>')

 $('<input data-text-field="name" data-value-field="id"  data-bind="value:' + options.field + '"/>')

with it show me fine in my action I get the id but in my grid when I choose in my dropdown a departament in the grid it show me the id (so is no the behavior I want)

What can I do to get this to work? I guess it is in the bind part..

Upvotes: 0

Views: 1508

Answers (1)

Alexander Skogorev
Alexander Skogorev

Reputation: 59

I faced the same problem some time ago. And my solution is a bit tricky. The idea is to use "id|name" format. I set template for the field:

template: '#= (fldName).substring((fldName).indexOf("|", 0) + 1) #';

And I set editor:

function(container, options) {
        $('<input id="autoComplete" data-bind="value:' + options.field + '"/>')
            .appendTo(container)                                    
            .kendoComboBox({
                dataTextField: "name",
                dataValueField: "id",
                minLength: 1,
                template: '#= (id).substring((id).indexOf("|", 0) + 1) #',
                filter: "contains",
                delay: 0,
                dataSource: new kendo.data.DataSource({
                    type: "json",
                    transport: {
                        read: {
                            url: "services/SearchContent",
                            type: "GET"
                        },
                        parameterMap: function (opt, operation) {
                            var val;
                            if(opt.filter) {
                                val = opt.filter.filters[0].value;
                            } else {                                                        
                                val = options.model[$("#autoComplete").data("bind").substring(6)];
                                val = (val).substring((val).indexOf("|", 0) + 1);
                            }
                            if(val == "") val = "a";
                            return {
                                searchString: val,
                                resultsCount: 10,
                                date: (new Date()).valueOf()
                            };
                        }
                    },
                    schema: {
                        data: "results",
                        type: "json"
                    },
                    serverFiltering: true
                })
            });

My service also dynamically filters results by user input, but I think you can get my idea. And of course to make this work, your grid service shell return "id|name" format for this column instead of just "name".

Upvotes: 1

Related Questions