将kendo网格字段中的数字值格式化为十进制问题

时间:2017-04-06 08:34:29

标签: kendo-ui kendo-grid

我需要以十进制(2)格式在我的kendoGrid中显示数字。但到目前为止似乎没有任何工作。所以我需要帮助理解为什么。我搜索论坛但很简单 '将类型字段设置为数字并使用格式:" {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"
    }
]
编辑:我找到了问题的解决方案。我需要添加模板:&#39;#= parseFloat(namefield).toFixed(2)#&#39;在{field ...}参数中。

1 个答案:

答案 0 :(得分:0)

您遇到此问题是因为您没有设置正确的json数据源。在双引号中的Json值(&#34;&#34;)表示字符串而不是数字, 数字应该没有引号(&#34;&#34;)。这就是为什么数字格式不适用于它的原因。所以改变你的json它会起作用。

有效的json应该是这样的:

 "data":[
        {
            "product":"Med",
            "price":40.3,
            "count":"1",
            "base":238.42628640776698,
            "PDV25":8.059999999999999,
            "PDV28":54.68750000000001,
            "PDV3":0.32621359223300966
        },..
]
相关问题