解决方案
给table添加点击事件
onCheck:onCheck,
onUncheck:onUncheck
//合并单元格后复选框也进行了合并,选中复选框合并行全部选中
var rs = -1;
function onCheck(index, row) {
if (rs == -1) {
rs = index;
var rbs = row.name;
var rows = $("#table01").datagrid("getRows");
for (var z = 0; z < rows.length; z++) {
if (rbs === rows[z].name) {
$("#table01").datagrid("checkRow", z);
}
}
rs = -1
}
}
function onUncheck(index, row) {
if (rs == -1) {
rs = index;
var rbs = row.name;
var rows = $("#table01").datagrid("getRows");
for (var i = 0; i < rows.length; i++) {
if (rbs === rows[i].name) {
$("#table01").datagrid("uncheckRow", i);
}
}
rs = -1
}
}
示例
html页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>列表</title>
<script src="js/table.js"></script>
</head>
<body>
<table id="table01">
<button class="btn-blue" onclick="select()">获取勾选的内容</button>
</body>
</html>
js页面
$(function() {
addtable();
}
function addtable(){
$.datagrid = {
url: 'url',
id:'table01',
method:'POST',
remoteSort:false,
formNav:true,
singleSelect:false,
striped:true,
queryParams:{}
mergeCol:['name'],
columns: [[
{field: 'id', title: "编号", width: 0,align: 'center',checkbox:true},
{field: 'name', title: "姓名", width: '15%',align: 'center'},
{field: 'age', title: "年龄", width: '10%',align: 'center',
formatter: function(value,row,index){
return "年龄:"+value;
}
}
{field: 'address', title: "地址", width: '15%',align: 'center'}
]],
pagination: true,
height:'90%',
onLoadSuccess: function (data) {
$('#table01').datagrid('clearSelections'); //一定要加上这一句,要不然datagrid会记住之前的选择状态,删除时会出问题
},
onCheck:onCheck,
onUncheck:onUncheck
}
}
//合并单元格后复选框也进行了合并,选中复选框合并行全部选中
var rs = -1;
function onCheck(index, row) {
if (rs == -1) {
rs = index;
var rbs = row.name;
var rows = $("#table01").datagrid("getRows");
for (var z = 0; z < rows.length; z++) {
if (rbs === rows[z].name) {
$("#table01").datagrid("checkRow", z);
}
}
rs = -1
}
}
function onUncheck(index, row) {
if (rs == -1) {
rs = index;
var rbs = row.name;
var rows = $("#table01").datagrid("getRows");
for (var i = 0; i < rows.length; i++) {
if (rbs === rows[i].name) {
$("#table01").datagrid("uncheckRow", i);
}
}
rs = -1
}
}
获取被点击的行的内容
function select(){
var checkeds = $('#table01').datagrid('getChecked');
var ages=[];
for(var i=0;i<checkeds.length;i++){
ages.push(checkeds[i].age);
}
console.log(ages);