访问jqgrid列和值

时间:2010-12-09 03:01:46

标签: jquery coldfusion 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 个答案:

答案 0 :(得分:1)

是的,它是可能的。你必须使用的是自定义格式化程序中的第三个参数。第三个参数是行对象,因此它总是具有整行的值,您可以根据它在格式化程序中执行任何操作

相关问题