Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine two fields for the dataTextField of Kendo UI autocomplete?

I want to combine two fields for the dataTextField property of the Kendo autocomplete.

My datasouce has a FirstName field and a LastName field.

schema: {
            data: "d",
            model: {
                id: "PersonId",
                fields: {
                    PersonId: {
                        type: "number",
                        editable: false // this field is not editable
                    },
                    FirstName: {
                        type: "text",
                        validation: { // validation rules
                            required: true // the field is required
                        }
                    },
                    LastName: {
                        type: "text",
                        validation: { // validation rules
                            required: true // the field is required
                        }
                    }
                }
            }
        }

Is there a way I can configure the autocomplete to display FirstName + LastName?

Maybe I have to do something with the datasource instead, and if that is the case, can someone provide a simple example?

Thank you!

like image 403
Naner Avatar asked Feb 27 '13 16:02

Naner


2 Answers

You should use template:

for example:

template:"The name is : #= FirstName # #=LastName #"
like image 177
Petur Subev Avatar answered Nov 05 '22 18:11

Petur Subev


Using templates is a valid solution. However, if you want to always have a composite or calculated field accessible on your model you can define a parse function in the schema definition.

schema: {
    data: "d",
    model: {
        id: "PersonId",
        fields: {
            PersonId: {
                type: "number",
                editable: false // this field is not editable
            },
            FirstName: {
                type: "string",
                validation: { // validation rules
                    required: true // the field is required
                }
            },
            LastName: {
                type: "string",
                validation: { // validation rules
                    required: true // the field is required
                }
            },
            Name: {
                type: "string"
                // add any other requirements for your model here
            }
        }
    },
    parse: function (response) {
        var values = response.d,
            n = values.length,
            i = 0,
            value;
        for (; i < n; i++) {
            value = values[i];
            value.Name = kendo.format("{0} {1}",
                value.FirstName,
                value.LastName);
        }

        return response;
    }
}

The parse function pre-processes the server response before it is used, so you can modify existing fields or attach new ones. You can then reference the field(s) directly in any controls that use the Model.

like image 7
Kevin Babcock Avatar answered Nov 05 '22 18:11

Kevin Babcock