jqGrid默认选中行

jqGrid默认选中行

1.确定对比参数列表

var  usernames=["taoql","matea"];

2.loadComplete中添加方法gridSelected()

/**
 * 根据usernames对比选中
 */
function gridSelected(){
    var rowIds = jQuery(grid_selector).jqGrid('getDataIDs');
    for(var k=0; kvar curRowData = jQuery(grid_selector).jqGrid('getRowData', rowIds[k]);
       for(i=0;iif(curRowData.username == username){
               jQuery(grid_selector).setSelection(rowIds[k], false); 
           }
       }
    }
}

3.属性添加选中事件onSelectRow: onClickWaitRow,


/**
 * 行点击事件
 */
function onClickWaitRow(id,status){
    var curRowData = jQuery(grid_selector).jqGrid('getRowData', id);
    if(!status){
        gridRemoveSelect(curRowData.username);
    }else{
        gridAddSelect(curRowData.username);
    }
}

4.添加选中、取消事件

/**
 * 取消选中
 */
function gridRemoveSelect(username){
    usernames.remove(username);
}

/**
 * 添加选中
 */
function gridAddSelect(username){
    usernames.push(username);
}

5.重写Array方法,便于remove


/**
 * 重写Array
 */
Array.prototype.remove = function(val) {
    var index = this.indexOf(val);
    if (index > -1) {
    this.splice(index, 1);
    }
};

/**
 * 重写Array
 */
Array.prototype.indexOf = function(val) {
    for (var i = 0; i < this.length; i++) {
    if (this[i] == val) return i;
    }
    return -1;
};

你可能感兴趣的:(jqGrid)