zoukankan      html  css  js  c++  java
  • 【JavaScript】bootstrap-table-treegrid 异步加载实现

    插件修改自 jquery.treegrid.extension.js,新增按需加载子节点的功能(即 lazyload: true 模式),需配合 TreeGrid plugin for jQuery 使用

    改造思路如下:
    1 > 首次只加载根节点
    2 > 在节点上绑定点击事件,查询后台数据,动态加载子节点(插入到点击行的后面紧邻的位置,同样也绑定点击事件)
    3 > 重新渲染节点图标,并缓存展开节点至cookie(treegrid save state 功能,依赖 jquery.cookie.js)

    ; (function($) {
        "use strict";
    
        $.fn.bootstrapTreeTable = function(options, param) {
            // 如果是调用方法
            if (typeof options == 'string') {
                return $.fn.bootstrapTreeTable.methods[options](this, param);
            }
    
            // 如果是初始化组件
            options = $.extend({}, $.fn.bootstrapTreeTable.defaults, options || {});
            // 是否有radio或checkbox
            var hasSelectItem = false;
            var target = $(this);
    
            // 在外层包装一下div,样式用的bootstrap-table的
            var _main_div = target.parent('.fixed-table-container');
            if (_main_div.length == 0) {
                _main_div = $("<div class='fixed-table-container'></div>");
                target.before(_main_div);
                _main_div.append(target);
            }
            target.addClass("table table-hover treegrid-table");
            if (options.bordered) {
                target.addClass('table-bordered');
            }
            if (options.striped) {
                target.addClass('table-striped');
            }
            // 工具条在外层包装一下div,样式用的bootstrap-table的
            if (options.toolbar) {
                var _tool_div = $("<div class='fixed-table-toolbar'></div>");
                var _tool_left_div = $("<div class='bs-bars pull-left'></div>");
                var _tool_bar = $(options.toolbar);
                if (_tool_bar.length == 0) {
                    _tool_div.css('display', 'none');
                } else {
                    _tool_left_div.append(_tool_bar);
                }
                _tool_div.append(_tool_left_div);
                _main_div.before(_tool_div);
            }
    
            // 得到根节点
            target.getRootNodes = function(data) {
                // 指定Root节点值
                var _root = options.rootCodeValue ? options.rootCodeValue: null;
                var result = [];
                $.each(data, function(index, item) {
                    // 这里兼容几种常见Root节点写法
                    // 默认的几种判断
                    var _defaultRootFlag = item[options.parentCode] == '0' 
                                        || item[options.parentCode] == 0 
                                        || item[options.parentCode] == null 
                                        || item[options.parentCode] == '';
                    if (!item[options.parentCode] || (_root ? (item[options.parentCode] == options.rootCodeValue) : _defaultRootFlag)) {
                        result.push(item);
                    }
                    // 添加一个默认属性,用来判断当前节点有没有被显示
                    item.isShow = false;
                });
                return result;
            };
    
            var j = 0;
            // 获取子节点, 并且设置子节点
            target.getChildNodes = function(data, parentNode, parentIndex, tbody, isRecursive) {
                $.each(data, function(i, item) {
                    if (item[options.parentCode] == parentNode[options.code]) {
                        var tr = $('<tr></tr>');
                        // var nowParentIndex = (parentIndex + (j++) + 1);
                        var nowParentIndex = item[options.code];
                        tr.addClass('treegrid-' + nowParentIndex);
                        tr.addClass('treegrid-parent-' + parentIndex);
                        target.renderRow(tr, item, nowParentIndex);
                        item.isShow = true;
                        tbody.append(tr);
                        if (typeof isRecursive == 'boolean' && isRecursive) {
                            target.getChildNodes(data, item, nowParentIndex, tbody, isRecursive);
                        }
                    }
                });
            };
    
            // 绘制行
            target.renderRow = function(tr, item, parentIndex) {
                if (options.lazyload) {
                    tr.attr('data-id', parentIndex);
                }
                $.each(options.columns, function(index, column) {
                    // 判断有没有选择列
                    if (index == 0 && column.field == 'selectItem') {
                        hasSelectItem = true;
                        var td = $('<td style="text-align:center;36px"></td>');
                        if (column.radio) {
                            var _ipt = $('<input name="select_item" type="radio" value="' + item[options.id] + '"></input>');
                            td.append(_ipt);
                        }
                        if (column.checkbox) {
                            var _ipt = $('<input name="select_item" type="checkbox" value="' + item[options.id] + '"></input>');
                            td.append(_ipt);
                        }
                        tr.append(td);
                    } else {
                        var td = $('<td style="' + ((column.width) ? ('' + column.width) : '') + '"></td>');
                        // 增加formatter渲染
                        if (column.formatter) {
                            td.html(column.formatter.call(this, item, index));
                        } else {
                            td.text(item[column.field]);
                        }
                        tr.append(td);
                    }
                });
            };
    
            // 加载数据
            target.load = function(params) {
                // 加载数据前先清空
                target.html("");
                // 构造表头
                var thr = $('<tr></tr>');
                $.each(options.columns, function(i, item) {
                    var th = null;
                    // 判断有没有选择列
                    if (i == 0 && item.field == 'selectItem') {
                        hasSelectItem = true;
                        th = $('<th style="text-align:' + item.valign + ';36px"></th>');
                    } else {
                        th = $('<th style="text-align:' + item.valign + ';padding:10px;' + ((item.width) ? ('' + item.width) : '') + '"></th>');
                    }
                    th.text(item.title);
                    thr.append(th);
                });
                var thead = $('<thead class="treegrid-thead"></thead>');
                thead.append(thr);
                target.append(thead);
                // 构造表体
                var tbody = $('<tbody class="treegrid-tbody"></tbody>');
                target.append(tbody);
                // 添加加载loading
                var _loading = '<tr><td colspan="' + options.columns.length + '"><div style="display: block;text-align: center;">正在努力地加载数据中,请稍候……</div></td></tr>';
                tbody.html(_loading);
                // 默认高度
                if (options.height) {
                    tbody.css("height", options.height);
                }
                $.ajax({
                    type: options.type,
                    url: options.url,
                    data: params ? params: options.ajaxParams,
                    dataType: "JSON",
                    success: function(data, textStatus, jqXHR) {
                        // 加载完数据先清空
                        tbody.html("");
                        if (!data || data.length <= 0) {
                            var _empty = '<tr><td colspan="' + options.columns.length + '"><div style="display: block;text-align: center;">没有记录</div></td></tr>';
                            tbody.html(_empty);
                            return;
                        }
                        var rootNode = target.getRootNodes(data);
                        $.each(rootNode, function(i, item) {
                            var tr = $('<tr></tr>');
                            // tr.addClass('treegrid-' + (j + "_" + i));
                            tr.addClass('treegrid-' + item[options.code]);
                            // target.renderRow(tr, item, (j + "_" + i));
                            target.renderRow(tr, item, item[options.code]);
                            item.isShow = true;
                            tbody.append(tr);
                            if (!options.lazyload) {
                                // target.getChildNodes(data, item, (j + "_" + i), tbody, true);
                                target.getChildNodes(data, item, item[options.code], tbody, true);
                            }
                        });
                        // 下边的操作主要是为了查询时让一些没有根节点的节点显示
                        $.each(data, function(i, item) {
                            if (!item.isShow) {
                                var tr = $('<tr></tr>');
                                // tr.addClass('treegrid-' + (j + "_" + i));
                                tr.addClass('treegrid-' + item[options.code]);
                                target.renderRow(tr, item);
                                tbody.append(tr);
                            }
                        });
                        
                        target.append(tbody);
                        // 初始化treegrid
                        target.treegrid({
                            // 如果有radio或checkbox默认第二列层级显示,当前是在用户未设置的提前下
                            'treeColumn': options.expandColumn ? options.expandColumn: (hasSelectItem ? 1 : 0),
                            'expanderExpandedClass': options.expanderExpandedClass,
                            'expanderCollapsedClass': options.expanderCollapsedClass,
                            'initialState': options.expandAll ? 'expanded': 'collapsed',
                            'saveState': options.lazyload
                        });
                        if (!options.expandAll && !options.lazyload) {
                            target.treegrid('collapseAll');
                        }
    
                        target.find('tbody').on('click', 'tr', function(e) {
                            var e = e || window.event;
                            var _self = $(this);
                            // 行点击选中
                            if (hasSelectItem) {
                                var _ipt = _self.find("input[name='select_item']");
                                if (_ipt.attr("type") == "radio") {
                                    _ipt.prop('checked', true);
                                    target.find('tr.treegrid-selected').removeClass("treegrid-selected");
                                    _self.addClass("treegrid-selected");
                                } else {
                                    if (_ipt.prop('checked')) {
                                        _ipt.prop('checked', false);
                                        _self.removeClass("treegrid-selected");
                                    } else {
                                        _ipt.prop('checked', true);
                                        _self.addClass("treegrid-selected");
                                    }
                                }
                            }
                            // 子节点加载
                            if (options.lazyload) {
                                if (_self.attr('data-loaded') != 'true') {
                                    if ($(e.target).hasClass('treegrid-expander')) {
                                        var _params = {};
                                        // _params[options.parentCode] = _self.attr('class').split(" ")[0].split("-")[1];
                                        _params[options.parentCode] = _self.attr('data-id');
                                        target.loadChilds(_params);
                                    }
                                }
                            }
                        });
    
                        if (options.lazyload) {
                            target.repainExpends();
                        }
                    },
                    error: function(xhr, status, error) {
                        var _errorMsg = '<tr><td colspan="' + options.columns.length + '"><div style="display: block;text-align: center;">' + xhr.responseText + '</div></td></tr>';
                        tbody.html(_errorMsg);
                        debugger;
                    },
                });
            }
    
            // 重新绘制节点图标
            target.repainExpends = function() {
                var trExpends = target.find('tbody').find('tr');
                $.each(trExpends,
                function(index, item) {
                    var _item = $(item);
                    if (_item.attr('data-isleaf') != 'true') {
                        if (_item.attr('data-loaded') != 'true') {
                            _item.find("span.treegrid-expander").addClass(options.expanderCollapsedClass);
                        }
                    } else {
                        _item.find("span.treegrid-expander").removeClass(options.expanderCollapsedClass);
                    }
                });
            }
    
            // 动态添加子节点
            target.renderChildRows = function(data, parentNode, parentIndex, tbody) {
                for (var x = data.length - 1; x >= 0; x--) {
                    var item = data[x];
                    if (item[options.parentCode] == parentNode[options.code]) {
                        var tr = $('<tr></tr>');
                        // var nowParentIndex = (parentIndex + x);
                        var nowParentIndex = item[options.code];
                        tr.addClass('treegrid-' + nowParentIndex);
                        tr.addClass('treegrid-parent-' + parentIndex);
                        target.renderRow(tr, item, nowParentIndex);
                        item.isShow = true;
                        tbody.after(tr);
                    }
                }
            };
    
            // 加载子节点数据
            target.loadChilds = function(params) {
                $.ajax({
                    type: options.type,
                    url: options.url,
                    data: params ? params: options.ajaxParams,
                    dataType: "JSON",
                    success: function(data, textStatus, jqXHR) {
                        var _tr = target.find(".treegrid-" + params[options.parentCode]);
                        _tr.attr('data-loaded', 'true');
                        if (data == null || data.length == 0) {
                            _tr.attr('data-isleaf', 'true');
                        } else {
                            var _pid = {};
                            _pid[options.code] = params[options.parentCode];
                            target.renderChildRows(data, _pid, params[options.parentCode], _tr);
                            target.treegrid({
                                // 如果有radio或checkbox默认第二列层级显示,当前是在用户未设置的提前下
                                'treeColumn': options.expandColumn ? options.expandColumn: (hasSelectItem ? 1 : 0),
                                'expanderExpandedClass': options.expanderExpandedClass,
                                'expanderCollapsedClass': options.expanderCollapsedClass,
                                'initialState': options.expandAll ? 'expanded': 'collapsed',
                                'saveState': options.lazyload
                            });
                            // 展开节点
                            _tr.treegrid('expand');
                        }
                        target.repainExpends();
                    },
                    error: function(xhr, status, error) {
                        var _errorMsg = '<tr><td colspan="' + options.columns.length + '"><div style="display: block;text-align: center;">' + xhr.responseText + '</div></td></tr>';
                        tbody.html(_errorMsg);
                        debugger;
                    }
                });
            }
    
            if (options.url) {
                target.load();
            } else {
                // 也可以通过defaults里面的data属性通过传递一个数据集合进来对组件进行初始化....有兴趣可以自己实现,思路和上述类似
            }
    
            return target;
        };
    
        // 组件方法封装........
        $.fn.bootstrapTreeTable.methods = {
            // 返回选中记录的id(返回的id由配置中的id属性指定)
            // 为了兼容bootstrap-table的写法,统一返回数组,这里只返回了指定的id
            getSelections: function(target, data) {
                // 所有被选中的记录input
                var _ipt = target.find("tbody").find("tr").find("input[name='select_item']:checked");
                var chk_value = [];
                // 如果是radio
                if (_ipt.attr("type") == "radio") {
                    chk_value.push({
                        id: _ipt.val()
                    });
                } else {
                    _ipt.each(function(_i, _item) {
                        chk_value.push({
                            id: $(_item).val()
                        });
                    });
                }
                return chk_value;
            },
            // 刷新记录
            refresh: function(target, params) {
                if (params) {
                    target.load(params);
                } else {
                    target.load();
                }
            },
            // 重置表格视图
            resetView: function(target, params) {
                if (params) {
                    target.find("tbody").css("height", params);
                } else {
                    target.find("tbody").css("height", options.height);
                }
            }
            // 组件的其他方法也可以进行类似封装........
        };
    
        $.fn.bootstrapTreeTable.defaults = {
            id: 'menuId', // 选取记录返回的值
            code: 'menuId', // 用于设置父子关系
            parentCode: 'parentId', // 用于设置父子关系
            rootCodeValue: null, //设置根节点code值----可指定根节点,默认为null,"",0,"0"
            data: [], // 构造table的数据集合
            type: "GET", // 请求数据的ajax类型
            url: null, // 请求数据的ajax的url
            ajaxParams: {}, // 请求数据的ajax的data属性
            expandColumn: null, // 在哪一列上面显示展开按钮
            expandAll: false, // 是否全部展开
            striped: false, // 是否各行渐变色
            bordered: false, //是否显示边框
            lazyload: false, //是否延迟加载
            columns: [],
            toolbar: null, //顶部工具条
            height: 0,
            expanderExpandedClass: 'glyphicon glyphicon-chevron-down', // 展开的按钮的图标
            expanderCollapsedClass: 'glyphicon glyphicon-chevron-right' // 缩起的按钮的图标
        };
    })(jQuery);
    
    ; (function($) {
        "use strict";
    
        var TreeTable = function(tableId, url, columns) {
            this.btInstance = null; // jquery和bootstrapTreeTable绑定的对象
            this.bstableId = tableId;
            this.url = url;
            this.method = "GET";
            this.columns = columns;
            this.data = {}; // ajax的参数
            this.expandColumn = null; // 展开显示的列 
            this.id = 'menuId'; // 选取记录返回的值
            this.code = 'menuId'; // 用于设置父子关系
            this.parentCode = 'parentId'; // 用于设置父子关系
            this.expandAll = false; // 是否默认全部展开
            this.lazyload = false; // 是否延迟加载
            this.striped = true; // 是否各行渐变色
            this.bordered = true; // 是否显示边框
            this.toolbarId = null;
            this.height = 440;
        };
    
        TreeTable.prototype = {
            /**
             * 初始化bootstrap table
             */
            init: function() {
                var tableId = this.bstableId;
                this.btInstance = $('#' + tableId).bootstrapTreeTable({
                    id: this.id, // 选取记录返回的值
                    code: this.code, // 用于设置父子关系
                    parentCode: this.parentCode, // 用于设置父子关系
                    rootCodeValue: this.rootCodeValue, //设置根节点code值----可指定根节点,默认为null,"",0,"0"
                    type: this.method, //请求数据的ajax类型
                    url: this.url, //请求数据的ajax的url
                    ajaxParams: this.data, //请求数据的ajax的data属性
                    expandColumn: this.expandColumn, //在哪一列上面显示展开按钮,从0开始
                    striped: this.striped, //是否各行渐变色
                    bordered: this.bordered, //是否显示边框
                    expandAll: this.expandAll, //是否全部展开
                    lazyload: this.lazyload, //是否延迟加载
                    columns: this.columns, //列数组
                    toolbar: this.toolbarId ? ('#' + this.toolbarId) : null, //顶部工具条
                    height: this.height
                });
                return this;
            },
            /**
             * 设置顶部工具条
             */
            setToolbarId: function(toolbarId) {
                this.toolbar = toolbarId;
            },
            /**
             * 设置在哪一列上面显示展开按钮,从0开始
             */
            setExpandColumn: function(expandColumn) {
                this.expandColumn = expandColumn;
            },
            /**
             * 设置记录返回的id值
             */
            setIdField: function(id) {
                this.id = id;
            },
            /**
             * 设置记录分级的字段
             */
            setCodeField: function(code) {
                this.code = code;
            },
            /**
             * 设置记录分级的父级字段
             */
            setParentCodeField: function(parentCode) {
                this.parentCode = parentCode;
            },
            /**
             * 设置根节点code值----可指定根节点,默认为null,"",0,"0"
             */
            setRootCodeValue: function(rootCodeValue) {
                this.rootCodeValue = rootCodeValue;
            },
            /**
             * 设置是否默认全部展开
             */
            setExpandAll: function(expandAll) {
                this.expandAll = expandAll;
            },
            /**
             * 设置是否延迟加载
             */
            setLazyload: function(lazyload) {
                this.lazyload = lazyload;
            },
            /**
             * 设置表格高度
             */
            setHeight: function(height) {
                this.height = height;
            },
            /**
             * 设置ajax post请求时候附带的参数
             */
            set: function(key, value) {
                if (typeof key == "object") {
                    for (var i in key) {
                        if (typeof i == "function") {
                            continue;
                        }
                        this.data[i] = key[i];
                    }
                } else {
                    this.data[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
                }
                return this;
            },
            /**
             * 设置ajax get请求时候附带的参数
             */
            setData: function(data) {
                this.data = data;
                return this;
            },
            /**
             * 清空ajax post请求参数
             */
            clear: function() {
                this.data = {};
                return this;
            },
            /**
             * 刷新表格
             */
            refresh: function(params) {
                if (typeof params != "undefined") {
                    this.btInstance.bootstrapTreeTable('refresh', params.query); // 为了兼容bootstrap-table的写法
                } else {
                    this.btInstance.bootstrapTreeTable('refresh');
                }
            },
            /**
             * 重置表格视图
             */
            resetView: function(params) {
                if (typeof params != "undefined") {
                    this.btInstance.bootstrapTreeTable('resetView', params.height); // 为了兼容bootstrap-table的写法
                }
            }
        };
    
        window.TreeTable = TreeTable;
    
    })(jQuery);
    
  • 相关阅读:
    ls 按大小排序 按时间排序
    【u035】奶牛的电信
    【record】#10
    【record】10.30..11.6
    【33.33%】【codeforces 608C】Chain Reaction
    【44.19%】【codeforces 608D】Zuma
    【22.73%】【codeforces 606D】Lazy Student
    【27.40%】【codeforces 599D】Spongebob and Squares
    【26.67%】【codeforces 596C】Wilbur and Points
    【13.91%】【codeforces 593D】Happy Tree Party
  • 原文地址:https://www.cnblogs.com/zhuzhongxing/p/14147095.html
Copyright © 2011-2022 走看看