layui table 跨页记忆选择

layui 表格功能目前默认不支持跨页记忆选择

下面来实现layui table跨页记忆选择实现

基于layui版本 1.4.5

表格跨页通用方法

//表格分页复选框
layui.define([‘jquery‘, ‘table‘], function (exports) {
    var $ = layui.jquery
        , table = layui.table;

    //记录选中表格记录编号http://www.1994july.club/seo/?p=2797
    var checkedList = {};

    var tableCheckBoxUtil = {
        /*初始化分页设置*/
        init: function (settings) {
            var param = {
                //表格id
                gridId: ‘‘
                //表格lay-filter值
                , filterId: ‘‘
                //表格主键字段名
                , fieldName: ‘‘
            };
            $.extend(param, settings);

            //设置当前保存数据参数http://www.1994july.club/seo/?p=2800
            if (checkedList[param.gridId] == null) {
                checkedList[param.gridId] = [];
            }

            //监听选中行
            table.on(‘checkbox(‘ + param.filterId + ‘)‘, function (obj) {
                var type = obj.type;
                var checked = obj.checked;
                console.log(table);

                //当前页数据
                var currentPageData = table.cache[param.gridId];
                //当前选中数据
                var checkRowData = [];
                //当前取消选中的数据
                var cacelCheckedRowData = [];

                //debugger;
                //选中
                if (checked) {
                    checkRowData = table.checkStatus(param.gridId).data;
                }
                //取消选中
                else {
                    if (type == ‘all‘) {
                        cacelCheckedRowData = currentPageData;
                    }
                    else {
                        cacelCheckedRowData.push(obj.data);
                    }
                    //console.log(cacelCheckedRowData);
                }
                //debugger;
                //清除数据
                $.each(cacelCheckedRowData, function (index, item) {
                    var itemValue = item[param.fieldName];

                    checkedList[param.gridId] = checkedList[param.gridId].filter(function (fItem, fIndex) {
                        return fItem != itemValue;
                    })
                });

                //添加选中数据
                $.each(checkRowData, function (index, item) {
                    var itemValue = item[param.fieldName];
                    if (checkedList[param.gridId].indexOf(itemValue) < 0) {
                        checkedList[param.gridId].push(itemValue);
                    }
                });

                console.log(checkedList);
            });
        }
        //设置页面默认选中(在表格加载完成之后调用http://www.1994july.club/seo/?p=2803)
        , checkedDefault: function (settings) {
            var param = {
                //表格id
                gridId: ‘‘
                //表格主键字段名
                , fieldName: ‘‘
            };

            $.extend(param, settings);

            //当前页数据
            var currentPageData = table.cache[param.gridId];
            if (checkedList[param.gridId] != null && checkedList[param.gridId].length > 0) {
                $.each(currentPageData, function (index, item) {
                    var itemValue = item[param.fieldName];

                    if (checkedList[param.gridId].indexOf(itemValue) >= 0) {
                        //设置选中状态
                        item.LAY_CHECKED = true;

                        var rowIndex = item[‘LAY_TABLE_INDEX‘];
                        updateRowCheckStatus(param.gridId, ‘tr[data-index=‘ + rowIndex + ‘] input[type="checkbox"]‘);
                    }
                });
            }
            //判断当前页是否全选
            var currentPageCheckedAll = table.checkStatus(param.gridId).isAll;
            if (currentPageCheckedAll) {
                updateRowCheckStatus(param.gridId, ‘thead tr input[type="checkbox"]‘);
            }
            //console.log(table.cache[param.gridId]);
        }
        //获取当前获取的所有集合值
        , getValue: function (settings) {
            var param = {
                //表格id
                gridId: ‘‘
            };
            $.extend(param, settings);

            return checkedList[param.gridId];
        }
        //设置选中的id(一般在编辑时候调用初始化选中值)
        , setIds: function (settings) {
            var param = {
                gridId: ‘‘
                //数据集合
                , ids: []
            };
            $.extend(param, settings);

            checkedList[param.gridId] = [];
            $.each(param.ids, function (index, item) {
                checkedList[param.gridId].push(parseInt(item));
            });
        }
    };

    var updateRowCheckStatus = function (gridId, ele) {
        var layTableView = $(‘.layui-table-view‘);
        //一个页面多个表格,这里防止更新表格错误http://www.1994july.club/seo/?p=2805
        $.each(layTableView, function (index, item) {
            if ($(item).attr(‘lay-id‘) == gridId) {
                $(item).find(ele).prop(‘checked‘, true);
                $(item).find(ele).next().addClass(‘layui-form-checked‘);
            }
        });
    }
    //输出
    exports(‘tableCheckBoxUtil‘, tableCheckBoxUtil);
});

Html

@{
    ViewBag.Title = "View";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@using MKAdmin.Web.App_Start

@section Styles {
    @Styles.Render(PageCssFilesConfig.CheckBoxTableIndex)
}

<div class="page-content-wrap wx_p20">
    <div class="zf-wx-index-main">
        <form class="layui-form cc-padding-top pr" action="">
            <div class="layui-input-block zf-margin-left">
                <input type="text" name="title"
                       id="txt_BasicIndex_Name" placeholder="请输入姓名"
                       class="layui-input zf-fixed-length zf_h25 w_200">
                <a class="layui-btn layui-btn-normal btn_color_confirm zf_h25"
                   id="btn_CheckBoxSearch" data-type="reload">搜索</a>

                <a class="layui-btn layui-btn-normal btn_color_confirm zf_h25"
                   id="btn_CheckBoxTableGetValue" data-type="reload">获取选中编号</a>
            </div>
        </form>

    </div>
    <table id="grid_CheckBoxTable" lay-filter="ft_CheckBoxTable"></table>
</div>

@section Scripts {
    @Scripts.Render(PageJsFilesConfig.CheckBoxTableIndex)
}

Js调用

//表格管理 - 基本表格http://www.1994july.club/seo/?p=2807
layui.use([‘jquery‘, ‘table‘, ‘tableCheckBoxUtil‘], function () {
        var table = layui.table,
            $ = layui.jquery,
            tableCheckBoxUtil = layui.tableCheckBoxUtil;

        //表格渲染
        table.render({
            id: ‘cn_GridCheckBoxTable‘
            , elem: ‘#grid_CheckBoxTable‘
            , method: ‘post‘
            , url: ‘/table/basictable/list‘
            //启动分页
            , page: true
            , height: ‘full-100‘
            , where: {
                name: ‘‘
            }
            , cols: [[
                { checkbox: true }
                , { field: ‘EmployeeId‘, width: 80, title: ‘编号‘ }
                , { field: ‘EmployeeName‘, width: 150, title: ‘姓名‘ }
                , { field: ‘EmployeeAge‘, title: ‘年龄‘, width: 100 }
                , { field: ‘Education‘, width: 180, title: ‘学历‘ }
            ]]
            , done: function (data) {
                //2.初始化表格行选中状态
                tableCheckBoxUtil.checkedDefault({
                    gridId: ‘cn_GridCheckBoxTable‘
                    , fieldName: ‘EmployeeId‘
                });
            }
        });

        //1.初始化分页设置
        tableCheckBoxUtil.init({
            gridId: ‘cn_GridCheckBoxTable‘
            , filterId: ‘ft_CheckBoxTable‘
            , fieldName: ‘EmployeeId‘
        });

        //3.获取选中编号
        $("#btn_CheckBoxTableGetValue").on(‘click‘, function () {
            var selectedData = tableCheckBoxUtil.getValue({
                gridId: ‘cn_GridCheckBoxTable‘
            });

            alert(selectedData);
        });

    });

预览:

http://www.1994july.club/seo/?p=2812

原文地址:https://www.cnblogs.com/1994jinnan/p/12057080.html

时间: 2024-08-30 11:13:55

layui table 跨页记忆选择的相关文章

网页打印A4纸-----表格在跨页时自动换页打印的实现 (转)

在最近所做的一个项目中,需要通过网页来打印不少的表单,但是又不想每个打印页签各占用一个页面,这样就需要生存很多不同的冗余页面,为了减少冗余,所有的表单通过jquery的页签tab来实现的. 一 :基本打印的实现: 1:tab页签在切换时的change事件中,记住每个页签的index, 2:在点击打印按钮时,根据所记住的index,由页签的id("tab名+index"组成),获取当前tab页签下所有的html; 3: 将获取到的需要打印页签的html赋值给document.body.i

Candence下对“跨页连接器(off-page connector)”进行批量重命名的方法

parts.ports.alias等等均可以在"属性编辑器(Property Editor)"中进行查看编辑,并通过复制到Excel等表格软件来进行批量修改.之后再粘贴回去的方法进行批量编辑.但是"跨页连接器(off-page connector)"无法在Property Editor中进行查看和编辑. 选择多个off-page connector之后,通过"Ctrl+E"或者"右键-->Edit Properties"

A在SP.NET跨页多选

在ASP.NET跨页多选 本文介绍怎样在ASP.NET中实现多页面选择的问题.其详细思路非常easy:用隐藏的INPUT记住每次选择的项目,在进行数据绑定时.检查保存的值,再在DataGrid中进行选中显示. 以下时完整的代码和样例: 查看样例 SelectMultiPages.aspx <%@ Page EnableViewState="true" CodeBehind="SelectMultiPages.aspx.cs" Language="c#

layui table数据渲染页面+筛选医生+在筛选日期一条龙2

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <title>预约

layui table表格详解

上次做table有些东西 忘记了 这次当作来个分析总结一下  跟大家共同学习 闲话不多说 直接上例子   代码: <form id="form1" runat="server"> <div> <table id="demo" lay-filter="test"></table> </div> </form> <script> //*******

关于云主机Thinkphp框架Session跨页失效的问题

在网站部署到云主机之后,前台一直能够正常显示,后台确登录不上去,验证码也无法显示,研究半天,才确定是Session跨页传递失效的问题.找网上各种解决方法,都是关于Php.ini文件的设置,可又解决不了问题,于是狠下心来研究,最后确定是Session路径的问题.由于Thinkphp的Session默认不是保存在网站目录下,导致Session无效的问题,最后更改Session的保存路径,成功解决了问题. 具体解决办法: 1.在index.php中定义session保存路径: define('ROOT

layui table 渲染完成后,怎样拿到表个里的所有数据

var url = layui.table.cache.test[index]["url"]; $("#show_pic").attr("src", url); 其中 test 是 table 的 id 原文地址:https://www.cnblogs.com/mentiantian/p/10218074.html

layui table 列宽百分比显示

var layer = layui.layer, form = layui.form, table = layui.table; var $ = layui.$; /*select gysmc,zyzw ,xm ,sjhm ,bgshm ,emilyx , zgsrgzwsj ,zggsgl ,cast(gysxxid as int) as gysxxid, pc, gysryxxMdataID as ID,sjbs from gysryxxMdata*/ var tableInit = tab

layui table指定某一行样式

想指定layui table中某一行的样式,找了这个资源可行.转自: https://blog.csdn.net/weixin_44729896/article/details/100524824 table.render({ elem: "#test", // id: "table_cell_data", url: "http://localhost:3000/all", toolbar: "#toolbarDemo", /