0

在此处输入图像描述

当用户点击链接公式 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"

        });
4

2 回答 2

2

You can just replace cellvalue inside of the code of the custom formatter to options.rowId:

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

You should additionally fix your JSON data and use "name":"Formula 1" instead of "name"="Formula 1" (the same for all data of "name" column). Probably it's just typing error during preparing of your question on the stackoverflow.

于 2013-09-20T11:20:51.823 回答
1

Try with this:

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>';
          }

Try putting hidden true and get the text from this td.

于 2013-09-20T11:20:39.517 回答