具有自定义参数的JqGrid自定义格式化程序 [英] JqGrid custom formatter with custom parameter

查看:78
本文介绍了具有自定义参数的JqGrid自定义格式化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对自定义格式化程序有疑问.

I have a question about custom formatters.

我试图实现的是一个currencyFormatter,仅用于服务器发送的带有Locale的金额,而未定义或不支持locale时退回到英式英语. 像这样:

What I try to achieve is a currencyFormatter just for the amount with Locale sent by the server, when locale is not define or supported fall back to British English. Something like this:

function currencyFmatter(cellvalue, options, rowdata) {
    return new Intl.NumberFormat([locale, "en-GB"], {minimumFractionDigits: 2, maximumFractionDigits: 2}).format(cellvalue);
}

我的问题是如何将我的变量语言环境传递给格式化程序,我很确定这必须是一种方法,但是现在我看不到它.

My problem is how to pass my variable locale to the formatter, I’m pretty sure it has to be a way to do it but right now I don’t see it.

谢谢

推荐答案

这是一个有趣的问题!有很多方法可以实现您的需求.

It's an interesting question! There are many ways to implement your requirements.

1)您可以使用指定数据区域设置的其他信息来扩展从服务器返回的输入数据.例如,您可以返回"de-DE:10.000,04"而不是"10.000,04",它表示用德语语言环境格式化的1000.04(其中,将用作小数点分隔符,而.用作千位分隔符).它允许您使用cellvalue.split(":")来获取具有数字区域设置和数字本身的数组["de-DE", "10.000,04"]

1) you can extend your input data returned from the server with additional information which specify the locale of data. For example you can returns "de-DE:10.000,04" instead of "10.000,04" which represent 1000.04 formatted in German locale (where , will be used as the decimal separator and . used as the thousands separator). It allows you to use cellvalue.split(":") to get array ["de-DE", "10.000,04"] with the locale of the number and the number itself

function currencyFmatter(cellvalue, options, rowdata) {
    var data;
    if (typeof cellvalue === "string") {
        data = cellvalue.cellvalue.split(":");
        if (data.length === 2) {
            return new Intl.NumberFormat([data[0], "en-GB"], {
                           minimumFractionDigits: 2,
                           maximumFractionDigits: 2
                       }).format(data[1]);
        }
    }
    return cellvalue;
}

或者,您可以将有关数字语言环境的信息放在输入数据的单独字段(例如numLocale)中,并使用类似rowdata.numLocale的方式(或rowdata[12]取决于JSON数据的输入格式)访问语言环境.

Alternatively you can place the information about locale of the number in separate field (for example numLocale) of the input data and use something like rowdata.numLocale (or rowdata[12] depend on the input format of the JSON data) to access the locale.

2)可能是从服务器返回的所有数据都将采用相同的格式.在这种情况下,最好不要在所有数字前添加相同的前缀"de-DE:".例如,您可以做的是使用其他字段扩展从服务器返回的数据.例如,您可以使用

2) It could be that all the data returned from the server will be in the same format. In the case it would be not the best way to prepend all numbers with the same prefix "de-DE:". What you can do for example is to extend the data returned from the server with additional field. For example you can use

{
    "total": "12",
    "page": "1",
    "records": "12345",
    "localOfNumbers": "de-DE",
    "rows" : [
        ...
    ]
}

您可以访问beforeProcessing回调内部的自定义localOfNumbers字段.这是非常实用的回调.它允许您预处理从服务器返回的数据之前将由jqGrid处理.我建议您阅读答案答案 ).让我们来为目标选择选项gridLocale.然后,您可以执行以下操作:

You can access the custom localOfNumbers field inside of beforeProcessing callback. It's very practical callback. It allows you to pre-process the data returned from the server before the data will be processed by jqGrid. I recommend you to read the answer and this one for more code example. What you can do for example is to save localOfNumbers value in some new option of jqGrid (see the answer for more details). Let us you want to have an option gridLocale for the goal. Then you can do something like the following:

beforeProcessing: function (data) {
    if (typeof data.localOfNumbers === "string") {
        $(this).jqGrid("setGridParam", {gridLocale: data.localOfNumbers});
    }
}

要使用新的gridLocale选项,您可以使用

To access the new gridLocale option you can use

function currencyFmatter(cellvalue, options, rowdata) {
    var locale = $(this).jqGrid("getGridParam", "gridLocale"); // this.p.gridLocale
}    

3)您可以考虑将有关语言环境的信息另存为列属性,而不是使用一种常见的gridLocale网格选项.为此,您可以在colModel中定义列,如下所示:

3) You can consider to save the information about the locale as column property instead of usage one common gridLocale grid option. To do this you can define the column in colModel like below

{ name: 'col2', width: 200, formatoptions: { colLocale: "en-IN" },
    formatter: function (cellvalue, options, rowdata) {
        // options.colModel.formatoptions.colLocale get you the value
    }

也可以在beforeProcessing内部设置formatoptions.colLocale的属性.您可以使用

One can set the property of formatoptions.colLocale inside of beforeProcessing too. You can use

{
    "total": "12",
    "page": "1",
    "records": "12345",
    "localOfColumns": {
        "col2": "de-DE",
        "col5": "en-IN"
    },
    "rows" : [
        ...
    ]
}

beforeProcessing: function (data) {
    if ($.isPlainObject(data.localOfColumns)) {
        if (typeof data.localOfColumns.col2 === "string") {
            $(this).jqGrid("setColProp", "col2", {
                formatoptions: { colLocale: data.localOfColumns.col2 }
            });
        }
        if (typeof data.localOfColumns.col5 === "string") {
            $(this).jqGrid("setColProp", "col5", {
                formatoptions: { colLocale: data.localOfColumns.col5 }
            });
        }
    }
}

我敢肯定,您可以提出更多实现需求的方法.

I'm sure that one can suggest even more ways to implement your requirements.

这篇关于具有自定义参数的JqGrid自定义格式化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆