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. A1071. Speech Patterns

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...

  2. 【codevs4919】线段树练习4

    题目大意:维护一个长度为 N 的序列,支持两种操作:区间加,区间查询有多少数是 7 的倍数. 题解:在每个线段树中维护一个权值数组 [0,6],由于个数显然支持区间可加性,因此可用线段树来维护. 代码 ...

  3. OpenCv练习

    // ConsoleApplication1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream&g ...

  4. redux与react-redux

    Redux是一个数据状态管理插件,论是移动端还是 pc 端,当你使用 React 或者 vue 开发组件化的 SPA 程序时, 组件之间共享信息是一个非常大的问题.在react开发中,使用 React ...

  5. 焦点监听事件FocusListener

    public class Demo extends JFrame { public Demo() { setBounds(100, 100, 200, 120); setDefaultCloseOpe ...

  6. JAVA分布式架构的演进

    系统架构演化历程-初始阶段架构 初始阶段 的小型系统 应用程序.数据库.文件等所有的资源都在一台服务器上通俗称为LAMP 特征:应用程序.数据库.文件等所有的资源都在一台服务器上. 描述:通常服务器操 ...

  7. curl模拟http发送get或post接口测试

    一.get请求 curl "http://www.baidu.com"  如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地 curl -i "http:// ...

  8. Nginx 学习笔记(五)nginx-vod-module 模块

    nginx-vod-module 一.编译 ./configure \ --user=www \ --group=www \ --prefix=/usr/local/openresty \ --wit ...

  9. 转---python os.exec*()家族函数的用法

    execl(file, arg0,arg1,...) 用参数列表arg0, arg1 等等执行文件 execv(file, arglist) 除了使用参数向量列表,其他的和execl()相同 exec ...

  10. 一、linux IO 编程---内存管理

    1.1 进程在虚拟空间中的布局 32位的操作系统虚拟空间的大小为 4GB,即每个进程在系统中分配的虚拟空间大小为4GB.这4GB的大小被分为了两个部分: 内核空间:1GB,内核起的进程 用户空间:3G ...