bootgrid修改成可以全勾选和全取消勾选操作
1. 引言
由于项目需要,需要在不同页面上选择全勾选能全部勾选所有的记录,反勾选也如此。这个需求可以解决了一个样例:如果有150条记录,当前页就10条,你又在每一个页面勾选部分的记录,然后,如果你要全部全选,需要在每一个页面上勾上全选checkbox,全部反选也有这个问题。
2.修改的代码
基于jQuery Bootgrid v1.3.1这个版本。
bootgrid.js修改的部分如下:
//原有的select和deselect方法
/**
* Selects rows by ids. Selects all visible rows if no ids are provided.
* In server-side scenarios only visible rows are selectable.
*
* @method select
* @param [rowsIds] {Array} An array of rows ids to select
* @chainable
**/
Grid.prototype.select = function(rowIds)
{
if (this.selection)
{
rowIds = rowIds || this.currentRows.propValues(this.identifier); var id, i,
selectedRows = []; while (rowIds.length > 0 && !(!this.options.multiSelect && selectedRows.length === 1))
{
id = rowIds.pop();
if ($.inArray(id, this.selectedRows) === -1)
{
for (i = 0; i < this.currentRows.length; i++)
{
if (this.currentRows[i][this.identifier] === id)
{
selectedRows.push(this.currentRows[i]);
this.selectedRows.push(id);
break;
}
}
}
} if (selectedRows.length > 0)
{
var selectBoxSelector = getCssSelector(this.options.css.selectBox),
selectMultiSelectBox = this.selectedRows.length >= this.currentRows.length; i = 0;
while (!this.options.keepSelection && selectMultiSelectBox && i < this.currentRows.length)
{
selectMultiSelectBox = ($.inArray(this.currentRows[i++][this.identifier], this.selectedRows) !== -1);
}
this.element.find("thead " + selectBoxSelector).prop("checked", selectMultiSelectBox); if (!this.options.multiSelect)
{
//this.element.find("tbody > tr " + selectBoxSelector + ":checked")
// .trigger("click" + namespace);
var lastId = this.selectedRows[this.selectedRows.length-1];
var checkboxes = this.element.find("tbody > tr " + selectBoxSelector + ":checked");
for (i = 0; i < checkboxes.length; i++) {
var $checkbox = $(checkboxes[i]);
if (lastId != $checkbox.val()) {
$checkbox.trigger("click" + namespace);
}
}
} for (i = 0; i < this.selectedRows.length; i++)
{
this.element.find("tbody > tr[data-row-id=\"" + this.selectedRows[i] + "\"]")
.addClass(this.options.css.selected)._bgAria("selected", "true")
.find(selectBoxSelector).prop("checked", true);
} this.element.trigger("selected" + namespace, [selectedRows]);
}
} return this;
}; /**
* Deselects rows by ids. Deselects all visible rows if no ids are provided.
* In server-side scenarios only visible rows are deselectable.
*
* @method deselect
* @param [rowsIds] {Array} An array of rows ids to deselect
* @chainable
**/
Grid.prototype.deselect = function(rowIds)
{
if (this.selection)
{
rowIds = rowIds || this.currentRows.propValues(this.identifier); var id, i, pos,
deselectedRows = []; while (rowIds.length > 0)
{
id = rowIds.pop();
pos = $.inArray(id, this.selectedRows);
if (pos !== -1)
{
for (i = 0; i < this.currentRows.length; i++)
{
if (this.currentRows[i][this.identifier] === id)
{
deselectedRows.push(this.currentRows[i]);
this.selectedRows.splice(pos, 1);
break;
}
}
}
} if (deselectedRows.length > 0)
{
var selectBoxSelector = getCssSelector(this.options.css.selectBox); this.element.find("thead " + selectBoxSelector).prop("checked", false);
for (i = 0; i < deselectedRows.length; i++)
{
this.element.find("tbody > tr[data-row-id=\"" + deselectedRows[i][this.identifier] + "\"]")
.removeClass(this.options.css.selected)._bgAria("selected", "false")
.find(selectBoxSelector).prop("checked", false);
} this.element.trigger("deselected" + namespace, [deselectedRows]);
}
} return this;
}; //修改后的select和deselect方法
/**
* Selects rows by ids. Selects all visible rows if no ids are provided.
* In server-side scenarios only visible rows are selectable.
*
* @method select
* @param [rowsIds] {Array} An array of rows ids to select
* @chainable
**/
Grid.prototype.select = function(rowIds,flag)
{
var selectAllFlag = false;
var selectBoxSelector = getCssSelector(this.options.css.selectBox); if (this.selection)
{ //rowIds = rowIds || this.currentRows.propValues(this.identifier);
var id, i,
selectedRows = []; if(typeof rowIds == 'undefined'){//select All
selectAllFlag = true;
this.element.find("thead " + selectBoxSelector).prop("indeterminate", false).prop("checked", true).val('all');
//selectedRows = this.currentRows;
var this_currentRows = this.currentRows;
this.selectedRows = Object.keys(this_currentRows).map(function(i){return this_currentRows[i].Id}); }else if(typeof flag != 'undefined' && flag == 'load'){//load selected rowIds
selectedRows = Object.keys(rowIds).map(function(i){return rowIds[i]});
this.selectedRows = Object.keys(rowIds).map(function(i){return rowIds[i]});
}else{
while (rowIds.length > 0 && !(!this.options.multiSelect && selectedRows.length === 1))
{
id = rowIds.pop();
if ($.inArray(id, this.selectedRows) === -1)
{
for (i = 0; i < this.currentRows.length; i++)
{
if (this.currentRows[i][this.identifier] === id)
{
selectedRows.push(this.currentRows[i]);
this.selectedRows.push(id);
break;
}
}
}
}
} if (selectedRows.length > 0 || selectAllFlag)
{
//var selectBoxSelector = getCssSelector(this.options.css.selectBox),
/* var selectMultiSelectBox = this.selectedRows.length >= this.currentRows.length; i = 0;
while (!this.options.keepSelection && selectMultiSelectBox && i < this.currentRows.length)
{
selectMultiSelectBox = ($.inArray(this.currentRows[i++][this.identifier], this.selectedRows) !== -1);
}*/
//this.element.find("thead " + selectBoxSelector).prop("checked", selectMultiSelectBox); //PR1098 by fanbi
if(!selectAllFlag){
if(this.getTotalRowCount()== this.selectedRows.length){
this.element.find("thead " + selectBoxSelector).prop("indeterminate", false).prop("checked", true).val("all");
}else{
this.element.find("thead " + selectBoxSelector).prop("indeterminate", true).val("part");
}
} if (!this.options.multiSelect)
{
//this.element.find("tbody > tr " + selectBoxSelector + ":checked")
// .trigger("click" + namespace);
var lastId = this.selectedRows[this.selectedRows.length-1];
var checkboxes = this.element.find("tbody > tr " + selectBoxSelector + ":checked");
for (i = 0; i < checkboxes.length; i++) {
var $checkbox = $(checkboxes[i]);
if (lastId != $checkbox.val()) {
$checkbox.trigger("click" + namespace);
}
}
} for (i = 0; i < this.selectedRows.length; i++)
{
this.element.find("tbody > tr[data-row-id=\"" + this.selectedRows[i] + "\"]")
.addClass(this.options.css.selected)._bgAria("selected", "true")
.find(selectBoxSelector).prop("checked", true);
} this.element.trigger("selected" + namespace, [selectedRows]);
}
} return this;
}; /**
* Deselects rows by ids. Deselects all visible rows if no ids are provided.
* In server-side scenarios only visible rows are deselectable.
*
* @method deselect
* @param [rowsIds] {Array} An array of rows ids to deselect
* @chainable
**/
Grid.prototype.deselect = function(rowIds)
{
var deSelectAllFlag = false;
var selectBoxSelector = getCssSelector(this.options.css.selectBox); if (this.selection)
{
//rowIds = rowIds || this.currentRows.propValues(this.identifier); var id, i, pos,
deselectedRows = []; if(typeof rowIds == 'undefined'){//Deselect All
deSelectAllFlag = true;
this.element.find("thead " + selectBoxSelector).prop("indeterminate", false).prop("checked", false).val('none');
this.selectedRows= [];
deselectedRows = this.currentRows;
}else{//single deSelect
while (rowIds.length > 0)
{
id = rowIds.pop();
pos = $.inArray(id, this.selectedRows);
if (pos !== -1)
{
for (i = 0; i < this.currentRows.length; i++)
{
if (this.currentRows[i][this.identifier] === id)
{
deselectedRows.push(this.currentRows[i]);
this.selectedRows.splice(pos, 1);
break;
}
}
}
}
} if (deselectedRows.length > 0 ||deSelectAllFlag == true)
{
//var selectBoxSelector = getCssSelector(this.options.css.selectBox); //PR1098 by fanbi
if(!deSelectAllFlag){
if(0 != this.selectedRows.length){
this.element.find("thead " + selectBoxSelector).prop("indeterminate", true).val("part");
}else{
this.selectedRows = [];
this.element.find("thead " + selectBoxSelector).prop("indeterminate", false).prop("checked", false).val("none");;
}
}
//this.element.find("thead " + selectBoxSelector).prop("checked", false);
for (i = 0; i < deselectedRows.length; i++)
{
this.element.find("tbody > tr[data-row-id=\"" + deselectedRows[i][this.identifier] + "\"]")
.removeClass(this.options.css.selected)._bgAria("selected", "false")
.find(selectBoxSelector).prop("checked", false);
} this.element.trigger("deselected" + namespace, [deselectedRows]);
}
} return this;
}; //原renderTableHeader方法里的变量赋值.
//在这个版本,不管全勾选或者反勾选,勾选中的html代码都是 value ="all",就像是一个bug,这个value值没有什么作用
var selectBox = (this.options.multiSelect) ?
tpl.select.resolve(getParams.call(that, { type: "checkbox", value: "all" })) : "";
//修改成默认是part
var selectBox = (this.options.multiSelect) ?
tpl.select.resolve(getParams.call(that, { type: "checkbox", value: "part" })) : "";
html里的修改如下:
var checkBoxVal = "none;//增加变量来保存当前是全选,部分选还是全不全状态
checkBoxVal =$('#yourGridId .select-box').val();
ajax{
data:{
checkBoxVal :checkBoxVal
}
}
//从后台的数据返回的值赋值给checkBoxs,存储数据可以用文件,数据库或者Localstorage来储存,按需来处理
$("#yourGridId ").bootgrid("select", checkBoxs, "load");
java代码
String checkBoxVal = request.getParameter("checkBoxVal ");
if("all".equalsIgnoreCase(checkBoxVal )){
checkBox = Utils.getAllXXXIds();
//something to do
}else if("none".equalsIgnoreCase(checkBoxVal )){
checkBox = "";
//something to do
}
3. 使用修正后的版本的结果
部分选中的图标如下:

3.1 不管在那一页全选,当前页都会选上,而且当你切换到其它记录页时,checkbox也是全选的,表头的checkbox都是打钩√
3.2 不管在那一页全反选,当前页都会去掉√,而且当你切换到其它记录页时,checkbox也是全没选中的,表头的checkbox都是没选中状态
3.3 不管在哪一页部分选,而且当你切换到其它记录页时,表头的checkbox都是显示部分选的,不同浏览器差异如上图。
bootgrid修改成可以全勾选和全取消勾选操作的更多相关文章
- tree的所有节点都勾选上或者取消勾选
还有一个功能,就是让tree的所有节点都勾选上或者取消勾选,在api中找了一下有一个方法: check target 选中指定节点. 那我们只能是选中根节点后,实现全选. getRoot none 获 ...
- Ztree勾选节点后取消勾选其父子节点
前言: Ztree官方给的API可以设置勾选一个节点的同时勾选子节点或者父节点,也可以设置不影响父子节点,即将chkboxType设置为{"Y":"",&quo ...
- vue多级复杂列表展开/折叠,全选/分组全选实现
首先,来看下效果图 在线体验地址:https://hxkj.vip/demo/multipleList/.温馨提示,打开之后按F12,使用手机模式食用,口味更佳! 可以看出,这个列表有三种展现形式: ...
- ecshop彻底去版权把信息修改成自己的全教程
前台部分: 一.去掉头部title部分的ECSHOP演示站-Powered by ecshop 1.问题:“ECSHOP演示站”方法:在后台商店设置 – 商店标题修改2.问题:“ Powered by ...
- jquery checkbox勾选取消勾选的诡异问题
jquery checkbox勾选/取消勾选的诡异问题jquery checkbox勾选/取消勾选的诡异问题 <form> 你爱好的运动是?<input type=&q ...
- 老毛桃winpe优盘启动系统个性修改全攻略.(全)
博主从05年开始接触计算机,不能说是高手也算个老菜了,当时装系统还是用蕃茄花园的光盘安装系统,后来在学校管理机房,哪台电脑坏了就硬盘对拷. 时到今日,重启系统的方法五花八门,其中使用最广的莫过于PE优 ...
- 用 JS(JavaScript )实现多选、全选、反选
JS小例题 学习内容: 需求 总结: 学习内容: 需求 用 JavaScript 实现全选.反选.多选 实现代码 <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- jquery的checkbox 全选和全不选
今天写了一个checkbox的全选和全不选的功能: var check_all=function(){ if(this.checked){ //alert($(".adv_check_num ...
- 全选,全不选,反选的js实现
全选练习 ** 使用复选框上面一个属性判断是否选中 - checked属性 - checked=true:选中 ...
随机推荐
- A1067. Sort with Swap(0,*)
Given any permutation of the numbers {0, 1, 2,..., N-1}, it is easy to sort them in increasing order ...
- 关于jqGrid中GridUnload方法的困惑
首先 GridUnload 这个方法在 4.7.1 + 的版本中已经删除,直接把4.7.1中的grid.common.js合来用就行. GridUnload 这个方法是直接删除原来的table,重新生 ...
- apache 基本vhost配置 【目的及过程】
转: apache 基本vhost配置 2012年04月18日 09:39:28 chamtianjiao 阅读数:74075 经常使用Apache虚拟主机进行开发和测试,但每次需要配置虚拟主 ...
- OpenCV教程(43) harris角的检测(1)
计算机视觉中,我们经常要匹配两幅图像.匹配的的方式就是通过比较两幅图像中的公共特征,比如边,角,以及图像块(blob)等,来对两幅图像进行匹配. 相对于边,角更适合描述图像特征, ...
- 2018.10.2浪在ACM 集训队第三次测试赛
2018.10.26 浪在ACM 集训队第三次测试赛 今天是暴力场吗???????可怕 题目一览表 来源 考察知识点 完成时间 A 1275 珠心算测试 NOIP 普及组 2014 暴力??? 201 ...
- Logstash配置文件介绍
Logstash配置文件介绍 Logstash配置文件有两种,分别是pipeline配置文件和setting配置文件. Pipeline配置文件主要定义logstash使用的插件以及每个插件的设置,定 ...
- Nvivo
sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...
- .Net MVC个人笔记
关于转向的问题,目前知道的是Response.Redirect 和 location.href 我现在有两个controller,Home和Test <h2>this is Home< ...
- TCP的代码
视频已经发布,这里是所有的代码仅供参考. TCP服务器: MainWindow里面的代码: using System; using System.Collections.Generic; using ...
- 2、Python-流程控制
比较运算符 运算符 描述 示例 == 检查两个操作数的值是否相等,如果是则条件变为真. 如a=3,b=3则(a == b) 为 true. != 检查两个操作数的值是否相等,如果值不相等,则条件变为真 ...