jqgrid中自定义链接的参数

时间:2013-09-20 10:53:44

标签: jquery jqgrid href

enter image description here

当用户点击链接公式1时,我想重定向到http://en.wikipedia.org/wiki/7 而不是http://en.wikipedia.org/wiki/Formula 1

其中7是公式1的id

我的JSON响应如下所示

  {"rows":[{"id":7,"person":"Michael Schumacher","type":"Sport","name":"Formula 1"},
          {"id":8,"person":"Lukas Podolski","type":"Sport","name":"Football"},
          {"id":9,"person":"Blaise Pascal","type":"Sport","name":"mathematics"},
          {"id":6,"person":"Albert Einstein","type":"Sport","name":"Physics"}]}

jqgrid的代码是

     jQuery("#list2").jqGrid({
     url:"***.****",
        datatype: "json",
            mtype: 'GET',
            colNames:['Name','Category','Subcategory'],
            colModel:[
               {name:'person',index:'person', width:150},
               {name:'type',index:'type', width:150},
               {name:'name',index:'name', width:150,        
                   formatter: function (cellvalue, options, rowObject) {
                        var cellPrefix = '';
                        return cellPrefix + '<a href="http://en.wikipedia.org/wiki/' + cellvalue + '">' +
                               cellvalue + '</a>';
                    }}

            ],

            width:"647px",
            caption:"How to create custom Unobtrusive links"

        });

2 个答案:

答案 0 :(得分:2)

您只需将自定义格式化程序代码中的cellvalue替换为options.rowId

formatter: function (cellvalue, options) {
    return '<a href="http://en.wikipedia.org/wiki/' + options.rowId + '">' +
            cellvalue + '</a>';
}

您还应该修复您的JSON数据并使用"name":"Formula 1"而不是"name"="Formula 1""name"列的所有数据都相同)。可能只是在堆栈溢出中准备问题时输入错误。

答案 1 :(得分:1)

试试这个:

colNames:['Id','Name','Category','Subcategory'],
colModel:[
          {name:'id',index:'id', width:150, hidden: true},
          {name:'person',index:'person', width:150},
          {name:'type',index:'type', width:150},
          {name:'name',index:'name', width:150,     
          formatter: function (cellvalue, options, rowObject) {
                       var pageId = jQuery('[id="' + options.rowId + '"]').find('td:first').text();
                       return '<a href="http://en.wikipedia.org/wiki/' + pageId + '">' +
                       cellvalue + '</a>';
          }

尝试将隐藏的true设置为来自此td的文本。