2017-04-06 73 views
0

我需要在我kendoGrid十進制(2)格式顯示數字。但目前似乎沒有任何工作。所以我需要幫助理解原因。我搜索論壇,但簡單的 「設置類型字段,以數​​量和使用的格式:‘{0:N2}’」不工作。我從一個字段值有很多小數的JSON中獲取我的數據。所以我希望它們在我的kendoGrid中只有2位小數。格式數值爲十進制的問題

這裏是我的代碼和我的失敗嘗試得到它的工作

var gridStavke = $("#gridItems").kendoGrid({ 
     scrollable: true, 
     sortable: true, 
     resizable: true, 
     dataSource: { 
      schema: { 
      model: { 

       fields: { 

        PDV25 : { type : "number" }, 
        PDV3 : { type : "number" }, 
        PDV28 : { type : "number" } 
       } 
      } 
     } 
    }, 
     height: $(document).height() - 190,   
     columns: [ 
      {field: "product", title: "Product"}, 
      {field: "price", title: "Price"}, 
      {field: "base", title: "Base"}, 
      {field: "count", title: "Count",footerTemplate:"total"}, 
      {field: "PDV25", title: "PDV 25%",format:"{0:n2}"}, 
      {field:"PDV3",title:"PDV 3%",format:"{0:n2}", editor:function(container,options){$('<input />').appendTo(container).kendoNumericTextBox({format:"n",decimals:2});}}, 
      {field: "PDV28", title: "PDV 28%", editor:numberEditor} 

     ] 

    }).data("kendoGrid"); 
    function numberEditor(container, options) { 
$('<input name="' + options.field + '"/>') 
     .appendTo(container) 
     .kendoNumericTextBox({ 
      format : "{0:n2}", 
      decimals: 2, 
      step : 0.01 
     }); 
} 

JSON

"data":[ 
    { 
     "product":"Med", 
     "price":"40.3", 
     "count":"1", 
     "base":"238.42628640776698", 
     "PDV25":"8.059999999999999", 
     "PDV28":"54.68750000000001", 
     "PDV3":"0.32621359223300966" 
    }, 
    { 
     "product":"Sir", 
     "price":"250", 
     "count":"1", 
     "base":"238.42628640776698", 
     "PDV25":"8.059999999999999", 
     "PDV28":"54.68750000000001", 
     "PDV3":"0.32621359223300966" 
    }, 
    { 
     "product":"Mlijeko", 
     "price":"11.2", 
     "count":"2", 
     "base":"238.42628640776698", 
     "PDV25":"8.059999999999999", 
     "PDV28":"54.68750000000001", 
     "PDV3":"0.32621359223300966" 
    } 
] 

編輯:我發現這個問題的解決方案。我需要添加模板:在{場...}參數 '#= parseFloat(名稱字段).toFixed(2)#'。

回答

0

你是因爲你沒有設置正確的JSON數據源獲取這個問題。在雙引號的Json值(「」)代表一個字符串不是一個數字, 號碼應該是不帶引號(「」)。這就是爲什麼數字格式不適用於它。所以改變你的JSON,它會工作。

一個有效的JSON應該是這樣的:

"data":[ 
     { 
      "product":"Med", 
      "price":40.3, 
      "count":"1", 
      "base":238.42628640776698, 
      "PDV25":8.059999999999999, 
      "PDV28":54.68750000000001, 
      "PDV3":0.32621359223300966 
     },.. 
] 
+0

我用parseFloat在JavaScript =),我真的不知道如何改變我的JSON輸出現在。需要更多地瞭解它。 – DraganK

+0

哪種技術/你正在使用服務器端語言? –