您可以通过不同的方式做到这一点。第一个是使用formatter:'showlink'
形式如下
formatoptions: {
baseLinkUrl: 'javascript:',
showAction: "MyBase.GetAndShowUserData(jQuery('#list'),'",
addParam: "');"
}
(see my 旧答案了解详情)。它将产生<a>
链接到
href="javascript:MyBase.GetAndShowUserData(jQuery('#userlist'),'?id=rowId');"
where rowId
将是相应网格行的 id。在你的自定义里面global功能MyBase.GetAndShowUserData
你应该剪掉"?id="
第二个参数的前缀。这样您就可以访问网格并且知道所选的 ID。
另一种方法是自己编写自定义格式化程序而不是使用formatter:'showlink'
.
我认为这两种方法的主要缺点是使用global功能。此外,我更喜欢遵循的概念不引人注目的 JavaScript。所以我可以建议你另一种方式我的答案在 Trirand 论坛上。这个想法是使用预定义的格式化程序showlink
以 '#' 作为值href
属性和进行绑定 to the click
内部链接的事件loadComplete
功能:
colModel: [
{ name: 'Subcategory', formatter:'showlink',formatoptions:{baseLinkUrl:'#'}
...
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 || this.innerText;
alert("clicked the row with id='"+id+"'. Link contain '"+text+"'");
location.href="http://en.wikipedia.org/wiki/"+text;
}
e.preventDefault();
});
}
}
观看现场演示here。在演示中,如果您单击表格中的“Physics”等文本,将会打开该 urlhttp://en.wikipedia.org/wiki/Physics这将是动态构建的。我添加了一个附加警报,以展示如何另外解码有关行 id 的信息。
UPDATED:看改进后的代码(从性能方面)loadComplete
in 另一个答案.