bootstrap-table按住Shift多选设计思路-优化

在网上找到bootstra-table按住shift实现多选的思路后,进行改进,实现批量选中/批量取消选中,批量选中后取消部分选中并保留剩下选中项,向上/向下批量操作。
参考地址:https://blog.youkuaiyun.com/qq_36360308/article/details/115604059

// 按住shift一次选中多个数据行实现批量选中/批量取消选中
    function shiftMultiSelectTableRows(){
        // shift多选
        let mode = ""; // 模式:批量选中/批量取消选中
        let type = ""; // 当前选中行的操作类型:选中/取消选中
        let lastStart = null; // 上一次记录的起点
        let lastEnd = null; // 上一次记录的终点
        let nowEnd = null; // 批量选中后部分取消时,记录的实际选中项的终点
        let flag = true;

        $(document).on("change", ".bs-checkbox input", function(e) {
            if(mode == ""){
                if($(this).is(':checked') && flag) mode = "batchCheck";
                if(!$(this).is(':checked') && flag) mode = "batchUncheck";
            }

            if(mode == "batchCheck"){
                if ($(this).is(':checked') && flag){
                    type = "check";
                    if (lastEnd != null) lastStart = lastEnd;
                    lastEnd = $(this).attr("data-index");

                }else if (!$(this).is(':checked')){
                    type = "uncheck";
                    nowEnd = $(this).attr("data-index");
                }

            }else if(mode == "batchUncheck"){
                if (!$(this).is(':checked') && flag){
                    type = "uncheck";
                    if (lastEnd != null) lastStart = lastEnd;
                    lastEnd = $(this).attr("data-index");
                }
            }
        });

        //让上面方法先执行完
        $(document).mousedown(function(e) {
            if (e.shiftKey) {
                setTimeout(function (){
                    if(mode == "batchCheck"){
                        if(type == "check"){
                            if (lastStart != null && lastEnd != null){
                                let agentNum = null; // 中间变量,支持向上/向下批量选择
                                if(lastStart > lastEnd){
                                    agentNum = lastStart;
                                    lastStart = lastEnd;
                                    lastEnd = agentNum;
                                }
                                flag = false;
                                for(let i = parseInt(lastStart)+1;i<parseInt(lastEnd);i++){
                                    if(!$("input[data-index='"+i+"']").is(":checked")) $("input[data-index='"+i+"']").trigger("click");
                                }
                                flag = true;
                            }
                        }else if(type == "uncheck"){
                            if (nowEnd !=null && lastEnd != null){
                                flag = false;
                                for(let i = parseInt(nowEnd)+1;i<=parseInt(lastEnd);i++){
                                    if($("input[data-index='"+i+"']").is(":checked")) $("input[data-index='"+i+"']").trigger("click");
                                }
                                flag = true;
                            }
                            lastEnd = null;
                        }
                    }else if(mode == "batchUncheck"){
                        if(type == "uncheck"){
                            if (lastStart != null && lastEnd != null){
                                let agentNum = null; // 中间变量,支持向上/向下批量选择
                                if(lastStart > lastEnd){
                                    agentNum = lastStart;
                                    lastStart = lastEnd;
                                    lastEnd = agentNum;
                                }
                                flag = false;
                                for(let i = parseInt(lastStart)+1;i<parseInt(lastEnd);i++){
                                    if($("input[data-index='"+i+"']").is(":checked")) $("input[data-index='"+i+"']").trigger("click");
                                }
                                flag = true;
                            }
                        }
                    }
                },250)
            }
        });

        $(document).mouseup(function (e) {
            if(!e.shiftKey){
                // 还原
                lastStart = null;
                lastEnd = null;
                nowEnd = null;
                type = "";
                mode = "";
                flag = true;
            }
        })
    }
### 实现 Bootstrap-Table 功能 为了在 `bootstrap-table` 中实现功能,可以利用插件自带的选择框特性。具体来说,在初始化表格时设置特定的数据属性来开启模式。 #### HTML 结构配置 定义表格结构并加入必要的数据属性以支持操作: ```html <table id="table" data-toggle="table" data-url="/data.json" <!-- 数据源 --> data-pagination="true" <!-- 启用分页 --> data-side-pagination="server" <!-- 服务器端分页 --> data-click-to-select="true" <!-- 单击行即--> data-show-columns="true"> <!-- 显示列控制器 --> <thead> <tr> <th data-field="state" data-checkbox="true"></th> <!-- 添加此列为复--> <th data-field="id">ID</th> <th data-field="name">Name</th> <th data-field="price">Price</th> </tr> </thead> </table> ``` 上述代码片段展示了如何创建一个表单[^1]。注意 `<th>` 标签内的 `data-checkbox=true` 属性用于指示该列表头应呈现为选择框而不是常规文本字段。 #### JavaScript 配置项 除了HTML中的静态声明外,还可以通过JavaScript动态调整一些行为参数或者监听事件处理逻辑: ```javascript $('#table').on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function () { console.log($('#table').bootstrapTable('getSelections')); }); ``` 这段脚本绑定了几个重要的勾状态改变事件,并打印当前被中的所有行对象到控制台。这有助于开发者进一步开发基于用户交互的功能扩展[^2]。 #### 自定义样式与增强体验 对于更复杂的场景,可能还需要自定义CSS类名以及额外的JS函数来进行界面美化和服务请求优化等工作。例如,可以通过修改 `.select-item:checked + .text { color:red }` 这样的方式突出显示已项目;也可以结合AJAX技术实现实时更新服务端资源池等功能[^3]。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值