/** 鍏ㄥ眬缁勪欢锛歱ub/sub */ var jqTableObject = $({}); $.sub = function () { jqTableObject.on.apply(jqTableObject, arguments); }; $.unsub = function () { jqTableObject.off.apply(jqTableObject, arguments); }; $.pub = function () { jqTableObject.trigger.apply(jqTableObject, arguments); }; $(function () { var iconClass = ['icon-right', 'icon-down']; //鏈夊眰绾у叧绯 var $body = $('body'); //鐐瑰嚮灞傜骇琛ㄦ牸title灞曞紑鏀惰捣锛堜緥瀛愶細鐩戞帶椤甸潰锛 $body.on('click', '.g-toggleTable--button', function (event) { var $this = $(this).parents('.g-toggleTable--title'); var $parent = $this.parents('.g-toggleTable--main'); var $title = $parent.find('.g-toggleTable--title'); var $tr = $parent.find('tr'); var indexStart = $tr.index($this); var indexEnd = $tr.length; var $nextTitle = $title.eq($title.index($this) + 1); var isTitle2 = $parent.data('level'); isTitle2 ? indexStart = indexStart + 1 : false; if ($nextTitle.length) { indexEnd = $tr.index($nextTitle); } $.each($parent.find('tr'), function (index, el) { if (index > indexStart && index < indexEnd) { $(el).toggleClass('hide'); } }); $this.find('.g-toggleTable--button').toggleClass(iconClass.join(' ')); singleChange($parent); }); function singleChange($parent) { //鍒ゆ柇鏄惁瑕佹敼鍙樺ご閮 var $title = $parent.find('.g-toggleTable--title'); var $selectAll = $parent.find('.g-toggleTable--buttonAll'); var plusLen = $parent.find('.g-toggleTable--button.' + iconClass[0]).length; var minusLen = $parent.find('.g-toggleTable--button.' + iconClass[1]).length; var titleLen = $title.length - 1; if (minusLen === titleLen) { $selectAll.removeClass(iconClass.join(' ')).addClass(iconClass[1]); } if (plusLen === titleLen) { $selectAll.removeClass(iconClass.join(' ')).addClass(iconClass[0]); } } $body.on('click', '.g-toggleTable--buttonAll', function () { var $this = $(this); var willShow = $this.hasClass(iconClass[0]); var $child = $this.parents('.g-toggleTable--main').find('.c-table__child'); var $title = $this.parents('.g-toggleTable--main').find('.g-toggleTable--title .g-toggleTable--button'); $title.removeClass(iconClass.join(' ')); $this.removeClass(iconClass.join(' ')); if (willShow) { $child.removeClass('hide'); $title.addClass(iconClass[1]); $this.addClass(iconClass[1]); } else { $child.addClass('hide'); $title.addClass(iconClass[0]); $this.addClass(iconClass[0]); } }) }) //琛ㄦ牸鍏ㄩ€ $(function () { var $body = $('body'); // 鍏ㄩ€ $body.on('change', '.g-selectAll', function (event) { $(this).parents('.g-selectAll--main').find('.g-selectAll--input').prop('checked', this.checked); }); // 閫変竴涓 $body.on('change', '.g-selectAll--input', function (event) { var $parents = $(this).parents('.g-selectAll--main'); var length = $parents.find('.g-selectAll--input').not('.g-selectAll').length; var lengthChecked = $parents.find('.g-selectAll--input:checked').not('.g-selectAll').length; if (lengthChecked === length) { $parents.find('.g-selectAll').prop('checked', true); return; } $parents.find('.g-selectAll').prop('checked', false); }); //鎵归噺鎿嶄綔 $body.on('click', '.g-selectAll--btn', function (event) { event.preventDefault(); gSelectAllBtnFn($(this)); }); function gSelectAllBtnFn($target) { var isDestory = $target.data('destory'); var $main = $target.parents('.g-selectAll--main'); var $checked = $main.find('.g-selectAll--input:checked').not('.g-selectAll'); var checkedArr = []; var text = $target.data('text') || '璇峰厛閫夋嫨'; var tag = $target.data('tag') || ''; var params = {}; var values = []; //isDestory涓簍rue涓嶉獙璇佷负绌 if (!isDestory && !$checked.length) { $.message({ type: 'error', message: text, }); return false; } $.each($checked, function (index, ele) { checkedArr.push($(ele).data('value')); values.push($(ele).val()); }); params = { ele: $target, checked: $checked, checkedArr: checkedArr, value: values }; $.pub('GSelectAllSuccess' + tag, [params]); }; }) //琛ㄦ牸涓讳笟鍔★細宸﹀彸鍥哄畾锛屾帓搴忥紝璁$畻鎬绘暟锛屽琛ㄥご绛 $(function () { var defaultOptions = { fixedLeft: false, fixedRight: false, fixedMinWidth: 100, fixedMaxWidth: 500, totalString: '--', totalTitle: '鎬昏', handleSortData: {}, noWidthColAdaptClient: false }; var JQTABLESCROLLWIDTH = getScrollBarWidth(); //鎺掑簭妯$増 var SORTTPL = '
' + '' + '' + '
'; // 鎷兼帴妯$増 var FIXEDWRAPPERTPL = '
' + '{{theadHtml}}' + '
' + '
' + '{{tbodyHtml}}' + '
'; var FIXEDWRAPPERTPLLEFT = '
' + '{{fixedWrapper}}' + '
'; var FIXEDWRAPPERTPLRIGHT = '
' + '{{fixedWrapper}}' + '
'; var TEMPLATETPL = '
' + // '{{hideColumn}}' + '
' + '{{theadHtml}}' + '
' + '
' + '{{tbodyHtml}}' + '
' + '{{fixedLeft}}' + '{{fixedRight}}' + '
'; var GUTTERTPL = ''; var GUTTERWIDTHTPL = ''; var FIXEDRIGHTPATCHTPL = '
'; // 鐐瑰嚮鎺掑簭 $('body').on('click.jqTable', '.c-table__down', function () { sortFn($(this), true); }); $('body').on('click.jqTable', '.c-table__up', function () { sortFn($(this), false); }); function jqTable(options, ele) { this.$container = ele; this.config = $.extend({}, defaultOptions, options); this.init(this.$container); } $.extend(jqTable.prototype, { //$this:瑕佸鍒剁殑琛ㄦ牸html init: function ($this) { this.isSafria = /version\/([\d.]+).*safari/i.test(navigator.userAgent); // 娓叉煋琛ㄦ牸 this.render($this); // 娣诲姞浜嬩欢 this.event($this); var _this = this; var $sort = this.$table.find('.c-table__sort'); if ($sort) { $.each($sort, function (index, el) { if ($(el).hasClass('activeUp')) { $(el).find('.c-table__up').trigger('click.jqTable'); _this.updateEvent(); return false; } if ($(el).hasClass('activeDown')) { $(el).find('.c-table__down').trigger('click.jqTable'); _this.updateEvent(); return false; } }); } $(window).trigger('resize'); }, render: function ($this) { var _this = this; var $template = render(_this, $this).data('table', _this); if (!_this.$table) { // 鍒濆鍖 _this.tableHeight = $this.data('height'); // 澶勭悊class var $table = $this.parents('.c-table').last(); $template.addClass($table.attr('class')); $table.removeAttr('class'); $this.after($template).remove(); _this.$table = $template; _this.$table.data('height', _this.tableHeight); } else { // 鏇存柊琛ㄦ牸鏁版嵁 _this.$table.html($template.html()); $template = _this.$table; } // 鑾峰彇澶撮儴淇℃伅 // 鍏煎绛変簬瀹藉害鐧惧垎鐧炬椂col涓嶈缃畐idth涓? $template.find('table').addClass('c-table--noWp100'); _this.colJson = getColInfo(_this); $template.find('table').removeClass('c-table--noWp100'); _this.totalJson = getColTotal(_this); _this.fixedWidth = _this.colJson.isAllWidth; _this.headerHeight = getHeaderHeight(_this.$table); if (!_this.totalJson.noTotal) { _this.totalArr = getTotalTdContent(_this); } // 瀹藉害鍏ㄩ儴鍥哄畾鏃讹紝璁剧疆瀹藉害锛屼笉鏀惧埌setsize閲岋紝resize涓嶉渶瑕侀噸鏂拌瀹藉害 if (_this.fixedWidth) { var sumWidth = _this.colJson.width.reduce(add, 0); _this.$table.width(sumWidth + 1); } //璁剧疆瀹藉害绛 // ie if (!document.addEventListener) { setSizeFn(_this); } else { setSize(_this); } }, event: function ($target) { var _this = this; // 绐楀彛澶у皬鏀瑰彉 $(window).resize(function () { setSize(_this.$table.data('table')); }); this.updateEvent(); }, updateEvent: function () { var _this = this; // 婊氬姩琛ㄦ牸 if (document.addEventListener) { _this.$table.find('.c-table__body-wrapper').scroll(function () { var $this = $(this); var $parents = $this.parents('.c-table--main'); var scrollL = $this.scrollLeft(); var scrollT = $this.scrollTop(); var $headerTable = $parents.find('.c-table__header-wrapper table'); var $footerTable = $parents.find('.c-table__footer-wrapper table'); var $fixedBodyTable = $parents.find('.c-table__fixed-body-wrapper table'); $headerTable.css({ 'margin-left': -scrollL + 'px' }); $footerTable.css({ 'margin-left': -scrollL + 'px' }); $fixedBodyTable.css({ 'margin-top': -scrollT + 'px' }); }); // 婊氬姩鍥哄畾鍒楀弬鏁 var scrollConfig = { isInit: false, scrollTop: 0, timer: '', prevTop: 0, maxScroll: 0, bodyStyle: '', isFirst: false, isBodyScroll: function () { return ($('html')[0].scrollHeight - $('html')[0].clientHeight) > 0; } }; // 婊氬姩鍥哄畾鍒 _this.$table.find('.c-table__fixed-body-wrapper').on('mousewheel', function (event) { clearTimeout(scrollConfig.timer); var $main = $(this).parents('.c-table--main'); var $bodyWrapper = $main.find('.c-table__body-wrapper'); var _this = $main.data('table'); // 鏈夌旱鍚戞粴鍔 if (!_this.scrollResult.colScroll) { return; } // 鍒ゆ柇婊氬姩缁撴潫 scrollConfig.timer = setTimeout(function () { clearWheel(); if (scrollConfig.isBodyScroll()) { $('body').attr('style', scrollConfig.bodyStyle); scrollConfig.isFirst = false; } }, 500); // 鍏煎body婊氬姩 if (!scrollConfig.isFirst && scrollConfig.isBodyScroll()) { scrollConfig.isFirst = true; // 闃绘body婊氬姩 scrollConfig.bodyStyle = $('body').attr('style'); $('body').css({ 'overflow': 'hidden', 'padding-right': JQTABLESCROLLWIDTH + 'px' }); } // 鍒ゆ柇鍒濆鍖栨粴鍔ㄥ弬鏁 if (!scrollConfig.isInit) { scrollConfig.isInit = true; scrollConfig.scrollTop = $bodyWrapper.scrollTop(); scrollConfig.maxScroll = $bodyWrapper.find('table').height() - $bodyWrapper.height(); if (_this.scrollResult.rowScroll) { scrollConfig.maxScroll += JQTABLESCROLLWIDTH; } return; } // 鍒ゆ柇鏄惁閲嶆柊寮€濮嬫粴鍔 if (scrollConfig.prevTop > Math.abs(event.deltaY)) { clearWheel(); return; } // 淇濆瓨top,缁欎笅涓€娆℃粴鍔ㄤ娇鐢 scrollConfig.prevTop = Math.abs(event.deltaY); // 绗﹀悎鏉′欢锛岃缃粴鍔 var top = - scrollConfig.scrollTop + event.deltaY; if (top > 0) { top = 0; } if (top < (-scrollConfig.maxScroll)) { top = -scrollConfig.maxScroll; } $main.find('.c-table__fixed-body-wrapper table').css({ 'margin-top': top + 'px' }); $bodyWrapper.scrollTop(-top); }); // 閲嶇疆婊氬姩鍙傛暟 function clearWheel() { scrollConfig.isInit = false; scrollConfig.scrollTop = 0; scrollConfig.prevTop = 0; } } if (_this.config.fixedLeft || _this.config.fixedRight) { _this.$table.find('tbody tr').hover(function () { var $this = $(this); var $main = $this.parents('.c-table--main'); var index = $this.parents('tbody').find('tr').index($this); var $tbody = $main.find('tbody'); // $tbody.find('tr').removeClass('hover-row') $.each($tbody, function (_index, el) { $(el).find('tr').eq(index).addClass('hover-row'); }); }, function () { var $tbody = $(this).parents('.c-table--main').find('tbody'); $tbody.find('tr').removeClass('hover-row'); }); } }, /*鏇存柊琛ㄦ牸html*/ updateHtml: function (html) { this.init($(html)); }, // 鏇存柊楂樺害 updateHeight: function (height) { this.tableHeight = height || this.$table.data('height'); this.$table.data('height', this.tableHeight); var tableBodyHeight = this.tableHeight - this.headerHeight; var fixedHeight = this.tableHeight; if (this.scrollResult.rowScroll) { tableBodyHeight -= JQTABLESCROLLWIDTH; fixedHeight -= JQTABLESCROLLWIDTH; } // 鏈夊浐瀹氭椂,璁剧疆楂樺害 if (this.config.fixedRight || this.config.fixedLeft) { this.$table.find('.c-table__fixed-body-wrapper').css({ 'max-height': tableBodyHeight + 'px', 'top': this.headerHeight + 'px' }); } if (this.config.fixedLeft) { this.$table.find('.c-table__fixed').height(fixedHeight + 'px'); } if (this.config.fixedRight) { this.$table.find('.c-table__fixed-right').height(fixedHeight + 'px'); } setSize(this); // setTableHeight(this); } }); /*========BEGIN 鎺掑簭===========*/ // 鎺掑簭 function sortFn($this, isDown) { var className = isDown ? 'activeDown' : 'activeUp'; //鍒囨崲class var $template = $this.parents('.c-table--main'); var _this = $template.data('table'); var $fixedTbody = $template.find('.c-table__fixed-body-wrapper tbody'); var $tbody = $template.find('.c-table__body-wrapper table tbody'); var $tr = $tbody.find('tr'); var $th = $this.parents('thead').find('th'); var sortJson = []; var index = $th.index($this.parents('th')); //鍒囨崲class $template.find('.c-table__sort').removeClass('activeUp activeDown'); $template.find('.c-table__fixed-header-wrapper th').eq(index).find('.c-table__sort').addClass(className); $template.find('.c-table__header-wrapper th').eq(index).find('.c-table__sort').addClass(className); // 闀垮害灏忎簬1涓嶆帓搴 if ($tr.length <= 1) { return; } //娓叉煋鎺掑簭鎵€闇€鐨刯son $.each($tr, function (_index, el) { var _temp = {}; _temp.index = _index; _temp.value = $(el).find('td').eq(index).text().trim(); sortJson.push(_temp); }); var handleSortDataJson = _this.config.handleSortData; var sortConfig = _this.sortConfig; // 鍒ゆ柇鏄惁瀛樺湪鎺掑簭鐗规畩閰嶇疆 if (sortConfig[index]) { if (sortConfig[index] && handleSortDataJson[sortConfig[index]] && typeof (handleSortDataJson[sortConfig[index]]) === 'function') { // 鍒ゆ柇鎺掑簭鍙傛暟涓哄嚱鏁帮紝涓斿湪config涓敞鍐岃繃 sortJson = handleSortDataJson[sortConfig[index]](sortJson); } else { // 鍙傛暟涓哄瓧绗︿覆鏇挎崲 sortJson = deleteMatchStr(sortJson, sortConfig[index]); } } //鎺掑簭 sortJson = order(sortJson, 'value', isDown); // 娓叉煋鎺掑簭濂界殑html sortRender(_this, $tbody, sortJson); $.each($fixedTbody, function (index, fixedTbody) { sortRender(_this, $(fixedTbody), sortJson); }); } // 瀛楃涓叉浛鎹 function deleteMatchStr(data, str) { var result = []; var _data; var reg = new RegExp(str, 'g'); for (var i = 0; i < data.length; i++) { _data = data[i]; _data.value = (_data.value + '').replace(reg, ''); result.push(_data); } return result; } // 娓叉煋鎺掑簭濂界殑html function sortRender(_this, $tbody, sortJson) { var $tr = $tbody.find('tr'); var $_tbody = $tbody.clone(); $_tbody.html(''); for (var i = 0, len = sortJson.length; i < len; i++) { var __index = sortJson[i].index; $_tbody.append($tr.eq(__index).clone()); } $tbody.replaceWith($_tbody); _this.updateEvent(); } //desc闄嶅簭 function order(arr, key, desc) { var orderfn; if (arr.length < 2) { return; } if (isNaN(arr[0][key])) { //瀛楃涓茬被鍨 orderfn = orderString; } else { //鏁板瓧绫诲瀷 orderfn = orderNumber; } arr.sort(function (a, b) { var _a = a[key], _b = b[key]; if (desc) { _a = b[key]; _b = a[key]; } return orderfn(_a, _b); }); return arr; } //鏁板瓧鎺掑簭 function orderNumber(a, b) { return a - b; } //瀛楃涓叉帓搴 function orderString(a, b) { var titleA = a.toLowerCase(), titleB = b.toLowerCase(); if (titleA < titleB) return -1; if (titleA > titleB) return 1; return 0; } //娣诲姞鎺掑簭 function addSort(_this, $thead) { var result = getColSort(_this, $thead); if (result.noSort) { return; } _this.sortConfig = result.sortConfig; var sortArr = result.sort; var $th = $thead.find('thead th'); for (var i = 0, len = sortArr.length; i < len; i++) { var _sort = sortArr[i]; if (_sort) { var className = _sort === 'up' ? 'activeUp' : _sort === 'down' ? 'activeDown' : ''; var tpl = $(SORTTPL.replace('{{class}}', className)); $th.eq(i).find('.cell').append(tpl); } } } /*========END 鎺掑簭===========*/ /*========BEGIN 娓叉煋琛ㄦ牸===========*/ //鑾峰彇楂樺害 function getHeaderHeight($template) { return $template.find('.c-table__header-wrapper').height(); } //鎷兼帴琛ㄦ牸 function render(_this, $this) { var $thead = $this.clone().addClass('c-table__header'); var $tbody = $this.clone().addClass('c-table__body'); $thead.find('tbody').remove(); $tbody.find('thead').remove(); addSort(_this, $thead); var theadHtml = $thead[0].outerHTML; var tbodyHtml = $tbody[0].outerHTML; // 鎷兼帴妯$増 var fixedWrapper = FIXEDWRAPPERTPL.replace('{{theadHtml}}', theadHtml).replace('{{tbodyHtml}}', tbodyHtml); var fixedLeft = _this.config.fixedLeft ? FIXEDWRAPPERTPLLEFT.replace(/{{fixedWrapper}}/g, fixedWrapper) : ''; var fixedRight = _this.config.fixedRight ? FIXEDWRAPPERTPLRIGHT.replace(/{{fixedWrapper}}/g, fixedWrapper) : ''; var template = TEMPLATETPL.replace('{{theadHtml}}', theadHtml).replace('{{tbodyHtml}}', tbodyHtml).replace('{{fixedLeft}}', fixedLeft).replace('{{fixedRight}}', fixedRight); var $template = $(template); return $template; } function setSizeFn(_this) { var $template = _this.$table; // var $headerwrapper = $template.find('.c-table__header-wrapper'); var $bodywrapper = $template.find('.c-table__body-wrapper'); // var $thead = $headerwrapper.find('table'); var $tbody = $bodywrapper.find('table'); // var $colgroup = $template.find('colgroup'); var scrollResult; // 鑾峰彇colgroup瀹藉害灞炴€т俊鎭 var colJson = _this.colJson; //璁剧疆table楂樺害 setTableHeight(_this); scrollResult = judgeIsScroll($bodywrapper, $tbody, $template, colJson); //涓嶆槸鍏ㄩ儴鍥哄畾瀹藉害 if (!_this.fixedWidth) { //閲嶇疆Colgroup瀹藉害灞炴€ resetColgroupWidth(_this, $template, colJson, scrollResult); } // 淇敼瀹藉害鍊 // 鑾峰彇琛ㄦ牸瀹藉害--濡傛灉鎯虫敼鍙樺搴 灏卞湪澶栧洿鍔燿iv闄愬埗锛宑-table榛樿width:100% // var tableWidth = $template.width(); // $thead.width(tableWidth); // $tbody.width(tableWidth); // 璁剧疆gutter setGutter($template, scrollResult); // 鏈夋粴鍔ㄦ潯&&鍏ㄩ儴鍒楀浐瀹氬搴﹁缃搴 if (_this.fixedWidth) { setTableWidthScroll(_this, $template, scrollResult); } // 鏄惁鏈夋粴鍔ㄦ潯 scrollResult = judgeIsScroll($bodywrapper, $tbody, $template, colJson); _this.scrollResult = scrollResult; // 鏈夊浐瀹氬垪鏃讹紝璁剧疆鍥哄畾鍒楀楂 if (_this.config.fixedRight || _this.config.fixedLeft) { setFixedSize(_this, $template, scrollResult); removeFixedCheckbox(_this, $template, scrollResult); } else { $template.addClass('el-table--enable-row-transition'); } //鍏煎鏍峰紡锛氱旱鍚戞粴鍔ㄦ潯&鍥哄畾鍙宠竟&娌℃湁patch if (scrollResult.colScroll && !$template.find('.c-table__fixed-right-patch').length) { $bodywrapper.after(FIXEDRIGHTPATCHTPL.replace('{{headerHeight}}', _this.headerHeight)); } else if (!scrollResult.colScroll) { $template.find('.c-table__fixed-right-patch').remove(); } //娣诲姞鎬昏 if (!_this.totalJson.noTotal) { setTotalHtml(_this, $template); } } // 璁剧疆瀹藉害 function setSize(_this) { if (!document.addEventListener) { return; } setSizeFn(_this); } //璁剧疆鎬昏html function setTotalHtml(_this, $template) { var $bodywrapper = $template.find('.c-table__body-wrapper'); var $footerWrapper = $bodywrapper.clone().removeClass('c-table__body-wrapper').addClass('c-table__footer-wrapper'); $footerWrapper.find('.c-table__body').removeClass('.c-table__body').addClass('c-table__footer'); var $footerWrapperTr = $footerWrapper.find('tbody tr:not(:first)').remove(); var $footerWrapperOld = $template.find('.c-table__footer-wrapper'); if ($footerWrapperOld.length) { $footerWrapperOld.replaceWith($footerWrapper); } else { $bodywrapper.after($footerWrapper); } var $td = $footerWrapper.find('tbody tr').eq(0).find('td'); $.each($td, function (index, el) { $td.find('.cell').eq(index).html(_this.totalArr[index]); }); } // 鐢ㄤ簡缃戜笂鐨勬柟娉 function formatFloat(f, digit) { digit = digit || 1; var m = Math.pow(10, digit); return parseInt(f * m, 10) / m; }; //鑾峰彇鍚堣鐨則d鐨勬暟缁 function getTotalTdContent(_this) { var totalJson = _this.totalJson; var $tr = _this.$table.find('.c-table__body-wrapper').find('tbody tr'); var footerArr = [_this.config.totalTitle]; var footerDataArr = []; var totalString = _this.config.totalString; $.each($tr, function (index, el) { var $_td = $(el).find('td'); $.each($_td, function (_index, _el) { if (index === 0) { footerDataArr.push([]); } footerDataArr[_index].push($(_el).text().trim()); }); }); var temp; for (var i = 1; i < totalJson.total.length; i++) { var _total = totalJson.total[i]; if (_total) { temp = footerDataArr[i].reduce(addNum, 0); } else { temp = totalString; } footerArr.push(temp); } return footerArr; } //绉婚櫎閫変腑鐨勫閫夋锛屽吋瀹瑰垪鍥哄畾 function removeFixedCheckbox(_this, $template, scrollResult) { var $checkbox = $template.find('.g-selectAll--input'); if (!$checkbox.length) { return; } var $tableFixed = $template.find('.c-table__fixed'); var $tableFixedRight = $template.find('.c-table__fixed-right'); var $tr = $template.find('.c-table__body-wrapper tr'); var $fixedTr, len = _this.colJson.width.length - 1; var leftCheckboxLen = fixedCheckboxLen($tr, 0); var rightCheckboxLen = fixedCheckboxLen($tr, len); //瀛樺湪宸﹀浐瀹氬垪锛岀Щ闄よ〃鏍奸鍒楀拰鍙冲浐瀹氬垪棣栧垪checkbox if (_this.config.fixedLeft && leftCheckboxLen) { matchRemoveCheckbox($tableFixedRight, $tr, 0); } //瀛樺湪鍙冲浐瀹氬垪 if (_this.config.fixedRight && rightCheckboxLen) { matchRemoveCheckbox($tableFixed, $tr, len); } } //寰幆鍖归厤 function matchRemoveCheckbox($fixed, $tr, index) { var $fixedTr = $fixed.find('.c-table__fixed-body-wrapper tr'); if ($fixed.length) { $.each($fixedTr, function (_index, _tr) { removeCheckbox(_tr, index); }); } $.each($tr, function (_index, _tr) { removeCheckbox(_tr, index); }); } //绉婚櫎input function removeCheckbox(target, index) { $(target).find('td').eq(index).find('.g-selectAll--input').remove(); } // 鍥哄畾鍒楅噷鐨刢heckbox涓暟 function fixedCheckboxLen($tr, index) { return $tr.eq(0).find('td').eq(index).find('.g-selectAll--input').length; } //璁剧疆fixed function setFixedSize(_this, $template, scrollResult) { //鎬诲垪鏁 var colLen = _this.colJson.width.length; var $theadTh = $template.find('.c-table__header-wrapper table th'); var $tbodyTr = $template.find('.c-table__body-wrapper table tr').eq(0); var $tbodyTd = $tbodyTr.find('td:not(.gutter)'); var tableHeaderHeight = _this.headerHeight; var tableHeight = $template.height(); var tableWidth = $template.width(); var tableBodyHeight = tableHeight - tableHeaderHeight; var $tableFixed = $template.find('.c-table__fixed'); var $tableFixedRight = $template.find('.c-table__fixed-right'); var _config = _this.config; var _fixedLeft = _config.fixedLeft; var _fixedRight = _config.fixedRight; var shadowName = 'c-table__fixed--shadow'; if (scrollResult.rowScroll) { tableBodyHeight -= JQTABLESCROLLWIDTH; } // 鏈夊浐瀹氭椂,璁剧疆楂樺害 if (_this.config.fixedRight || _fixedLeft) { $template.find('.c-table__fixed-body-wrapper').css({ 'max-height': tableBodyHeight + 'px', 'top': _this.headerHeight + 'px' }); if (scrollResult.rowScroll) { $tableFixed.addClass(shadowName); $tableFixedRight.addClass(shadowName); } else { $tableFixed.removeClass(shadowName); $tableFixedRight.removeClass(shadowName); } } // 宸︿晶鍥哄畾鍒楅殣钘 if (_fixedLeft && _fixedLeft <= 1) { // 鍗曞垪 $tableFixed.find('.c-table__fixed-body-wrapper tr td:not(:first-child):not(.gutter)').addClass('is-hidden'); } else if (_fixedLeft > 1) { // 澶氬垪 $.each($tableFixed.find('.c-table__fixed-body-wrapper tr'), function (index, _tr) { $.each($(_tr).find('td:not(.gutter)'), function (index, _td) { if (index >= _fixedLeft) { $(_td).addClass('is-hidden'); } }) }) } // 鍙充晶鍥哄畾鍒楅殣钘 if (_fixedRight && _fixedRight <= 1) { // 鍗曞垪 $tableFixedRight.find('.c-table__fixed-body-wrapper tr td:not(:nth-child(' + colLen + ')):not(.gutter)').addClass('is-hidden'); } else if (_fixedRight > 1) { // 澶氬垪 $.each($tableFixedRight.find('.c-table__fixed-body-wrapper tr'), function (index, _tr) { $.each($(_tr).find('td:not(.gutter)'), function (index, _td) { if (index <= colLen - _fixedRight - 1) { $(_td).addClass('is-hidden'); } }) }) } var fixedHeight = scrollResult.rowScroll ? tableHeight - JQTABLESCROLLWIDTH : tableHeight; if (!_this.totalJson.noTotal) { fixedHeight -= $tbodyTr.height(); } if (_fixedLeft) { var fixedWidth = 0; // 鍗曞垪 if (_fixedLeft <= 1) { fixedWidth = $tbodyTd.eq(0).width(); } else if (_fixedLeft > 1) { //澶氬垪 for (var index = 0; index < _fixedLeft; index++) { fixedWidth += $tbodyTd.eq(index).width(); } } $tableFixed.width(fixedWidth).height(fixedHeight); // 涓嶆槸鍏ㄩ儴鍥哄畾鍒楋紝鍦╮esetColgroupWidth璁剧疆瀹藉害 if (_this.colJson.isAllWidth === true) { $tableFixed.find('table').width(tableWidth); } } if (_this.config.fixedRight) { var fixedRight = scrollResult.colScroll ? JQTABLESCROLLWIDTH : 0; var fixedRightWidth = 0; if (_fixedRight <= 1) { // 鍗曞垪 fixedRightWidth = $tbodyTd.eq($tbodyTd.length - 1).width(); } else if (_fixedRight > 1) { //澶氬垪 for (var index = 0; index < _fixedRight; index++) { fixedRightWidth += $tbodyTd.eq($tbodyTd.length - 1 - index).width(); } } //鍙充晶鍒楀搴 $tableFixedRight.width(fixedRightWidth).height(fixedHeight).css({ right: fixedRight }); // 涓嶆槸鍏ㄩ儴鍥哄畾鍒楋紝鍦╮esetColgroupWidth璁剧疆瀹藉害 if (_this.colJson.isAllWidth === true) { $tableFixedRight.find('table').width(tableWidth); } } } //鏈夋粴鍔ㄦ潯&&鍏ㄩ儴鍒楀浐瀹氬搴 function setTableWidthScroll(_this, $template, scrollResult) { var $thead = $template.find('.c-table__header-wrapper table'); var $tbody = $template.find('.c-table__body-wrapper table'); var sumWidth = _this.colJson.width.reduce(add, 0); var templateWidth = scrollResult.colScroll ? sumWidth + JQTABLESCROLLWIDTH : sumWidth; $template.width(templateWidth + 1); $thead.width(sumWidth); $tbody.width(sumWidth); } //gutter娣诲姞绉婚櫎 function setGutter($template, scrollResult) { var $thead = $template.find('.c-table__header-wrapper table'); var $tbody = $template.find('.c-table__body-wrapper table'); var isGutter = $thead.find('.gutter').length; if (isGutter) { $thead.find('.gutter').remove(); $tbody.find('.gutter').remove(); } } //璁剧疆琛ㄥ摜楂樺害 function setTableHeight(_this) { var $template = _this.$table; //璁剧疆table楂樺害 //var templateHeight = $this.data('height'); var templateHeight = _this.$table.data('height'); if (templateHeight) { var $bodywrapper = $template.find('.c-table__body-wrapper'); $template.css({ 'max-height': templateHeight + 'px' }); $bodywrapper.css({ 'max-height': 'none' }); // 鑾峰彇琛ㄦ牸楂樺害 var tableHeaderHeight = _this.headerHeight; var tableHeight = $template.height(); var bodyHeight = tableHeight - tableHeaderHeight; if (!_this.totalJson.noTotal) { bodyHeight -= $bodywrapper.find('tbody tr').eq(0).height(); } $bodywrapper.css({ 'max-height': bodyHeight + 'px' }); } } //璁剧疆col瀹藉害 function resetColgroupWidth(_this, $template, colJson, scrollResult) { var $colgroup = $template.find('colgroup'); var tableWidth = $template.width(); //閬嶅巻璧嬪€兼棤瀹藉害灞炴€у垪瀹藉害--鑷€傚簲鍒 //鎬诲垪鏁 //鏃犲搴﹁嚜閫傚簲鐨勫垪涓暟 var colNoWidthLen = colJson.noWidthIndex.length; //鎬诲搴 var sumWidth = colJson.tdWidth.reduce(add, 0); if (scrollResult.colScroll) { sumWidth += JQTABLESCROLLWIDTH; } var minWidth = _this.config.fixedMinWidth; var noWidthColAdaptClient = _this.config.noWidthColAdaptClient; var arr = []; for (var i = 0; i < colNoWidthLen; i++) { var _index = colJson.noWidthIndex[i]; arr.push(colJson.tdWidth[_index]); } var arrWidth = arr.reduce(add, 0); var leftwidth = tableWidth - sumWidth + arrWidth - 2; //閮戒笉澶熷垎锛屾渶灏忓搴 if (arr.length * minWidth >= leftwidth) { setColWidthEach(_this, $colgroup, colJson, arr, minWidth); } else { arr = getColgroupWidth(arr, leftwidth, minWidth, arr.length, noWidthColAdaptClient); setColWidthEach(_this, $colgroup, colJson, arr, false); } sumWidth = colJson.width.reduce(add, 0) + 2; $template.find('.c-table__header-wrapper table').width(sumWidth); $template.find('.c-table__body-wrapper table').width(sumWidth); $template.find('.c-table__fixed-header-wrapper table').width(sumWidth - 2); $template.find('.c-table__fixed-body-wrapper table').width(sumWidth - 2); } //寰幆璁剧疆鑷€傚簲col瀹藉害 function setColWidthEach(_this, $colgroup, colJson, arr, minWidth) { var colNoWidthLen = colJson.noWidthIndex.length; var overSizeColArr = []; for (var j = 0; j < colNoWidthLen; j++) { var _index = colJson.noWidthIndex[j]; if (/^\d+\.\d{4,}$/.test(arr[j] + '')) { arr[j] = Number(arr[j]).toFixed(3); } var _width = minWidth && arr[j] < minWidth ? minWidth : arr[j]; colJson.width[_index] = Number(_width); // 瓒呰繃鏈€澶у搴 if (_width >= _this.config.fixedMaxWidth) { overSizeColArr.push(_index); } $.each($colgroup, function (index, el) { $(el).find('col').eq(_index).attr('width', _width); }); } setSafriaCellWidthEach(_this, $colgroup, colJson, overSizeColArr); } // 鍏煎safria鍦╰d瀹藉害澶т簬col鏃讹紝col鐨剋idth澶辨晥 function setSafriaCellWidthEach(_this, $colgroup, colJson, arr) { // safria&&瓒呰繃鏈€澶у搴 if (_this.isSafria) { var noIndexArr = colJson.noWidthIndex; if (arr.length > 0) { setSafriaCellWidthEachFn($colgroup, colJson, arr, true); // 鍘绘帀璁剧疆楂樺害鐨 noIndexArr = noIndexArr.filter(function (item) { return arr.indexOf(item) === -1; }); } // 鍓╀綑瀹藉害璁句负auto-閽堝resize if (noIndexArr.length) { setSafriaCellWidthEachFn($colgroup, colJson, noIndexArr, false); } } } function setSafriaCellWidthEachFn($colgroup, colJson, arr, isOverSize) { for (var i = 0; i < arr.length; i++) { var _index = arr[i]; $.each($colgroup, function (i1, el) { var $tr = $(el).parents('table').find('tbody tr'); var _width = isOverSize ? colJson.width[_index] - 36 : 'auto'; $.each($tr, function (i2, _tr) { $(_tr).find('td').eq(_index).find('.cell').width(_width); }); }); } } //鑾峰彇鑷€傚簲鐨勬渶缁堝搴 function getColgroupWidth(arr, width, minwidth, length, adaptClient) { var totalWidth = arr.reduce(addNum, 0); var distance = (width - totalWidth) / length; // 涓嶈嚜閫傚簲锛屼竴鏃?0灏辫繑鍥烇紝涓嶅仛鎿嶄綔 if (!adaptClient && distance < 0) { return arr; } else { //鎿嶄綔distance鐨勬鏁 var len = 0; //鏈夊浣欑殑瀹藉害 var distanceLen = 0; //鍓╀綑鍙搷浣滅殑鏁 var countLen = 0; if (distance > 0) { for (var i = 0; i < arr.length; i++) { arr[i] = arr[i] + distance; arr[i] < minwidth ? arr[i] = minwidth : countLen++; } } else { for (var j = 0; j < arr.length; j++) { if (arr[j] < minwidth) { arr[j] = minwidth; } else if (arr[j] > minwidth) { var _width = arr[j] + distance; if (_width > minwidth) { countLen++; } arr[j] = _width < minwidth ? minwidth : _width; } } } totalWidth = arr.reduce(addNum, 0); //鎿嶄綔浜哾istance鐨勬鏁颁笉澶熼渶瑕佺殑 || 鏈夋瘮鏈€灏忓搴﹀皬鐨勶紝澶氬嚭浜嗗樊鍊间笖鏈夋暟鍙互琛 // if ((len < length && countLen > 0) || (distanceLen && countLen > 0)) { if (Math.floor(totalWidth) !== Math.floor(width)) { getColgroupWidth(arr, width, minwidth, countLen, adaptClient); } return arr; } } //distance>0 // function getColgroupWidth(arr, width, minwidth, length) { // var totalWidth = arr.reduce(addNum, 0); // var distance = (width - totalWidth) / length; // for (var i = 0; i < arr.length; i++) { // arr[i] += distance; // } // totalWidth = arr.reduce(addNum, 0); // if (totalWidth <= width) { // countLen = arr.length; // } // if (totalWidth !== width) { // getColgroupWidth(arr, width, minwidth, countLen); // } // return arr; // } //鏈€灏忓搴 function judgeMinWidth(_this, width) { var _width = width < _this.config.fixedMinWidth ? _this.config.fixedMinWidth : width; return _width; } function add(a, b) { return a + b; } function addNum(a, b) { return formatFloat(a - (-b)); } function getScrollBarWidth() { var inner = document.createElement('p'); inner.style.width = "100%"; inner.style.height = "200px"; var outer = document.createElement('div'); outer.style.position = "absolute"; outer.style.top = "0px"; outer.style.left = "0px"; outer.style.visibility = "hidden"; outer.style.width = "200px"; outer.style.height = "150px"; outer.style.overflow = "hidden"; outer.appendChild(inner); document.body.appendChild(outer); var w1 = inner.offsetWidth; outer.style.overflow = 'scroll'; var w2 = inner.offsetWidth; if (w1 == w2) w2 = outer.clientWidth; document.body.removeChild(outer); return (w1 - w2); } //鑾峰彇Col瀹藉害,鏄惁鎺掑簭 function getColSort(_this, $thead) { var $col = $thead.find('colgroup>col'); var result = getColFn('sort', $col); var sortconfig = getColFn('sortconfig', $col); return { sort: result.demo, noSort: result.nodemo, sortConfig: sortconfig.demo }; } //鑾峰彇Col鏄惁鎬昏 function getColTotal(_this) { var $thead = _this.$table.find('.c-table__header-wrapper table'); var $col = $thead.find('colgroup>col'); var result = getColFn('total', $col); var total = result.demo; var noTotal = result.nodemo; return { total: total, noTotal: noTotal }; } //鑾峰彇淇℃伅鍑芥暟 function getColFn(name, $col) { // 鎺掑簭 var demo = []; var demoLen = 0; $col = $col.not('[name=gutter]'); $.each($col, function (_index, el) { var _demo = $(el).data(name); demo.push(_demo); if (_demo) { demoLen++; } }); var nodemo = demoLen ? false : true; return { demo: demo, nodemo: nodemo }; } // 瀵规瘮th鍜宼d瀹藉害锛岃繑鍥炲垪鏈€缁堝搴 function getEachColWidth($tds, $ths, index) { var tdWidth = $tds.eq(index).width(); var thWidth = $ths.eq(index).width(); return tdWidth > thWidth ? tdWidth : thWidth; } // 鑾峰彇Col瀹藉害 function getColInfo(_this) { _this.$table.addClass('c-table--noWrap-init'); var $thead = _this.$table.find('.c-table__header-wrapper table'); var $tbody = _this.$table.find('.c-table__body-wrapper table'); var $col = $thead.find('colgroup>col'); var theadWidth = $thead.width(); var tbodyWidth = $tbody.width(); //鎵€鏈夌殑width var width = []; var tdWidth = []; //鐪熷疄鐨勫搴 var realTotalWidth = theadWidth > tbodyWidth ? theadWidth : tbodyWidth; //鏃犲搴︾殑搴忓彿 var noWidthIndex = []; //鏈夊搴︾殑瀹藉害鍊 var hasWidthArr = []; var isAllWidth = true; var $tds = $tbody.find('tr').eq(0).find('td'); var $ths = $thead.find('tr').eq(0).find('th'); $col = $col.not('[name=gutter]'); $.each($col, function (_index, el) { var _width = $(el).attr('width') ? Number($(el).attr('width')) : 0; _width = judgeMaxWidth(_this, _width); width.push(_width); if (_width) { hasWidthArr.push(_width); tdWidth.push(_width); } else { var _tdsWidth = judgeMaxWidth(_this, getEachColWidth($tds, $ths, _index) + 1); // var _tdWidth = _tdsWidth < _this.config.fixedMinWidth ? _this.config.fixedMinWidth : _tdsWidth; isAllWidth = false; noWidthIndex.push(_index); tdWidth.push(_tdsWidth); } }); _this.$table.addClass('c-table__cell--block').removeClass('c-table--noWrap-init'); return { width: width, noWidthIndex: noWidthIndex, hasWidthArr: hasWidthArr, isAllWidth: isAllWidth, realTotalWidth: realTotalWidth, tdWidth: tdWidth }; } // 鍒ゆ柇澶т簬鏈€澶у搴 function judgeMaxWidth(_this, width) { var _width = width > _this.config.fixedMaxWidth ? _this.config.fixedMaxWidth : width; return _width; } //鍒ゆ柇婊氬姩 function judgeIsScroll($bodywrapper, $tbody, $template, colJson) { var colScroll = $bodywrapper.height() < $tbody.height(); var totalWidth = colJson.width.reduce(add, 0) + 1; if (colScroll) { totalWidth += JQTABLESCROLLWIDTH; } var rowScroll = totalWidth > $template.width(); if (rowScroll) { colScroll = $bodywrapper.height() - JQTABLESCROLLWIDTH < $tbody.height(); } return { rowScroll: rowScroll, colScroll: colScroll }; } /*========END 娓叉煋琛ㄦ牸===========*/ $.fn.jqTable = function (options) { return this.each(function () { new jqTable(options, $(this)); }); }; }());