1. 程式人生 > >jquery jqGrid翻頁記錄原來資料並勾選已選擇的資料

jquery jqGrid翻頁記錄原來資料並勾選已選擇的資料



//新建一個數組,存放已勾選的id
var arrayNewList = new Array();

$("#proList").jqGrid({
   url:"<%= basePath %>/mPromo/selectProList.do",
   queryFormId:"queryForm",
   colNames:["產品編碼","產品名稱","分類編碼","分類名稱","二級產品線編碼","二級產品線名稱","id"],
    colModel:[
           {name:"code",index:"code",align:'center',width:80},
           {name:"name",index:"name",align:'left',width:100},
           {name:"categoryCode", index:"categoryCode",align:'left',width:80},
           {name:"categoryName", index:"categoryName",align:'left',width:100},
           {name:"productLineTwoCode",index:"productLineTwoCode",align:"left",width:80},
           {name:"productLineTwoName",index:"productLineTwoName",align:"left",width:100},
           {name:'id',index:'id',width:1,hidden:true/*,key:true*/}
          ],
    pager:"#gridPager",
     height: '350',
   beforeRequest:function(){
      if(gridParam.page!=""&&gridParam.page!=undefined&&gridParam.flag){
         gridParam.flag=false;
         $("#proList").setGridParam(gridParam);
      }
   },
  jsonReader: {id: "code"},
   multiselect: true,
 //為表格增加複選框
   gridComplete:function(){
      tb_init("a.thickbox, area.thickbox, input.thickbox");
      var rowIds = jQuery("#proList").jqGrid('getDataIDs');
      for(var k=0; k<rowIds.length; k++) {
         var curRowData = jQuery("#proList").jqGrid('getRowData', rowIds[k]);
         var curChk = $("#"+rowIds[k]+"").find(":checkbox");
         curChk.attr('name', 'checkboxname');   //給每一個checkbox賦名字
         curChk.attr('value', curRowData['code']);   //給checkbox賦值

      }
   },
  loadComplete:function(xhr){
        var array = xhr.list;
        if (arrayNewList.length > 0) {
            $.each(array, function (i, item) {
                if (arrayNewList.indexOf(item.code) > -1) {
                    //判斷arrayNewList中存在item.code值時,選中前面的複選框,
                    $("#jqg_proList_" + item.code).attr("checked", true);
                }
            });
        }

  },
  onSelectAll:function(aRowids,status){
       if(status==true){
           //迴圈aRowids陣列,將code放入arrayNewList陣列中
           $.each(aRowids,function(i,item){
              saveData(item);
           })
       }else{
           //迴圈aRowids陣列,將code從arrayNewList中刪除
           $.each(aRowids,function(i,item){
              deleteIndexData(item);
           })
       }

  },
   onSelectRow:function(aRowids,status){
       if(status==true){
          saveData(aRowids);
       }else{
          deleteIndexData(aRowids);
       }

   }

});

function saveData(obj){
    arrayNewList.push(obj);
}
function deleteIndexData(obj){
    //獲取obj在arrayNewList陣列中的索引值
    for(i = 0; i < arrayNewList.length; i++){
        if (arrayNewList[i] == obj){
            //根據索引值刪除arrayNewList中的資料
            arrayNewList.remove(i);
            i--;
        }
     }
}