【问题标题】:Kendo Grid numbers truncate to 2 decimal places. How to make it respect what the user entered?剑道网格数字截断到小数点后 2 位。如何让它尊重用户输入的内容?
【发布时间】:2015-09-26 15:20:42
【问题描述】:

在此Kendo Grid demo 中,如果您编辑“库存商品”下的数字并添加多个小数(尝试 2.203848),它会将其截断为 2.20。看起来这是默认行为。

而且我知道我们可以用{0:n4} 指定十进制格式,例如。

但如果小数位数未知或可以变化怎么办?有没有办法让网格使用用户输入的确切数字?

【问题讨论】:

    标签: javascript kendo-ui telerik kendo-grid


    【解决方案1】:

    要在网格中进行这项工作,您需要使用自定义编辑器。将小数位数设置为足够高的数字,并确保您的字段格式有足够的位置。这个很好的答案here 稍作调整可以解决您的问题。

    function numberEditor(container, options) {
    $('<input name="' + options.field + '"/>')
            .appendTo(container)
            .kendoNumericTextBox({
                decimals: 8,
                step    : 0.01
            });
    }
    
    $("#grid").kendoGrid({
        dataSource: dataSource,
        navigatable: true,
        pageable: true,
        height: 550,
        toolbar: ["create", "save", "cancel"],
        columns: [
            "ProductName",
            { field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: 120 },
            { field: "UnitsInStock", title: "Units In Stock", format: "{0:0.#########}", width: 120, editor: numberEditor },
            { field: "Discontinued", width: 120 },
            { command: "destroy", title: "&nbsp;", width: 150 }],
        editable: true
    });
    

    });

    【讨论】:

    • 做到了。谢谢!
    【解决方案2】:

    kendo ui 网格支持在直接调用 kendo.format() 时可以使用的任何格式。相关文档可在 here 获得。

    你要找的格式是

    {0:$###,###,###,###,###,###,###,###.##############} 
    

    您需要将其填充到您想要支持的小数位数。

    【讨论】:

    • 我试过了,但没有运气。这是我创建的 JSFiddle。该格式应用于 UnitsInStock 列 ({0:###.######}),但输入 20.238384 会返回 20.24。 jsfiddle.net/dmathisen/ot6tw2nu
    • 这里只讨论数字显示的格式。当用户用户输入一个值时,它不会影响行为。
    猜你喜欢
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多