2011-11-17 179 views
1

我需要找出一種方法來獲取我的jQuery的tableToGrid調用某些列的格式化我的幾個單元格。我知道你可以在colModel中放置一個formater,併爲你設置格式。我想知道是否有標準的電話號碼格式化程序,還是我會被迫寫一個?感謝您的輸入。jqGrid tableToGrid格式化電話號碼列

回答

2

我不知道你想如何格式化你的電話號碼,但這裏有一些我用於項目的代碼。如果您取消註釋行權,你可以切換(xxx)xxx-xxxxxxx-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; 
} 
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; 
} 
+0

這工作很適合我的國際電話號碼。函數格式PhoneNumber(cellvalue,options,rowObject){0} {0} { [0-9] {4,6})「,」g「); cellvalue = cellvalue.replace(re,「($ 1)$ 2- $ 3」); return cellvalue; } – user424855