JqGrid添加数据行中添加按钮

colNames: ['NewRequestId', 'Applicant', 'Applicant Department','Edit', 'Delete'], 
colModel: [
{ name: 'NewRequestId', index: 'NewRequestId', hidden: true, sortable: false, align: "center", width: "1px" },
{ name: 'ApplicatUserName', index: 'ApplicatUserName', sortable: false, align: "center", width: "120px" },
{ name: 'ApplicatDepartment', index: 'ApplicatDepartment', sortable: false, align: "center", width: "140px" },
{ name: 'Edit', index: 'Edit', sortable: false, align: "center", width: "100px" },

{ name: 'Delete', index: 'Delete', sortable: false, align: "center", width: "100px" }

],

 

在gridComplete事件中添加:

gridComplete: function () {
var ids = jQuery("#RequestList").jqGrid('getDataIDs');
for (var i = 0; i < ids.length; i++) {
var id = ids[i];
var DeleteBtn = "<a href='#' style='color:#f60' οnclick='OpenAllocationDialog()' >Abolish</a>";
var editBtn = "<a href='#' style='color:#f60' οnclick='OpenAllocationDialog()' >Edit</a>";
jQuery("#RequestList").jqGrid('setRowData', ids[i], { Edit: editBtn, Delete: DeleteBtn });
}


},

转载于:https://www.cnblogs.com/hexinxiaoyao/archive/2012/09/07/2674678.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 jqGrid 自带的 formatter 属性来实现操作列。首先,在 colModel 中定义操作列的属性,如下: ``` { name: 'operation', index: 'operation', width: 80, sortable: false, formatter: function(_, options, row) { var editBtn = '<button type="button" class="btn btn-xs btn-default editBtn" data-rowid="' + options.rowId + '">编辑</button>'; var delBtn = '<button type="button" class="btn btn-xs btn-danger delBtn" data-rowid="' + options.rowId + '">删除</button>'; return editBtn + ' ' + delBtn; } } ``` 其中,formatter 函数会接收三个参数: 1. `_`: 无意义 2. `options`: 当前单元格的一些属性,包括 rowId(行 ID)、colModel(列模型)、pos(当前单元格的左上角坐标)等等 3. `row`: 当前行的数据对象 在 formatter 中,我们可以根据 options 和 row 的值来自定义单元格的显示内容,返回一个 HTML 字符串即可。 然后,在 gridComplete 事件中为操作列的按钮绑定事件,如下: ``` gridComplete: function() { var $grid = $(this); // 编辑按钮点击事件 $grid.find('.editBtn').on('click', function() { var rowId = $(this).data('rowid'); var rowData = $grid.jqGrid('getRowData', rowId); // TODO:显示编辑对话框,并根据 rowData 的值填充表单 }); // 删除按钮点击事件 $grid.find('.delBtn').on('click', function() { var rowId = $(this).data('rowid'); $grid.jqGrid('delRowData', rowId); }); } ``` 在 gridComplete 中,我们可以利用 jqGrid 对象获取当前表格的 DOM 元素,并对其中的操作按钮绑定点击事件。需要注意的是,因为 jqGrid 会对表格的 DOM 结构进行修改,所以绑定事件时需要使用 $(this) 而非常规的选择器。 以上代码就可以实现在 jqGrid添加删除按钮了。如果需要修改按钮样式,可以在 formatter 函数中调整 HTML 字符串;如果需要添加更多的操作按钮,可在 formatter 和 gridComplete 中添加对应的代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值