我需要找到一种方法来获取我的jquery的tableToGrid调用的某些列来格式化我的几个单元格。我知道您可以在colModel中放置一个formater,它将为您格式化该列。我想知道是否有标准的电话号码格式化程序,还是我会被迫写一个?感谢您的投入。
答案 0 :(得分:2)
我不确定你想如何格式化你的电子邮件,但这里有一些我用于项目的代码。如果您取消注释右侧行,则可以在(xxx)xxx-xxxx
和xxx-xxx-xxxx
colModel: [
{ name: 'phonenumber', index: 'phonenumber', width: 60, align: "center", formatter: formatPhoneNumber}
]
// converts number to (xxx)xxx-xxxx or xxx-xxx-xxxx
function formatPhoneNumber(cellvalue, options, rowObject) {
var re = /\D/;
// test for this format: (xxx)xxx-xxxx
var re2 = /^\({1}\d{3}\)\d{3}-\d{4}/;
// test for this format: xxx-xxx-xxxx
//var re2 = /^\d{3}-\d{3}-\d{4}/;
var num = cellvalue;
if (num === null){
num = "";
}
var newNum = num;
if (num != "" && re2.test(num) != true) {
if (num != "") {
while (re.test(num)) {
num = num.replace(re, "");
}
}
if (num.length == 10) {
// for format (xxx)xxx-xxxx
newNum = '(' + num.substring(0, 3) + ')' + num.substring(3, 6) + '-' + num.substring(6, 10);
// for format xxx-xxx-xxxx
// newNum = num.substring(0,3) + '-' + num.substring(3,6) + '-' + num.substring(6,10);
}
}
return newNum;
}
答案 1 :(得分:2)
我认为这是最好的方法......
colModel: [
{ name: 'phonenumber', index: 'phonenumber', width: 60, align: "center", formatter: formatPhoneNumber}
]
function formatPhoneNumber(cellvalue, options, rowObject) {
var re = new RegExp("([0-9])([0-9]{3})([0-9]{3})([0-9]{3,6})", "g");
cellvalue=cellvalue.replace(re, "+$1($2)-$3-$4");
return cellvalue;
}