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

Template cells don't sort

1 Answer 233 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Yaroslav
Top achievements
Rank 1
Yaroslav asked on 08 Jan 2013, 03:14 PM
Hello.
I have some problem with Grid.
I have some grid with colums: Id, firstName, lastName and template YearOfBirth...
Here is my viewModels

Person = kendo.data.Model.define({
        id: "",
        firstName: "",
        lastName: "",
        age: "",
        myFunc: function () {
            return 2013 - this.age;
        }
    });
 
    viewModel = kendo.observable({
        personArr: [],
        loadDataFromController: function () {
            personArr = new kendo.data.ObservableArray([]);
            var vm = this;
            $.ajax({
                dataType: 'json',
                url: '/Home/GetPersons',
                success: function (json) {
                    vm.setData(json);
                }
            });
        },
        setData: function (json) {
            for (var i = 0; i < json.length; i++) {
                var temp = new Person();
                temp.id = json[i].Id;
                temp.firstName = json[i].FirstName;
                temp.lastName = json[i].LastName;
                temp.age = json[i].Age;
                this.get("personArr").push(temp);
            }
        }
    });
    viewModel.loadDataFromController();
    kendo.bind($('#PersonGrid'), viewModel);


and here is my KendoGrid initialization

<div id="PersonGrid">
    @(Html.Kendo().Grid(Model)
    .Name("Grid")
    .Columns(columns =>
        {
            columns.Bound("id").Groupable(false);
            columns.Bound("firstName");
            columns.Bound("lastName");
            columns.Template(@<text></text>).ClientTemplate("#=myFunc()#").Width(120).Title("Year of birth");
        })
    .Sortable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .ServerOperation(false)
     )
    .Pageable(page => page.PageSizes(true))
    .Scrollable()
    .Filterable()
    .HtmlAttributes(new Dictionary<string, object>
        {
            {"data-bind", "source: personArr" }
        })
    )
</div>

Everything is fine, but I can't sort my last column with template myFunc
How I can fix this ?


1 Answer, 1 is accepted

Sort by
0
Accepted
Dimo
Telerik team
answered on 08 Jan 2013, 05:06 PM
Hello Yaroslav,

Data operations (such as sorting) are executed over the data in the datasource before this data is passed to the Grid. Client templates are executed after data operations when the data is rendered by the browser. I hope you realise that you cannot sort by something which has not been calculated yet.

Your options are:

+ Have the year of birth in the datasource. This is a lot better than having the age, because the age changes every year, while the year of birth remains constant.

+ Use custom binding for the Grid and sort the data manually.
http://docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/custom-binding

Regards,
Dimo
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Yaroslav
Top achievements
Rank 1
Answers by
Dimo
Telerik team
Share this question
or