jsGrid编辑支付金额

jsGrid edit payment amount

我正在使用 jsGrid JQuery 插件来显示和编辑付款。虽然这是一个很好的选择,但我无法使用内置字段来编辑 'Payment Amount'。如果我使用 'number' 类型,我无法在小数位后输入数字。如果我使用 'text' 类型,我可以输入非数字值。

到目前为止,这是我的代码

    $("#jsGrid").jsGrid({
        width: "100%",
        height: "auto",

        editing: true,
        paging: true,
        autoload: true,

        loadIndication: true,
        loadIndicationDelay: 500,
        loadMessage: "Please, wait...",
        loadShading: true,

        controller: {
            loadData: function () {
                return $.ajax({
                    type: "GET",
                    url: AJAX_URL_GET_CALCULATED_AFFECTED_PAYMENTS,
                    data: { 'startDate': startDate, 'endDate': endDate },
                    dataType: "json",
                    cache: false,
                });
            },
        },
        fields: [
            { name: "PaymentDate", title: "Payment Date", type: "text", width: 150, editing: false, },
            { name: "PaymentEndDate", title: "Payment End Date", type: "text", width: 150, editing: false, },
            { name: "PaymentStatusDisplay", title: "Payment Status", type: "text", width: 150, align: "center", editing: false, },
            {
                name: "PaymentAmount", title: "Payment Amount", type: "number", width: 100, align: "center", editing: true,
                itemTemplate: function(value) {
                    return "$" + value;
                },
            },
            { type: "control", deleteButton: false, editButton: true, editButtonTooltip: "Edit Amount", }
        ]
    });

有没有人使用过这个插件得到了一个例子或设法创建自定义字段类型或使用字段模板编辑 jsGrid 中的字段以仅允许货币值(数字、逗号、小数)

提前致谢。

您可以像下面这样定义您的自定义字段:

function MoneyField(config) {
    jsGrid.NumberField.call(this, config);
}

MoneyField.prototype = new jsGrid.NumberField({

    itemTemplate: function(value) {
        return "$" + value.toFixed(2);
    }

    filterValue: function() {
        return parseFloat(this.filterControl.val() || 0);
    },

    insertValue: function() {
        return parseFloat(this.insertControl.val() || 0);
    },

    editValue: function() {
        return parseFloat(this.editControl.val() || 0);
    }

});

jsGrid.fields.money = MoneyField;

现在您可以在字段指定中使用它了type="money"

以下是设置显示值格式的方法。这将以 $1,000.00 的格式打印数字。

声明函数:

function formatNumberForDisplay(numberToFormat) {
    var formatter = new Intl.NumberFormat('en-US', {
        style: 'currency',
        currency: 'USD',
        digits: 2,
      });

      return formatter.format(numberToFormat);
}

然后在 itemTemplate 方法中使用它:

    itemTemplate: function(value) {
        return formatNumberForDisplay(value) ;
    },