javascript - 在编辑行的 jqGrid 列中添加 mask

标签 javascript jquery jqgrid

当我使用jqGrid添加或编辑记录时,我需要为某些列创建电话掩码(我的jqGrid版本是4.5.4)。

在我的代码下面:

this.montarGRID = function (p_gridName, p_dados, p_header, p_descriptor, p_contentName, p_primaryKey, p_filtroGrid) {
jQuery("#" + p_gridName).jqGrid( {
    data : p_dados, 
    datatype : "local", 
    sortable : true, 
    colNames : p_header, 
    colModel : p_descriptor,
...

该网格是动态生成的。我传递了一个包含 colModel 内容的 json。

[
{"formatter":"integer","index":"id","hidden":true,"sortable":true,"sorttype":"integer","width":75,"align":"center","name":"id"},
{"formatter":"telefone","index":"TELCONTATO01","search":true,"hidden":false,"sorttype":"text","sortable":true,"width":0,"align":"right","name":"TELCONTATO01","editoptions":{"text":true,"required":false,"dataInit":"function (elem) {  return mostra_telefone(elem); }"},"editrules":{"text":true,"required":false,"dataInit":"function (elem) {  return mostra_telefone(elem); }"},"editable":true},
{"formatter":"telefone","index":"TELCONTATO02","search":true,"hidden":false,"sorttype":"text","sortable":true,"width":0,"align":"right","name":"TELCONTATO02","editoptions":{"text":true,"required":false,"dataInit":"function (elem) {  return mostra_telefone(elem); }"},"editrules":{"text":true,"required":false,"dataInit":"function (elem) {  return mostra_telefone(elem); }"},"editable":true}
]

生成手机掩码的方法...

(function($) {
    'use strict';
    $.extend($.fn.fmatter, {
        mostra_telefone : function (elem) {
            $(elem).mask("(99)9999-9999?");
        }
    });
})(jQuery);

但是当我选择、更改或添加记录时它永远不会被调用。

最佳答案

这是一段执行所需行为的代码。为此,我们使用 inputpask 插件。 $("#jqGrid").jqGrid({ ... col模型:[

                {
                    name: 'Telephone',
                    width: 100,
                    formatter: function(cellvalue, options, rowObject) {
                        var re = new RegExp("([0-9]{3})([0-9]{3})([0-9]{4,6})", "g");
                        return cellvalue.replace(re, "($1) $2-$3"); 
                    },
                    unformat : function(cellvalue, options, rowObject) {
                        return cellvalue.replace("(",'').replace(") ",'').replace("-",'');
                    },
                    editable: true,
                    edittype: "custom",
                    editoptions :{
                        custom_element : function(value, options) {
                            var el = document.createElement("input");
                            el.type="text";
                            el.value = value;
                            $(el).addClass('inline-edit-cell ui-widget-content ui-corner-all').inputmask({"mask": "(999) 999-9999"});
                            return el;                              
                        },
                        custom_value : function(elem, operation, value) {
                            if(operation === 'get') {
                                return $(elem).val().replace("(",'').replace(") ",'').replace("-",'').replace(/\_/g,'');;
                            } else if(operation === 'set') {
                                $(elem).val(value);
                            }                               
                        }
                    },
                    editrules : {
                        requiered : true,
                        custom : true,
                        custom_func : function(val, name) {
                            // special replace mask at end
                            var cel  = val.replace("(",'').replace(") ",'').replace("-",'').replace(/\_/g,'');
                            if(cel.length !== 10) {
                                return [false,"Please, enter correct phone number"];
                            } 
                            return [true,""];
                        }
                    }   
                },                  

          ....
       ],
    ....
});

此代码也应该适用于您的版本。 这里还有一个link to the demo .

关于javascript - 在编辑行的 jqGrid 列中添加 mask ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60618300/

相关文章:

javascript - AppEngine API 客户端身份验证在更新后停止工作

javascript - Chrome 中的警告框 "not showing"- 关闭屏幕?

jquery - 选择内联 jquerymobile

javascript - 如何从json中获取仅数字键值对

jquery - jqgrid蒙版编辑

jquery-ui - 如何在jqgrid中将单列标题文本包装为多行

javascript - 如何在Javascript中获取页面URL并调用脚本?

javascript - 如何找到点击量?

javascript - AngularJS - 取消选中复选框

javascript - 有没有办法在 jqGrid 3.4.4 中设置每列的搜索选项?