jqGrid 列内容超过一定长度省略表示

 

 

jqgrid初始化方法中的,对应列添加formatter方法

colNames : [                                          
    "描述" 
  ],
colModel : [

    {
         name : 'description',
         index : 'description',
         width : 60,
         sortable : true,
         formatter : function(cellvalue, options,rowObject){
         //显示 转义
         cellvalue=cellvalue.replace(/({|})/g,'');   //过滤{}
         cellvalue=cellvalue.replace(/</g,'&lt;');    //置换符号<
         cellvalue=cellvalue.replace(/>/g,'&gt;');    //置换符号>
           //  t=t.replace(/<\/?[^>]*>/g,''); //*<\/?[^>]*>可以匹配<script></style></body>等,并置空。而不是替换<和>两个符号
         return role_strLimite(cellvalue);
         }, 
    },
],

 

当对应字段长度超过20个时,只显示前二十个,后面以省略号“...”表示

function role_strLimite(cellvalue) {
    if (cellvalue != null && cellvalue.length > 20) {
         var value = cellvalue.toString().replace("\"", " ").replace("\'", " ");
         return "<div title ='" + GLOBAL.XXX.YYY.showAllInfo(value) + "'>" + value.substring(0, 20) + ".....</div>";
    }
    if (cellvalue == null) {
         return "";
    }
        return cellvalue;
}

 

/**
 * 强制截取字符串换行,解决火狐浏览器悬浮窗不换行的问题
 * @param value
 * @returns {string}
 */
GLOBALXXX.YYY.showAllInfo = function (value) {
    var valueStr = "";
    var len = value.length;
    var count = Math.ceil(len / 50);
    for (var i = 0; i < count; i++) {
        if (i == count - 1) {
            valueStr = valueStr + value.substring(i * 50);
        } else {
            valueStr = valueStr + value.substring(i * 50, (i + 1) * 50) + "\n";
        }
    }

    return valueStr;
}

 

posted @ 2018-05-28 10:17  斑马森林  阅读(2363)  评论(0编辑  收藏  举报