2010-12-09 27 views
2

我一直在玩jqgrid,並有以下問題。訪問jqgrid列和值

  • 要獲取ID列的值,我們使用getDataIDs()。有沒有一種方法可以獲取其他列的值(我使用自定義格式化程序創建列鏈接並向「loadComplete」上的每一行添加刪除鏈接。)
  • 我可以在循環遍歷列時使用條件語句,值。我的意思是,我想循環下面的user_name列,並根據它的值我想顯示刪除某些值,並不想顯示某些值

以下是代碼。

$(document).ready(function() { 


    $("#list").jqGrid(
    { 
    url:'index.cfc?method=getData', //CFC that will return the users 
    datatype: 'json', //We specify that the datatype we will be using will be JSON 
    mtype: 'POST', 
    colNames:['User ID', 'User Name'], //Column Names 
    colModel :[  
    {name:'user_id',index:'user_id', sorttype:"string", formatter: 'showlink', formatoptions:{baseLinkUrl:'#'}}, 
    {name:'user_name',index:'user_name', sorttype:"string"}, 
    {name:'act',index:'act',sortable:false} 
    ], 
    pager: $('#pager'), //The div we have specified, tells jqGrid where to put the pager 
    toppager: true, 
    height: 'auto', 
    width: 1270, 
    rowNum:25, //Number of records we want to show per page 
    rowList:[25,50,100], //Row List, to allow user to select how many rows they want to see per page 
    sortorder: "asc", //Default sort order 
    sortname: "user_id", //Default sort column 
    loadComplete: function() { 
    var myGrid = $("#list"); 
    var ids = myGrid.getDataIDs(); 
    for (var i = 0, idCount = ids.length; i < idCount; i++) { 
     $("#"+ids[i]+" a",myGrid[0]).click(function(e) { 
     var hash=e.currentTarget.hash;// string like "#?id=0" 
     if (hash.substring(0,5) === '#?id=') { 
     var id = hash.substring(5,hash.length); 
     var text = this.textContent; 
     alert("clicked the row with id='"+id+"'. Link contain '"+text+"'"); 
     location.href="http://en.wikipedia.org/wiki/"+text; 

     } 
     e.preventDefault(); 

     }); 
    } 

    for(var i=0;i<ids.length;i++){ 
     var cl = ids[i]; 

     vdelete = "<a href='#' onclick=\"testfn('"+cl+"')\">Delete</a></ids>"; 
     jQuery("#list").setRowData(ids[i],{act:vdelete}) 
    }  
    }, 
    caption: '', //Grid Name 
    jsonReader: { 
    root: "ROWS", //our data 
    page: "PAGE", //current page 
    total: "TOTAL", //total pages 
    records:"RECORDS", //total records 
    cell: "", 
    id: "0" 

    } 
    }) 


    $("#list").jqGrid('navGrid','#pager', 
    { 
    edit:false, 
    add:false, 
    del:false, 
    search:true, 
    refresh: true, 
    searchtext:"Search", 
    refreshtext: "Refresh", 
    'cloneToTop':true 
    }); 


    }); 

好吧,我得到這個工作使用getRowData。

   var rows= jQuery("#list").jqGrid('getRowData'); 
       alert(rows.length); 
       for(var i=0;i<rows.length;i++){ 
         var row=rows[i]; 
        alert(row['user_id']);//get the user_id column value 
        } 

但我還有一個問題。我想要一個隱藏的列並獲取這些值。當我嘗試上面的代碼時,我只能得到空值。

colModel :[  
{name:'user_id',index:'user_id', sorttype:"string", formatter: 'showlink', formatoptions:{baseLinkUrl:'#'}}, 
{name:'user_name',index:'user_name', sorttype:"string"}, 
{name:'act',index:'act',sortable:false}, 
{name:'secretCol', hidden:true} 
], 

回答

1

是的,它是可能的。所有你必須使用在自定義格式化第三個參數。第三放慢參數是行對象,因此總是有整排的基礎上,你可以做任何你想要的值在你的格式化器中顯示