添加按钮,该按钮将重定向到JQGrid中当前行的“查看页面" [英] to add button which will redirect to View Page of current row in JQGrid
问题描述
我正在尝试添加按钮而不是View
列,但是我尝试使用formatter
仍未加载按钮,但其余列的记录即将到来.
下面是我的代码:
I am trying to add button instead of View
column but i tried with formatter
still button is not loading but records are coming for the rest of the columns.
Below is my code:
$(function () {
$("#grid").jqGrid({
url: "/Location/LocationsList1",
datatype: 'json',
mtype: 'Get',
colNames: ['Id', 'Country Name', 'State Name', 'City Name', 'Location Name','View'],
colModel: [
{ key: true, hidden: true, name: 'Id', index: 'Id', editable: true },
{ key: false, name: 'CountryName', index: 'CountryName', editable: true },
{ key: false, name: 'StateName', index: 'StateName', editable: true },
{ key: false, name: 'CityName', index: 'CityName', editable: true },
{ key: false, name: 'Name', index: 'Name', editable: true },
{ key: false, name: 'View', index: 'View', editable: true,formatter:ViewButton }],
pager: jQuery('#pager'),
rowNum: 10,
rowList: [10, 20, 30, 40],
height: '100%',
viewrecords: true,
caption: 'Location',
emptyrecords: 'No records to display',
jsonReader: {
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: false,
Id: "0"
},
});
});
function ViewButton(cellvalue, options, rowObject) {
var rowid= options.rowid;
var button = "<button class=\"viewLineItem\" id="+ rowid+">View Line Item</button>"
$('#' + rowid).die();
$('#' + rowid).live('click', function (rowId) {
alert("hi");
alert(rowId);
});
};
我是JqGrid的新手,不知道它是如何工作的.任何指导/帮助将不胜感激.
I am new to JqGrid and don't know how it works. Any guidance/Help will be appreciated.
推荐答案
代码有一些问题
-
options
没有rowid
属性,但是具有rowId
属性.您应该将options.rowid
更改为options.rowId
- 在构建网格主体的HTML片段期间,格式化程序将称为 .该页面上目前没有网格元素.因此,您目前无法使用
$('#' + rowid).live('click', ...);
. - 格式化程序必须返回HTML片段,该片段将放置在相应的单元格中(在
<td>
内部).在格式化程序末尾会错过return button;
. - JavaScript中存在众所周知的名称转换.应该使用函数,仅当您定义新类的构造函数时,该函数才应以大写字母开头.您会看到
ViewButton
将以其他颜色显示,以将类与其他功能区分开.您应该将ViewButton
重命名为viewButton
,以保持JavaScript的标准名称转换. - 最好不要在
colModel
中指定index
属性.以相同的方式,不应包含具有默认值的属性,例如key: false
.要为许多列指定 common 属性,可以使用cmTemplate
属性. - 应该减少 global 函数的数量,因为这些函数将被视为
window
对象的属性.
可以使用 - 代替
name: 'Id'
的用法隐藏列,而可以指定jsonReader
的id: 'Id'
属性.您使用repeatitems: false
属性,这意味着输入数据的每一项都具有属性CountryName
,StateName
等. id属性的默认名称(rowid-<tr>
元素的id
)为id
,但您改用了Id
.属性id: "Id"
通知jqGrid.
options
has norowid
property, but it hasrowId
property. You should changeoptions.rowid
tooptions.rowId
- The formatter will be called during building the HTML fragment of the grid body. No element of the grid exist at the moment on the page. Thus you can't use
$('#' + rowid).live('click', ...);
at the moment. - The formatter have to return the HTML fragment, which will be placed in the corresponding cell (inside of
<td>
). One missesreturn button;
at the end of the formatter. - There are exist well-known name conversion in JavaScript. One should use functions, which starts with capital letter only if you define the constructor of the new class. You see that
ViewButton
will be displayed in the other color to distinguish classes from other function. You should renameViewButton
toviewButton
to hold the standard name conversion of JavaScript. - It's better don't specify
index
property incolModel
. In the same way one should not include the properties with the defaul value, likekey: false
. To specify common property for many columns you can usecmTemplate
property. - One should reduce the number of global functions, because the functions will be considerd as the properties of
window
object. - instead of usage hidden column with
name: 'Id'
one can specifyid: 'Id'
property ofjsonReader
. You userepeatitems: false
property, which means that every item of input data has propertiesCountryName
,StateName
and so on. The default name of the id property (the rowid - theid
of<tr>
elements) isid
, but you useId
instead. The propertyid: "Id"
informs jqGrid about it.
修改后的代码可能与以下内容有关
The modified code could be about the following
$(function () {
function viewButton(cellvalue, options, rowObject) {
return "<button class=\"viewLineItem\" id=\"" +
options.rowId + "\">View Line Item</button>";
}
$("#grid").jqGrid({
url: "/Location/LocationsList1",
datatype: 'json',
colNames: ['Id', 'Country Name', 'State Name', 'City Name', 'Location Name','View'],
colModel: [
{ name: 'Id', key: true, hidden: true },
{ name: 'CountryName' },
{ name: 'StateName' },
{ name: 'CityName' },
{ name: 'Name' },
{ name: 'View', editable: false, formatter: viewButton }
],
cmTemplate: { editable: true },
pager: '#pager',
rowNum: 10,
rowList: [10, 20, 30, 40],
height: '100%',
viewrecords: true,
caption: 'Location',
emptyrecords: 'No records to display',
jsonReader: { repeatitems: false, id: "Id" }
});
$("#jqGridA").click(function (e) {
var $td = $(e.target).closest("tr.jqgrow>td"),
rowid = $td.parent().attr("id"),
p = $(this).jqGrid("getGridParam");
if ($td.length > 0 && p.colModel[$td[0].cellIndex].name === "View") {
alert(rowid);
}
});
});
上述代码的最后一部分($("#jqGridA").click(...);
)注册整个网格的一个单击处理程序.如果用户单击任何单元格,则会由于事件冒泡而调用事件处理程序. e.target
给出了被单击的DOM元素(例如<button>
).通过使用closest
,我们可以转到外部的<td>
元素,该元素的父级是网格的行(<tr>
).该行的.attr("id")
是rowid.通过将点击处理程序绑定到网格内的每个按钮,这种绑定可以更有效地工作.
The last part of the above code ($("#jqGridA").click(...);
) register one click handler for the whole grid. If the user clicks on any cell then the event handler will be called because of event bubbling. The e.target
gives as the DOM element, which was clicked (for example the <button>
). By using closest
we can go to the outer <td>
element, which parent is the row (<tr>
) of the grid. The .attr("id")
of the row is the rowid. Such binding works more effectively as binding click handler to every button inside of the grid.
通过jqGrid已经有一个click
事件处理程序.可以使用beforeSelectRow
回调,因为它将在click
处理程序内部被调用.一个应该不要忘记从beforeSelectRow
回调返回true
来通知jqGrid您允许选择该行.回调beforeSelectRow
已经具有rowid
作为第一个参数,这使我们的代码稍微简化了一些.最终的代码将是
By the way jqGrid has already one click
event handler. One can use beforeSelectRow
callback, because it will be called inside of the click
handler. One should only don't forget to return true
from the beforeSelectRow
callback to inform jqGrid that you allow to select the row. The callback beforeSelectRow
has already rowid
as the first parameter, which simplify our code a little. The final code will be
$(function () {
function viewButton(cellvalue, options, rowObject) {
return "<button class=\"viewLineItem\" id=\"" +
options.rowId + "\">View Line Item</button>";
}
$("#grid").jqGrid({
url: "/Location/LocationsList1",
datatype: 'json',
colNames: ['Id', 'Country Name', 'State Name', 'City Name', 'Location Name','View'],
colModel: [
{ name: 'CountryName' },
{ name: 'StateName' },
{ name: 'CityName' },
{ name: 'Name' },
{ name: 'View', editable: false, formatter: viewButton }
],
cmTemplate: { editable: true },
pager: '#pager',
rowNum: 10,
rowList: [10, 20, 30, 40],
height: '100%',
viewrecords: true,
caption: 'Location',
emptyrecords: 'No records to display',
jsonReader: { repeatitems: false, id: "Id" },
beforeSelectRow: function (rowid, e) {
var $td = $(e.target).closest("tr.jqgrow>td"),
p = $(this).jqGrid("getGridParam");
if ($td.length > 0 && p.colModel[$td[0].cellIndex].name === "View") {
alert(rowid);
}
}
});
});
这篇关于添加按钮,该按钮将重定向到JQGrid中当前行的“查看页面"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!