使用自定义值在剑道网格中排序



需要帮助在 kendoGrid 中按天数对这些 json 结果进行排序。 我需要一种方法将加号转换为 .5 或类似的东西。 我想我必须遍历每一个,如果我找到加号,然后将值更改为 number.5,然后在显示之前将新值放回去?

{ name: Alex, days: "2" },
{ name: Jason, days: "1" },
{ name: Fred, days: "2+" },
{ name: Jane, days: "3" },
{ name: John, days: "3+" }

这是我开始工作的代码

<body>
    <div id="grid">
    </div>
    <div>
        <script>
            $(document).ready(function () {
                //JSON data 
                var people = [
                { firstName: "Hasibul", lastName: "Haque", email: "hasibul2363@gmail.com", rank:"2" }
                , { firstName: "Jane", lastName: "Smith", email: "jane.smith@kendoui.com", rank: "3+" }
                , { firstName: "Jason", lastName: "Doe", email: "hasibul2363@gmail.com", rank: "1" }
                , { firstName: "John", lastName: "doe", email: "hasibul2363@gmail.com", rank: "3+" }
                , { firstName: "Joan", lastName: "doe", email: "hasibul2363@gmail.com", rank: "5" }
                , { firstName: "Jack", lastName: "doe", email: "hasibul2363@gmail.com", rank: "3" }
                 , { firstName: "Jacob", lastName: "doe", email: "hasibul2363@gmail.com", rank: "3-" }
                  , { firstName: "Joe", lastName: "doe", email: "hasibul2363@gmail.com", rank: "3-" }
                ];

                $('#grid').kendoGrid({
                    dataSource: {
                        type: "json",
                        data: people,
                        pageSize: 15,
                        sort: ({ field: "rank" })
                    },
                    sortable: true,
                    columns:[{
                        field: "rank",
                        sortable: {
                            compare: function (a, b, asc) {
                                var s1 = a.rank;
                                var s2 = b.rank;
                                var n1, n2;
                                var sg1, sg2;
                                var plus = s1.indexOf('+');
                                var minus = s1.indexOf('-');
                                if(plus >= 0){
                                    n1 = parseInt(s1.substr(0, plus));
                                    sg1 = 1;
                                }
                                else if(minus >= 0){
                                    n1 = parseInt(s1.substr(0, minus));
                                    sg1 = -1;
                                }
                                else{
                                    n1 = parseInt(s1);
                                    sg1 = 0;
                                }
                                plus = s2.indexOf('+');
                                minus = s2.indexOf('-');
                                if (plus >= 0) {
                                    n2 = parseInt(s2.substr(0, plus));
                                    sg2 = 1;
                                }
                                else if (minus >= 0) {
                                    n2 = parseInt(s2.substr(0, minus));
                                    sg2 = -1;
                                }
                                else {
                                    n2 = parseInt(s2);
                                    sg2 = 0;
                                }
                                if (n1 == n2) {
                                    return sg2 - sg1;
                                } else {
                                    return n2 - n1;
                                }
                            }
                        }
                    }]                                            
                    ,
                    pageable: {
                        buttonCount: 1
                    },
                    schema: {
                        data: "people"
                    }
                    //binding JSON data with grid
                });


            });
        </script>

相关内容

  • 没有找到相关文章

最新更新