sorting in kendo grid using custom values

50 Views Asked by At

need help with sorting these json results by days inside kendoGrid. I need a way to turn the plus sign into .5 or something along those lines. I suppose I have to loop through each one, if i find the plus sign then change the value to number.5 and then drop the new value back in before displaying?

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

There are 1 best solutions below

0
On

Here is code I got working

<body>
    <div id="grid">

    </div>
    <div>
        <script>
            $(document).ready(function () {
                //JSON data 
                var people = [
                { firstName: "Hasibul", lastName: "Haque", email: "[email protected]", rank:"2" }
                , { firstName: "Jane", lastName: "Smith", email: "[email protected]", rank: "3+" }
                , { firstName: "Jason", lastName: "Doe", email: "[email protected]", rank: "1" }
                , { firstName: "John", lastName: "doe", email: "[email protected]", rank: "3+" }
                , { firstName: "Joan", lastName: "doe", email: "[email protected]", rank: "5" }
                , { firstName: "Jack", lastName: "doe", email: "[email protected]", rank: "3" }
                 , { firstName: "Jacob", lastName: "doe", email: "[email protected]", rank: "3-" }
                  , { firstName: "Joe", lastName: "doe", email: "[email protected]", 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>