【问题标题】:Format number values in kendo grid fields to decimal problems将剑道网格字段中的数值格式化为十进制问题
【发布时间】:2017-04-06 08:34:29
【问题描述】:

我需要在我的 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"
    }
]

编辑:我找到了问题的解决方案。我需要在 {field...} 参数中添加模板:'#= parseFloat(namefield).toFixed(2)#'。

【问题讨论】:

    标签: kendo-ui kendo-grid


    【解决方案1】:

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

    一个有效的 json 应该是这样的:

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

    【讨论】:

    • 我在 javascript =) 中使用了 parseFloat,我现在真的不知道如何更改我的 json 输出。需要更深入地了解它。
    • 您在服务器端使用哪种技术/语言?
    猜你喜欢
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多