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修改成可以全勾选和全取消勾选操作的更多相关文章

  1. tree的所有节点都勾选上或者取消勾选

    还有一个功能,就是让tree的所有节点都勾选上或者取消勾选,在api中找了一下有一个方法: check target 选中指定节点. 那我们只能是选中根节点后,实现全选. getRoot none 获 ...

  2. Ztree勾选节点后取消勾选其父子节点

    前言: Ztree官方给的API可以设置勾选一个节点的同时勾选子节点或者父节点,也可以设置不影响父子节点,即将chkboxType设置为{"Y":"",&quo ...

  3. vue多级复杂列表展开/折叠,全选/分组全选实现

    首先,来看下效果图 在线体验地址:https://hxkj.vip/demo/multipleList/.温馨提示,打开之后按F12,使用手机模式食用,口味更佳! 可以看出,这个列表有三种展现形式: ...

  4. ecshop彻底去版权把信息修改成自己的全教程

    前台部分: 一.去掉头部title部分的ECSHOP演示站-Powered by ecshop 1.问题:“ECSHOP演示站”方法:在后台商店设置 – 商店标题修改2.问题:“ Powered by ...

  5. jquery checkbox勾选取消勾选的诡异问题

    jquery checkbox勾选/取消勾选的诡异问题jquery checkbox勾选/取消勾选的诡异问题 <form>        你爱好的运动是?<input type=&q ...

  6. 老毛桃winpe优盘启动系统个性修改全攻略.(全)

    博主从05年开始接触计算机,不能说是高手也算个老菜了,当时装系统还是用蕃茄花园的光盘安装系统,后来在学校管理机房,哪台电脑坏了就硬盘对拷. 时到今日,重启系统的方法五花八门,其中使用最广的莫过于PE优 ...

  7. 用 JS(JavaScript )实现多选、全选、反选

    JS小例题 学习内容: 需求 总结: 学习内容: 需求 用 JavaScript 实现全选.反选.多选 实现代码 <!DOCTYPE html PUBLIC "-//W3C//DTD ...

  8. jquery的checkbox 全选和全不选

    今天写了一个checkbox的全选和全不选的功能: var check_all=function(){ if(this.checked){ //alert($(".adv_check_num ...

  9. 全选,全不选,反选的js实现

    全选练习       ** 使用复选框上面一个属性判断是否选中                   - checked属性                   - checked=true:选中    ...

随机推荐

  1. 【洛谷P1072】Hankson 的趣味题

    题目大意:给定四个数字 a,b,c,d,求满足 \(gcd(a,x)=b,lcm(c,x)=d\) 的 x 的个数. 题解: 解法1:根据 lcm 的性质,x 一定为 d 的约数.因此,直接枚举 d ...

  2. [luogu2822][组合数问题]

    题目链接 题解: 对于上面和下面的式子进行分解质因数,然后看看上面的质因数个数减去下面的质因数个数能不能达到k的质因数的要求即可. 分解质因数的时候用对于阶乘分解质因数的常用方法:比如要求1999!中 ...

  3. context configure and clock schedule

    每个窗口都有自己的context,这里演示怎么配置context以及如何实现定时器...... #-*- coding:gbk -*- import pyglet platform=pyglet.wi ...

  4. Python中if-else的多种写法

    a, b= 1, 2 将a和b两个变量中的最大值赋值给c (1)常规写法 if a>b:     c = a else:     c = b   (2)表达式 c = a if a>b e ...

  5. 我的 $OI$, 退役前写点东西

    离 \(NOIp2018\) 还有五天, 总想写点什么 马上退役了啊 是什么时候喜欢上信息技术的呢 记不清了, 很小的时候就喜欢捣鼓关于电脑的东西 当时也不知道有算法这种东西 只是知道有黑客 巨 j8 ...

  6. ElasticSearch 例子

    ElasticSearch是一个接近实时的搜索平台,它利用Lucese进行文档索引. 本文会写个可以运行的简单例子,方便大家上手,日后深入了解. 需要引入maven依赖 <dependency& ...

  7. Elasticsearch之中文分词器插件es-ik的自定义词库

    它在哪里呢? 非常重要! [hadoop@HadoopMaster custom]$ pwd/home/hadoop/app/elasticsearch-2.4.3/plugins/ik/config ...

  8. python 内建函数专题

    all 用来控制 import , 甚至可以改变 _private 为 public enter , exit 用于上下文管理器 iter 用于迭代器 repr 给计算机读, str ==> s ...

  9. MIPS指令学习二

    1.MIPS寻址方式 MIPS架构的寻址模式有寄存器寻址.立即数寻址.寄存器相对寻址和PC相对寻址4种,其中寄存器相对寻址.PC相对寻址介绍如下: 1.1.寄存器相对寻址 这种寻址模式主要被加载/存储 ...

  10. Linux - openssl 加密

    openssl rand 15 -base64 # 口令生成 openssl sha1 filename # 哈希算法校验文件 openssl md5 filename # MD5校验文件 opens ...