EasyUi datagrid 单选框选中事件
Easyui datagrid中的单选框默认是这样定义的
columns: [[
{ field: 'CK', title: '', checkbox: true, width: 30 }]]。
平常使用没什么问题,但今天下等我要获取单框选中事件时,出了点问题。
因为这个checkbox是独立于行的,所以单击这个checkbox时,不会触发Easyui datagrid的onClickRow事件。
用户在单选框上打了勾,最后却被告知没有行选中,这不是Bug吗?
这是我们码农绝对不能忍受的,于是乎,对EasyUi datagrid的改造开始了。
首先,我重新定义checkbox,代码如下:
columns: [[
{ field: 'checked', title: 'Choice', width: 30,
formatter: function(value, row, index) {
return '<input type="checkbox" name="DataGridCheckbox">';
}}]]
这下子,checkbox与行成为一体了,单击checkbox时,行会选中,但新问题来了,单选行时,checkbox并不会选中。
于是,继续改造。
在onClickRow事件中我定义,行选中,对应的CheckBox也要被选中。
代码如下:
onClickRow: function(index, data) {
var row = $('#UserList').datagrid('getSelected');
$('#UserList').datagrid("getPanel").find(".datagrid-view2 .datagrid-body table input[type='checkbox']:eq(" + index + ") ").attr("checked", true);
}
这样,行被选中了,但单击其它行中,原来的行的CheckBox继续保持被选中,并没有被取消,这与期望不符呀。
于是,我继续改造,这次改造的目标,就是单击哪行,哪行及它的CheckBox被选中,其他的不被选中。
代码如下:
onClickRow: function(index, data) {
//将所有checkbox修改为未选中
$('#UserList').datagrid("getPanel").find(".datagrid-view2 .datagrid-body table input[type='checkbox'] ").attr("checked", false);
//将这次的checkbox标记为选中
$('#UserList').datagrid("getPanel").find(".datagrid-view2 .datagrid-body table input[type='checkbox']:eq(" + index + ") ").attr("checked", true);
}
到这个时候,还有其它问题,比如说:第一次单击的时候是选中,第二次,我希望能取消选中。
于是代码继续改造。改造完成之后的代码如下:
var selectIndex = "";
function UserListLoad() {
var customerNo = $('#txtCustomerNo').val();
var customerName = $('#txtCustomerName').val();
var country = $('#txtCountry').val(); $('#UserList').datagrid({
url: '/ForLog/OrderReport/GetSapUserList',
queryParams: { customerNo: customerNo, customerName: customerName, country: country },
pagination: true,
pageSize: 15,
singleSelect: true,
showPageList: false,
pageList: [5, 15, 15],
rownumbers: true,
nowrap: false,
loadMsg: 'Load……',
columns: [[
{ field: 'checked', title: 'Choice', width: 30,
formatter: function(value, row, index) {
return '<input type="checkbox" name="DataGridCheckbox">';
}
},
{ field: 'NO', title: 'Customer Order No.', width: 150 },
{ field: 'NAME', title: 'Customer', width: 200 },
{ field: 'COUNTRY', title: 'Country', width: 200 }
]],
onClickRow: function(index, data) {
var row = $('#UserList').datagrid('getSelected');
if (index == selectIndex) {
//第一次单击选中,第二次单击取消选中
$('#UserList').datagrid("getPanel").find(".datagrid-view2 .datagrid-body table input[type='checkbox']:eq(" + index + ") ").attr("checked", false);
$('#UserList').datagrid('clearSelections');
} var isCheck = $('#UserList').datagrid("getPanel").find(".datagrid-view2 .datagrid-body table input[type='checkbox']:eq(" + index + ") ").attr("checked"); if (isCheck) {
//将所有checkbox修改为未选中
$('#UserList').datagrid("getPanel").find(".datagrid-view2 .datagrid-body table input[type='checkbox'] ").attr("checked", false);
//将这次的checkbox标记为选中
$('#UserList').datagrid("getPanel").find(".datagrid-view2 .datagrid-body table input[type='checkbox']:eq(" + index + ") ").attr("checked", true);
}
else {
if (index == selectIndex) {
$('#UserList').datagrid("getPanel").find(".datagrid-view2 .datagrid-body table input[type='checkbox']:eq(" + index + ") ").attr("checked", false);
}
else {
//将所有checkbox修改为未选中
$('#UserList').datagrid("getPanel").find(".datagrid-view2 .datagrid-body table input[type='checkbox'] ").attr("checked", false);
//将这次的checkbox标记为选中
$('#UserList').datagrid("getPanel").find(".datagrid-view2 .datagrid-body table input[type='checkbox']:eq(" + index + ") ").attr("checked", true);
}
}
selectIndex = index;
}
});
到此,目标基本达成,效果如下图所示。
聪明的你,是否发现,这里其实还有一个问题的,就是当对某一行单击三次及三次以上,选中和非选中的切换是有问题的。
不过,我并不打算在这里解决了,有兴趣可以自己试试,必竟自己解决问题的那种喜悦和成就感是其他事情无法替代的。
EasyUi datagrid 单选框选中事件的更多相关文章
- layui 单选框选中事件
<div class="layui-form-item" pane=""> <label class="layui-form-lab ...
- easyui datagrid 单选框 效果
columns: [[{ field: 'oid', title: '选择', width: 20, forma ...
- easyUI datagrid中checkbox选中事件以及行点击事件,翻页之后还可以选中
DataGrid其中与选择,勾选相关 DataGrid属性:singleSelect boolean 如果为true,则只允许选择一行. false ctrlSelect boolean 在启用多行选 ...
- Jquery监控audio单选框选中事件(实际通过click)
$('input:radio[name="pathType"]').click(function(){ var checkValue = $('input:radio[name=& ...
- jquery复选框 选中事件 及其判断是否被选中
jquery复选框 选中事件 及其判断是否被选中 (2014-07-25 14:03:54) 转载▼ 标签: jquery复选框选中事件 分类: extjs jquery 今天做了 显示和不显示密 ...
- jquery 根据后台传过来的值动态设置下拉框、单选框选中
更多内容推荐微信公众号,欢迎关注: jquery 根据后台传过来的值动态设置下拉框.单选框选中 $(function(){ var sex=$("#sex").val(); va ...
- 判断单选框选中不成功,$("#xx").attr("checked")undefined
判断单选框选中状态,各种都不行,受到https://www.cnblogs.com/yxwkf/p/4853014.html 的启发,相关引用: 原来.在jquery1.6版本号便对此做出了改动: [ ...
- EasyUI DataGrid单选如何取消选中
EasyUI DataGrid在多选时,选中某行,可以取消:而在单选时,并不能取消选中某一行. 可以通过修改源码来完成. 在其源码中找到 opts.singleSelect==true 将代码做如下修 ...
- easyui datagrid editor checkbox 单击事件
Easyui datagrid treegrid中能够为行追加checkbox元素.比如: $('#tt').treegrid({ url:'get_data.php', idField:'id', ...
随机推荐
- TS流格式(转)
一 从TS流开始 数字电视机顶盒接收到的是一段段的码流,我们称之为TS(Transport Stream,传输流),每个TS流都携带一些信息,如Video.Audio以及我们需要学习的PAT.PMT等 ...
- 2109&2535: [Noi2010]Plane 航空管制 - BZOJ
Description世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此,小X表示很不满意. 在这次来烟台的路上, ...
- 高性能网络编程2----TCP消息的发送
转 陶辉 taohui.org.cn 在上一篇中,我们已经建立好的TCP连接,对应着操作系统分配的1个套接字.操作TCP协议发送数据时,面对的是数据流.通常调用诸如send或者write方法来发送数据 ...
- css table表格无法调整宽度问题分析
1.在网上查找了相关问题,有的说表格设置了背景图片,把原来的宽度撑开了,导致无法变窄~! 在项目中,原来美工设计的页面,设置了一个块的样式class="title",现在有一段ht ...
- java poi导入EXCEL xls文件代码
/** * */ package com.bn.car.common.report.excel; import java.io.FileInputStream; import java.io.IOEx ...
- asp.net 获取客户机IP地址
/// <summary> ///get client IP /// </summary> /// <returns></returns> public ...
- c#操作剪切板
C#定义了一个类System.Windows.Forms.Clipboard来简化剪切板操作,这个类有一个静态方法,主要有: Clear 清除剪切板中的所有数据: ContainsData,Conta ...
- 2014多校第一场D题 || HDU 4864 Task (贪心)
题目链接 题意 : 用N台机器,M个任务,每台机器都有一个最大工作时间和等级,每个任务有一个需要工作时间和一个等级.如果机器完成一个任务要求是:机器的工作时间要大于等于任务的时间,机器的等级要大于等于 ...
- 【Linux高频命令专题(1)】sort
介绍 sort命令是帮我们依据不同的数据类型进行排序,其语法及常用参数格式: sort [-bcfMnrtk][源文件][-o 输出文件] 补充说明:sort可针对文本文件的内容,以行为单位来排序. ...
- zenmap 的扫描方式
第一种:Intense scan (nmap -T4 -A -v) 一般来说,Intense scan可以满足一般扫描 -T4 加快执行速度 -A 操作系统及版本探测 -v 显示详细的输出 第二种:I ...