2016-02-22 108 views
0

需要幫助將這些json結果在kendoGrid中按天排序。我需要一種方法將加號轉換爲.5或其他符號。我想我必須遍歷每一個,如果我找到加號,然後將值更改爲數字5,然後在顯示之前將新值刪除?使用自定義值在劍道網格中進行排序

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

你有什麼已經嘗試過? –

+0

什麼都沒有,我想與社區分享,因爲有人已經擊敗了這個問題。我打算嘗試循環遍歷每個對象,用.5替換+號然後以這種方式顯示。思考? – KendoUiNewbie

回答

0

下面是代碼,我得到了工作

<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>