soulTable.js 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. /**
  2. *
  3. * @name: 表格增强插件
  4. * @author: yelog
  5. * @version: v1.4.4
  6. */
  7. layui.define(['table', 'tableFilter', 'tableChild', 'tableMerge'], function (exports) {
  8. var tableFilter = layui.tableFilter,
  9. tableChild = layui.tableChild,
  10. tableMerge = layui.tableMerge,
  11. $ = layui.$,
  12. table = layui.table,
  13. HIDE = 'layui-hide',
  14. isFirst = true; // 第一次加载表格
  15. // 封装方法
  16. var mod = {
  17. render: function (myTable) {
  18. var curTableSession = localStorage.getItem(location.pathname + location.hash + myTable.id);
  19. if (myTable.filter && myTable.filter.cache && isFirst && curTableSession) {
  20. myTable.cols = this.deepParse(curTableSession);
  21. isFirst = false;
  22. table.reload(myTable.id, myTable)
  23. } else {
  24. tableFilter.render(myTable);
  25. tableChild.render(myTable);
  26. tableMerge.render(myTable);
  27. // 修复合计栏固定列问题
  28. if (myTable.fixTotal) {
  29. this.fixTotal(myTable)
  30. }
  31. if (typeof myTable.drag === 'undefined' || myTable.drag) {
  32. this.drag(myTable);
  33. }
  34. if (myTable.rowDrag) {
  35. this.rowDrag(myTable)
  36. }
  37. this.autoColumnWidth(myTable)
  38. this.contextmenu(myTable);
  39. if (typeof myTable.fixResize === 'undefined' || myTable.fixResize) {
  40. this.fixResizeRightFixed(myTable);
  41. }
  42. if (myTable.overflow) {
  43. this.overflow(myTable);
  44. }
  45. this.fixFixedScroll(myTable);
  46. }
  47. }
  48. /**
  49. * excel表格导出
  50. * @param myTable
  51. * @param curExcel
  52. * @param customizeConfig 具体配置参考 tableFilter 中的 export 的 customizeConfig 配置
  53. */
  54. , export: function (myTable, curExcel,customizeConfig) {
  55. tableFilter.export(myTable.config||myTable, curExcel,customizeConfig);
  56. }
  57. ,getCssRule: function(that, key, callback){
  58. var style = that.elem.next().find('style')[0]
  59. ,sheet = style.sheet || style.styleSheet || {}
  60. ,rules = sheet.cssRules || sheet.rules;
  61. layui.each(rules, function(i, item){
  62. if(item.selectorText === ('.laytable-cell-'+ key)){
  63. return callback(item), true;
  64. }
  65. });
  66. }
  67. , autoColumnWidth: function (myTable) {
  68. var _this = this,
  69. $table = $(myTable.elem),
  70. th = $table.next().children('.layui-table-box').children('.layui-table-header').children('table').children('thead').children('tr').children('th'),
  71. fixTh = $table.next().children('.layui-table-box').children('.layui-table-fixed').children('.layui-table-header').children('table').children('thead').children('tr').children('th'),
  72. $tableBodytr = $table.next().children('.layui-table-box').children('.layui-table-body').children('table').children('tbody').children('tr');
  73. String.prototype.width = function(font) {
  74. var f = font || $('body').css('font'),
  75. o = $('<div>' + this + '</div>')
  76. .css({'position': 'absolute', 'float': 'left', 'white-space': 'nowrap', 'visibility': 'hidden', 'font': f})
  77. .appendTo($('body')),
  78. w = o.width();
  79. o.remove();
  80. return w;
  81. }
  82. th.add(fixTh).on('dblclick', function(e){
  83. var othis = $(this)
  84. ,field = othis.data('field')
  85. ,key = othis.data('key')
  86. ,oLeft = othis.offset().left
  87. ,pLeft = e.clientX - oLeft;
  88. if(othis.attr('colspan') > 1){
  89. return;
  90. }
  91. if ($(this).parents('.layui-table-fixed-r').length>0 ? pLeft<=10 : othis.width() - pLeft<=10) {
  92. var maxWidth = othis.text().width(othis.css('font'))+21, font = othis.css('font');
  93. $tableBodytr.children('td[data-field="'+field+'"]').each(function (index, elem) {
  94. var curWidth = $(this).text().width(font);
  95. if ( maxWidth <curWidth) {
  96. maxWidth = curWidth
  97. }
  98. })
  99. maxWidth +=32;
  100. _this.getCssRule(myTable, key, function(item){
  101. item.style.width = maxWidth+'px'
  102. });
  103. for (var i = 0; i < myTable.cols.length; i++) {
  104. for (var j = 0; j < myTable.cols[i].length; j++) {
  105. if (myTable.cols[i][j].field === field) {
  106. myTable.cols[i][j].width = maxWidth;
  107. break;
  108. }
  109. }
  110. }
  111. }
  112. })
  113. }
  114. /**
  115. * 左右拖拽调整列顺序、向上拖隐藏列
  116. * @param myTable
  117. */
  118. , drag: function (myTable) {
  119. if (myTable.cols.length>1) {
  120. // 如果是复杂表头,则自动禁用拖动效果
  121. return;
  122. }
  123. var _this = this,
  124. $table = $(myTable.elem),
  125. $tableBox = $table.next().children('.layui-table-box'),
  126. $tableHead = $.merge($tableBox.children('.layui-table-header').children('table'),$tableBox.children('.layui-table-fixed').children('.layui-table-header').children('table')),
  127. $fixedBody = $tableBox.children('.layui-table-fixed').children('.layui-table-body').children('table'),
  128. $noFixedBody = $tableBox.children('.layui-table-body').children('table'),
  129. $tableBody = $.merge($tableBox.children('.layui-table-body').children('table'), $fixedBody),
  130. $totalTable = $table.next().children('.layui-table-total').children('table'),
  131. $fixedTotalTable = $table.next().children('.layui-table-total').children('table.layui-table-total-fixed'),
  132. $noFixedTotalTable = $table.next().children('.layui-table-total').children('table:eq(0)'),
  133. tableId = myTable.id,
  134. isSimple = myTable.drag === 'simple' || (myTable.drag && myTable.drag.type === 'simple'), // 是否为简易拖拽
  135. toolbar = myTable.drag && myTable.drag.toolbar, // 是否开启工具栏
  136. isDraging = false, isStart = false;
  137. if (!$tableHead.attr('drag')) {
  138. $tableHead.attr('drag', true);
  139. if (toolbar) {
  140. $tableBox.append('<div class="soul-drag-bar"><div data-type="left">左固定</div><div data-type="none">不固定</div><div data-type="right">右固定</div></div>')
  141. var $dragBar = $tableBox.children('.soul-drag-bar');
  142. $dragBar.children('div').on('mouseenter', function () {
  143. $(this).addClass('active')
  144. }).on('mouseleave', function () {
  145. $(this).removeClass('active')
  146. })
  147. }
  148. $tableHead.find('th').each(function () {
  149. var $this = $(this),
  150. field = $this.data('field'),
  151. key = $this.data('key');
  152. if (!key) {return;}
  153. var keyArray = key.split('-'),
  154. curColumn = myTable.cols[keyArray[1]][keyArray[2]],
  155. curKey = keyArray[1] + '-' + keyArray[2],
  156. isInFixed = $this.parents('.layui-table-fixed').length>0;
  157. // 绑定鼠标按下事件
  158. $(this).find('span:first,.laytable-cell-checkbox')
  159. .css('cursor', 'move')
  160. .on('mousedown', function (e) {
  161. if (e.button !== 0) {
  162. return;
  163. }
  164. e.preventDefault();
  165. var $cloneHead = $this.clone().css('visibility', 'hidden'),
  166. originLeft = $this.position().left,
  167. originTop = $this.offset().top,
  168. disX = e.clientX - originLeft, // 鼠标距离被移动元素左侧的距离
  169. color = $this.parents('tr:eq(0)').css("background-color"),
  170. width = $this.width(), moveDistince = 0,
  171. $that = $(this),
  172. isFixed = curColumn.fixed;
  173. isStart = true;
  174. //区分click、drag事件
  175. // 阻止文本选中
  176. $(document).bind("selectstart", function () {
  177. return false;
  178. });
  179. // 移动事件
  180. $('body').on('mousemove', function (e) {
  181. if (isStart && $cloneHead) {
  182. $tableBox.removeClass('no-left-border');
  183. if (!isDraging) {
  184. if (toolbar) {
  185. $dragBar.attr('data-type', isFixed || 'none')
  186. $dragBar.addClass('active')
  187. }
  188. $this.after($cloneHead);
  189. $this.addClass('isDrag').css({
  190. 'position': 'absolute',
  191. 'z-index': 1,
  192. 'border-left': '1px solid #e6e6e6',
  193. 'background-color': color,
  194. 'width': width + 1
  195. });
  196. if (isSimple) {
  197. //设置蒙板
  198. } else {
  199. (isInFixed ? $fixedBody : $tableBody).find('td[data-key="' + key + '"]').each(function () {
  200. $(this).after($(this).clone().css('visibility', 'hidden').attr('data-clone', ''));
  201. $(this).addClass('isDrag').css({
  202. 'position': 'absolute',
  203. 'z-index': 1,
  204. 'border-left': '1px solid #e6e6e6',
  205. 'background-color': $(this).css('background-color'),
  206. 'width': width + 1
  207. });
  208. })
  209. if ($totalTable.length>0) {
  210. (isInFixed ? $fixedTotalTable : $totalTable).find('td[data-key="' + key + '"]').each(function () {
  211. $(this).after($(this).clone().css('visibility', 'hidden').attr('data-clone', ''));
  212. $(this).addClass('isDrag').css({
  213. 'position': 'absolute',
  214. 'z-index': 1,
  215. 'background-color': $(this).parents('tr:eq(0)').css('background-color'),
  216. 'width': width + 1
  217. });
  218. })
  219. }
  220. }
  221. }
  222. isDraging = true;
  223. var x, y, i, j, tempCols,
  224. left = e.clientX - disX, // 计算当前被移动列左侧位置应该哪里
  225. $leftTh = $cloneHead.prev().prev(),
  226. hasLeftTh = $leftTh.length > 0,
  227. leftKey = hasLeftTh ? $leftTh.data('key').split('-') : [],
  228. $rightTh = $cloneHead.next().hasClass('layui-table-patch') ? [] : $cloneHead.next(),
  229. hasRightTh = $rightTh.length > 0,
  230. rightKey = hasRightTh ? $rightTh.data('key').split('-') : [],
  231. leftMove = hasLeftTh && ($cloneHead.position().left - left > $leftTh.width() / 2.0),
  232. rightMove = hasRightTh && (left - $cloneHead.position().left > $rightTh.width() / 2.0);
  233. moveDistince = Math.abs($cloneHead.position().left - left); //记录移动距离
  234. // 移动到左右两端、checbox/radio 固定列等停止移动
  235. if ($cloneHead.position().left - left > 0
  236. ? !hasLeftTh || !!isFixed !== !!myTable.cols[leftKey[1]][leftKey[2]].fixed
  237. : !hasRightTh || !!isFixed !== !!myTable.cols[rightKey[1]][rightKey[2]].fixed) {
  238. $this.css('left',$cloneHead.position().left);
  239. $tableBody.find('td[data-key="' + key + '"][data-clone]').each(function (e) {
  240. $(this).prev().css('left', $cloneHead.position().left);
  241. })
  242. if ($totalTable.length>0) {
  243. $totalTable.find('td[data-key="' + key + '"][data-clone]').each(function (e) {
  244. $(this).prev().css('left', $cloneHead.position().left);
  245. })
  246. }
  247. $tableBox.addClass('no-left-border');
  248. return;
  249. }
  250. $this.css('left', left);
  251. if (leftMove) {
  252. $cloneHead.after($leftTh);
  253. // 更新隐藏列顺序
  254. $('#soul-columns' + tableId + '>li[data-value=' + field + ']').after($('#soul-columns' + tableId + '>li[data-value=' + field + ']').prev())
  255. // 更新配置信息
  256. for (i = 0; i < myTable.cols.length; i++) {
  257. for (j = 0; j < myTable.cols[i].length; j++) {
  258. if (myTable.cols[i][j].key === curKey) {
  259. x = i;
  260. y = j;
  261. break;
  262. }
  263. }
  264. if (typeof x !== 'undefined' && typeof y !== 'undefined') {
  265. break;
  266. }
  267. }
  268. tempCols = myTable.cols[x][y - 1];
  269. myTable.cols[x][y - 1] = myTable.cols[x][y];
  270. myTable.cols[x][y] = tempCols;
  271. if (myTable.filter && myTable.filter.cache) {
  272. localStorage.setItem(location.pathname + location.hash + myTable.id, _this.deepStringify(myTable.cols))
  273. }
  274. } else if (rightMove) {
  275. $cloneHead.prev().before($rightTh);
  276. // 更新隐藏列顺序
  277. $('#soul-columns' + tableId + '>li[data-value=' + field + ']').before($('#soul-columns' + tableId + '>li[data-value=' + field + ']').next())
  278. // 更新配置信息
  279. for (i = 0; i < myTable.cols.length; i++) {
  280. for (j = 0; j < myTable.cols[i].length; j++) {
  281. if (myTable.cols[i][j].key === curKey) {
  282. x = i;
  283. y = j;
  284. break;
  285. }
  286. }
  287. if (typeof x !== 'undefined' && typeof y !== 'undefined') {
  288. break;
  289. }
  290. }
  291. tempCols = myTable.cols[x][y + 1];
  292. myTable.cols[x][y + 1] = myTable.cols[x][y];
  293. myTable.cols[x][y] = tempCols;
  294. if (myTable.filter && myTable.filter.cache) {
  295. localStorage.setItem(location.pathname + location.hash + myTable.id, _this.deepStringify(myTable.cols))
  296. }
  297. }
  298. $tableBody.find('td[data-key="' + key + '"][data-clone]').each(function () {
  299. $(this).prev().css('left', left);
  300. if (leftMove) {
  301. if ($(this).prev().prev().length !== 0) {
  302. $(this).after($(this).prev().prev());
  303. }
  304. } else if (rightMove) {
  305. if ($(this).next().length !== 0) {
  306. $(this).prev().before($(this).next());
  307. }
  308. }
  309. })
  310. if ($totalTable.length>0) {
  311. $totalTable.find('td[data-key="' + key + '"][data-clone]').each(function () {
  312. $(this).prev().css('left', left);
  313. if (leftMove) {
  314. if ($(this).prev().prev().length !== 0) {
  315. $(this).after($(this).prev().prev());
  316. }
  317. } else if (rightMove) {
  318. if ($(this).next().length !== 0) {
  319. $(this).prev().before($(this).next());
  320. }
  321. }
  322. })
  323. }
  324. /* 拖动隐藏列 */
  325. if (e.clientY - originTop < -15) {
  326. if ($('#column-remove').length === 0) {
  327. $('body').append('<i id="column-remove" class="layui-red layui-icon layui-icon-delete"></i>')
  328. }
  329. $('#column-remove').css({
  330. top: e.clientY - $('#column-remove').height() / 2,
  331. left: e.clientX - $('#column-remove').width() / 2,
  332. 'font-size': (originTop - e.clientY) + 'px'
  333. })
  334. $('#column-remove').show();
  335. } else {
  336. $('#column-remove').hide();
  337. }
  338. }
  339. }).on('mouseup', function () {
  340. $(document).unbind("selectstart");
  341. $('body').off('mousemove').off('mouseup')
  342. if (isStart && $cloneHead) {
  343. isStart = false;
  344. if (isDraging) {
  345. if (curColumn.type !== 'checkbox') {
  346. $that.on('click', function (e) {
  347. e.stopPropagation();
  348. });
  349. }
  350. isDraging = false;
  351. $tableBox.removeClass('no-left-border')
  352. $this.removeClass('isDrag').css({
  353. 'position': 'relative',
  354. 'z-index': 'inherit',
  355. 'left': 'inherit',
  356. 'border-left': 'inherit',
  357. 'width': 'inherit',
  358. 'background-color': 'inherit'
  359. });
  360. $this.next().remove();
  361. var prefKey = $this.prev().data('key');
  362. if (isFixed) {
  363. var $noFixedTh = $tableBox.children('.layui-table-header').children('table').find('th[data-key="' + key + '"]');
  364. if (prefKey) {
  365. $noFixedTh.parent().children('th[data-key="' + prefKey + '"]').after($noFixedTh)
  366. } else {
  367. if (isFixed === 'right') {
  368. if ($this.siblings().length > 0) {
  369. $tableBox.children('.layui-table-header').children('table').find('th[data-key="' + $this.next().data('key') + '"]').prev().after($noFixedTh);
  370. }
  371. } else {
  372. $noFixedTh.parent().prepend('<th class="layui-hide"></th>');
  373. $noFixedTh.parent().children('th:first').after($noFixedTh);
  374. $noFixedTh.parent().children('th:first').remove();
  375. }
  376. }
  377. }
  378. if (isSimple) {
  379. $tableBody.find('td[data-key="' + key + '"]').each(function () {
  380. if (prefKey) {
  381. $(this).parent().children('td[data-key="' + prefKey + '"]').after($(this))
  382. } else {
  383. if (isFixed === 'right') {
  384. if ($this.siblings().length > 0) {
  385. var $preTd = $(this).parent().children('td[data-key="' + $this.next().data('key') + '"]').prev();
  386. if ($preTd.length>0) {
  387. $preTd.after($(this));
  388. } else {
  389. $(this).parent().prepend('<td class="layui-hide"></td>');
  390. $(this).parent().children('td:first').after($(this));
  391. $(this).parent().children('td:first').remove();
  392. }
  393. }
  394. } else {
  395. $(this).parent().prepend('<td class="layui-hide"></td>');
  396. $(this).parent().children('td:first').after($(this));
  397. $(this).parent().children('td:first').remove();
  398. }
  399. }
  400. });
  401. if ($totalTable.length>0) {
  402. $totalTable.find('td[data-key="' + key + '"]').each(function () {
  403. if (prefKey) {
  404. $(this).parent().children('td[data-key="' + prefKey + '"]').after($(this))
  405. } else {
  406. if (isFixed === 'right') {
  407. var $preTd = $(this).parent().children('td[data-key="' + $this.next().data('key') + '"]').prev();
  408. if ($preTd.length>0) {
  409. $preTd.after($(this));
  410. } else {
  411. $(this).parent().prepend('<td class="layui-hide"></td>');
  412. $(this).parent().children('td:first').after($(this));
  413. $(this).parent().children('td:first').remove();
  414. }
  415. } else {
  416. $(this).parent().prepend('<td class="layui-hide"></td>');
  417. $(this).parent().children('td:first').after($(this));
  418. $(this).parent().children('td:first').remove();
  419. }
  420. }
  421. });
  422. }
  423. } else if (isInFixed) {
  424. $noFixedBody.find('td[data-key="' + key + '"]').each(function () {
  425. if (prefKey) {
  426. $(this).parent().children('td[data-key="' + prefKey + '"]').after($(this))
  427. } else {
  428. if (isFixed === 'right') {
  429. if ($this.siblings().length > 0) {
  430. var $preTd = $(this).parent().children('td[data-key="' + $this.next().data('key') + '"]').prev();
  431. if ($preTd.length>0) {
  432. $preTd.after($(this));
  433. } else {
  434. $(this).parent().prepend('<td class="layui-hide"></td>');
  435. $(this).parent().children('td:first').after($(this));
  436. $(this).parent().children('td:first').remove();
  437. }
  438. }
  439. } else {
  440. $(this).parent().prepend('<td class="layui-hide"></td>');
  441. $(this).parent().children('td:first').after($(this));
  442. $(this).parent().children('td:first').remove();
  443. }
  444. }
  445. });
  446. $fixedBody.find('td[data-key="' + key + '"][data-clone]').each(function () {
  447. $(this).prev().removeClass('isDrag').css({
  448. 'position': 'relative',
  449. 'z-index': 'inherit',
  450. 'left': 'inherit',
  451. 'border-left': 'inherit',
  452. 'width': 'inherit',
  453. 'background-color': 'inherit'
  454. });
  455. $(this).remove();
  456. });
  457. if ($totalTable.length>0) {
  458. $noFixedTotalTable.find('td[data-key="' + key + '"]').each(function () {
  459. if (prefKey) {
  460. $(this).parent().children('td[data-key="' + prefKey + '"]').after($(this))
  461. } else {
  462. if (isFixed === 'right') {
  463. var $preTd = $(this).parent().children('td[data-key="' + $this.next().data('key') + '"]').prev();
  464. if ($preTd.length>0) {
  465. $preTd.after($(this));
  466. } else {
  467. $(this).parent().prepend('<td class="layui-hide"></td>');
  468. $(this).parent().children('td:first').after($(this));
  469. $(this).parent().children('td:first').remove();
  470. }
  471. } else {
  472. $(this).parent().prepend('<td class="layui-hide"></td>');
  473. $(this).parent().children('td:first').after($(this));
  474. $(this).parent().children('td:first').remove();
  475. }
  476. }
  477. });
  478. $fixedTotalTable.find('td[data-key="' + key + '"][data-clone]').each(function () {
  479. $(this).prev().removeClass('isDrag').css({
  480. 'position': 'relative',
  481. 'z-index': 'inherit',
  482. 'left': 'inherit',
  483. 'width': 'inherit',
  484. 'background-color': 'inherit'
  485. });
  486. $(this).remove();
  487. });
  488. }
  489. } else {
  490. $tableBody.find('td[data-key="' + key + '"][data-clone]').each(function () {
  491. $(this).prev().removeClass('isDrag').css({
  492. 'position': 'relative',
  493. 'z-index': 'inherit',
  494. 'left': 'inherit',
  495. 'width': 'inherit',
  496. 'background-color': 'inherit'
  497. });
  498. $(this).remove();
  499. });
  500. if ($totalTable.length>0) {
  501. $totalTable.find('td[data-key="' + key + '"][data-clone]').each(function () {
  502. $(this).prev().removeClass('isDrag').css({
  503. 'position': 'relative',
  504. 'z-index': 'inherit',
  505. 'left': 'inherit',
  506. 'width': 'inherit',
  507. 'background-color': 'inherit'
  508. });
  509. $(this).remove();
  510. });
  511. }
  512. }
  513. $cloneHead = null;
  514. // 处理 toolbar 事件
  515. if (toolbar) {
  516. if ($dragBar.children('.active').length > 0 && $dragBar.children('.active').attr('data-type') !== $dragBar.attr('data-type')) {
  517. var targetFix = $dragBar.children('.active').attr('data-type'),
  518. i, j, curPos, targetPos;
  519. for (i = 0; i < myTable.cols.length; i++) {
  520. for (j = 0; j < myTable.cols[i].length; j++) {
  521. if (targetFix==='right' || (targetFix === 'none' && $dragBar.attr('data-type') === 'right')) {
  522. if (typeof targetPos === 'undefined') {
  523. if (myTable.cols[i][j].fixed === 'right') {
  524. targetPos = {x: i, y: j};
  525. } else if (j === myTable.cols[i].length-1) {
  526. targetPos = {x: i, y: j+1};
  527. }
  528. }
  529. } else {
  530. if (typeof targetPos === 'undefined' && (!myTable.cols[i][j].fixed || myTable.cols[i][j].fixed === 'right')) {
  531. targetPos = {x: i, y: j};
  532. }
  533. }
  534. if (myTable.cols[i][j].key === curKey) {
  535. curPos = {x: i, y: j};
  536. }
  537. }
  538. }
  539. curColumn['fixed'] = targetFix === 'none' ? false : targetFix;
  540. if (curPos.y !== targetPos.y) {
  541. myTable.cols[curPos.x].splice(curPos.y, 1);
  542. if (curPos.y < targetPos.y) {
  543. targetPos.y -= 1
  544. }
  545. myTable.cols[targetPos.x].splice(targetPos.y, 0, curColumn)
  546. if (myTable.filter && myTable.filter.cache) {
  547. localStorage.setItem(location.pathname + location.hash + myTable.id, _this.deepStringify(myTable.cols))
  548. }
  549. }
  550. table.reload(tableId)
  551. }
  552. $dragBar.removeClass('active')
  553. }
  554. } else {
  555. $that.unbind('click');
  556. }
  557. if ($('#column-remove').is(':visible')) {
  558. $tableHead.find('thead>tr>th[data-key=' + key + ']').addClass(HIDE);
  559. $tableBody.find('tbody>tr>td[data-key="' + key + '"]').addClass(HIDE);
  560. $totalTable.find('tbody>tr>td[data-key="' + key + '"]').addClass(HIDE);
  561. // 同步配置
  562. curColumn['hide'] = true
  563. if (myTable.filter && myTable.filter.cache) {
  564. localStorage.setItem(location.pathname + location.hash + myTable.id, _this.deepStringify(myTable.cols))
  565. }
  566. // 更新下拉隐藏
  567. $('#soul-columns' + tableId).find('li[data-value="' + field + '"]>input').prop('checked', false);
  568. tableFilter.resize(myTable)
  569. }
  570. $('#column-remove').hide();
  571. }
  572. })
  573. });
  574. })
  575. }
  576. },
  577. rowDrag: function (myTable) {
  578. var _this = this,
  579. $table = $(myTable.elem),
  580. $tableBox = $table.next().children('.layui-table-box'),
  581. $fixedBody = $tableBox.children('.layui-table-fixed').children('.layui-table-body').children('table'),
  582. $noFixedBody = $tableBox.children('.layui-table-body').children('table'),
  583. $tableBody = $.merge($tableBox.children('.layui-table-body').children('table'), $fixedBody),
  584. tableId = myTable.id,
  585. isDraging = false,
  586. trigger = myTable.rowDrag.trigger || 'row',
  587. $trs = trigger === 'row' ? $tableBody.children('tbody').children('tr') : $tableBody.find(trigger);
  588. if (trigger !== 'row') {
  589. $tableBody.find(trigger).css('cursor', 'move')
  590. }
  591. $trs.on('mousedown', function (e) {
  592. if (e.button !== 0) {
  593. return;
  594. }
  595. var $this = trigger === 'row' ? $(this) : $(this).parents('tr:eq(0)'),
  596. index = parseInt($this.data('index')),
  597. $bodyTr = $noFixedBody.children('tbody').children('tr[data-index='+ index +']'),
  598. $cloneTr = $bodyTr.clone().css('visibility', 'hidden'),
  599. $FixBodyTr = $fixedBody.children('tbody').children('tr[data-index='+ index +']'),
  600. bodyScrollTop = $tableBox.children('.layui-table-body').scrollTop(), // 记录当前滚动条位置
  601. originTop = $this.position().top,
  602. disY = e.clientY - originTop; // 鼠标距离被移动元素上侧的距离
  603. $('body').on('mousemove', function (e) {
  604. if (!isDraging) {
  605. isDraging = true;
  606. // 设置鼠标样式
  607. // $table.next().find('style').append('.layui-table-view .layui-table td{cursor: move;}.layui-table tr{transition: none}')
  608. var style = $table.next().find('style')[0],
  609. sheet = style.sheet || style.styleSheet || {};
  610. _this.addCSSRule(sheet, '.layui-table-view .layui-table td', 'cursor: move')
  611. _this.addCSSRule(sheet, '.layui-table tr', 'transition: none')
  612. $tableBox.addClass('noselect'); // 禁止选中文本
  613. $bodyTr.after($cloneTr);
  614. $bodyTr.css({
  615. 'position': 'absolute',
  616. 'z-index': 1
  617. })
  618. $FixBodyTr.each(function () {
  619. $(this).after($(this).clone().css('visibility', 'hidden'))
  620. $(this).css({
  621. 'position': 'absolute',
  622. 'z-index': 102
  623. })
  624. })
  625. }
  626. var top = e.clientY - disY + ($tableBox.children('.layui-table-body').scrollTop() - bodyScrollTop), // 计算当前被移动行top位置应该哪里
  627. trTop = $cloneTr.position().top, //当前行所在位置
  628. $UpTr = $bodyTr.prev(),
  629. hasUpTr = $UpTr.length > 0,
  630. $downTr = $cloneTr.next(),
  631. hasDownTr = $downTr.length > 0,
  632. upMove = hasUpTr && (trTop - top > $UpTr.height() / 2.0),
  633. downMove = hasDownTr && (top - trTop > $downTr.height() / 2.0);
  634. if (trTop - top > 0 ? !hasUpTr : !hasDownTr) {
  635. $bodyTr.css('top', trTop);
  636. $FixBodyTr.each(function () {
  637. $(this).css('top', trTop);
  638. })
  639. return;
  640. }
  641. $bodyTr.css('top', top);
  642. $FixBodyTr.each(function () {
  643. $(this).css('top', top);
  644. })
  645. if (upMove) {
  646. updateDataIndex($bodyTr, -1)
  647. $cloneTr.after(updateDataIndex($UpTr, 1));
  648. $FixBodyTr.each(function () {
  649. updateDataIndex($(this), -1)
  650. $(this).next().after(updateDataIndex($(this).prev(), 1));
  651. })
  652. } else if (downMove) {
  653. updateDataIndex($bodyTr, 1).before(updateDataIndex($downTr, -1))
  654. $FixBodyTr.each(function () {
  655. updateDataIndex($(this), 1);
  656. $(this).before(updateDataIndex($(this).next().next(), -1));
  657. })
  658. }
  659. // 同步 data-index
  660. function updateDataIndex ($el, diff) {
  661. var tempIndex = parseInt($el.data('index')) + diff;
  662. $el.data('index', tempIndex);
  663. $el.attr('data-index', tempIndex);
  664. return $el
  665. }
  666. }).on('mouseup', function (e) {
  667. $('body').off('mousemove').off('mouseup');
  668. if (isDraging) {
  669. isDraging = false;
  670. $tableBox.removeClass('noselect'); // 取消禁止选中文本
  671. $bodyTr.css({'position': 'inherit','z-index': 'inherit'});
  672. $bodyTr.next().remove();
  673. $FixBodyTr.each(function () {
  674. $(this).css({'position': 'inherit','z-index': 'inherit'});
  675. $(this).next().remove()
  676. })
  677. // 恢复样式
  678. var style = $table.next().find('style')[0],
  679. sheet = style.sheet || style.styleSheet || {},
  680. rules = sheet.cssRules || sheet.rules;
  681. layui.each(rules, function(i, item){
  682. if(item.selectorText === ('.layui-table-view .layui-table td')){
  683. item.style.cursor = 'default';
  684. }
  685. });
  686. var newIndex = $this.index();
  687. if (newIndex !== index) { // 有位置变动
  688. if (typeof myTable.rowDrag.done === 'function') {
  689. var cache = layui.table.cache[tableId],
  690. row = cache.splice(index, 1)[0];
  691. cache.splice(newIndex , 0, row);
  692. myTable.rowDrag.done.call(myTable, {
  693. row: row,
  694. newIndex: newIndex,
  695. oldIndex: index,
  696. cache: cache
  697. })
  698. }
  699. }
  700. }
  701. })
  702. })
  703. },
  704. fixTableRemember: function(myTable, dict) {
  705. if (myTable.filter && myTable.filter.cache) {
  706. if (dict && dict.rule) {
  707. myTable.cols[dict.rule.selectorText.split('-')[3]][dict.rule.selectorText.split('-')[4]].width = dict.rule.style.width.replace('px','');
  708. }
  709. localStorage.setItem(location.pathname + location.hash + myTable.id, this.deepStringify(myTable.cols))
  710. }
  711. },
  712. overflow: function (myTable) {
  713. var options = {};
  714. if (typeof myTable.overflow === 'string') {
  715. options = {
  716. type: myTable.overflow
  717. }
  718. } else if (typeof myTable.overflow === 'object') {
  719. options = myTable.overflow
  720. } else {
  721. return;
  722. }
  723. var $table = $(myTable.elem),
  724. layBody = $table.next().find('.layui-table-body'),
  725. tooltipIndex,
  726. hoverTime = options.hoverTime || 0,
  727. tooltipTimeOut,
  728. color = options.color || 'white',
  729. bgColor = options.bgColor || 'black',
  730. minWidth = options.minWidth || 300,
  731. maxWidth = options.maxWidth || 300;
  732. if (options.type === 'tips') {
  733. layBody.off('mouseenter', 'td').on('mouseenter', 'td', function () {
  734. var _this = this;
  735. tooltipTimeOut = setTimeout(function () {
  736. toopTip.call(_this)
  737. }, hoverTime)
  738. }).on('mouseleave', 'td', function () {
  739. toopTip.call(this, 'hide')
  740. })
  741. function toopTip(hide) {
  742. clearTimeout(tooltipTimeOut);
  743. var othis = $(this)
  744. ,elemCell = othis.children('.layui-table-cell')
  745. ,width = elemCell.outerWidth()
  746. ,layerWidth = width < minWidth ? minWidth : width > maxWidth ? maxWidth : width;
  747. if(othis.data('off')) return;
  748. if (hide) {
  749. layer.close(tooltipIndex)
  750. } else if(elemCell.prop('scrollWidth') > width) {
  751. tooltipIndex = layer.tips('<span style="color: '+color+'">' + $(this).text() + '</span>', this, {
  752. tips: [1, bgColor],
  753. maxWidth: layerWidth,
  754. time: 0
  755. });
  756. }
  757. }
  758. } else if (options.type === 'title') {
  759. layBody.off('mouseenter', 'td').on('mouseenter', 'td', function () {
  760. var othis = $(this)
  761. ,elemCell = othis.children('.layui-table-cell');
  762. if(othis.data('off')) return;
  763. if(elemCell.prop('scrollWidth') > elemCell.outerWidth()) {
  764. elemCell.attr('title', $(this).text())
  765. }
  766. })
  767. }
  768. },
  769. // 右键菜单配置
  770. contextmenu: function (myTable) {
  771. var $table = $(myTable.elem),
  772. $tableBox = $table.next().children('.layui-table-box'),
  773. $tableHead = $.merge($tableBox.children('.layui-table-header').children('table'),$tableBox.children('.layui-table-fixed').children('.layui-table-header').children('table')),
  774. $fixedBody = $tableBox.children('.layui-table-fixed').children('.layui-table-body').children('table'),
  775. $tableBody = $.merge($tableBox.children('.layui-table-body').children('table'), $fixedBody),
  776. $totalTable = $table.next().children('.layui-table-total').children('table'),
  777. tableId = myTable.id,
  778. header = myTable.contextmenu ? myTable.contextmenu.header : '',
  779. body = myTable.contextmenu ? myTable.contextmenu.body : '',
  780. total = myTable.contextmenu ? myTable.contextmenu.total: '',
  781. options = {header: {box: $tableHead, tag:'th', opts: header, cols:{}},
  782. body: {box: $tableBody, tag:'td', opts: body, cols:{}, isBody: true},
  783. total: {box: $totalTable, tag: 'td', opts: total, cols:{}}},
  784. hasColsContext = false;
  785. for (var i = 0; i < myTable.cols.length; i++) {
  786. for (var j = 0; j < myTable.cols[i].length; j++) {
  787. if (myTable.cols[i][j].contextmenu) {
  788. hasColsContext = true
  789. options.header.cols[myTable.cols[i][j].key] = myTable.cols[i][j].contextmenu.header
  790. options.body.cols[myTable.cols[i][j].key] = myTable.cols[i][j].contextmenu.body
  791. options.total.cols[myTable.cols[i][j].key] = myTable.cols[i][j].contextmenu.total
  792. }
  793. }
  794. }
  795. if (!myTable.contextmenu && !hasColsContext) {
  796. return;
  797. }
  798. for (var name in options) {
  799. (function (name) {
  800. options[name].box.find(options[name].tag).on('contextmenu', function (e) {
  801. $('#soul-table-contextmenu-wrapper').remove();
  802. $('body').append('<div id="soul-table-contextmenu-wrapper"></div>');
  803. $('#soul-table-contextmenu-wrapper').on('click', function (e) {
  804. e.stopPropagation()
  805. })
  806. var curColsOpts = options[name].cols[$(this).data('key').substr($(this).data('key').indexOf('-')+1)];
  807. if (curColsOpts === false) {
  808. return false
  809. } else if (curColsOpts && curColsOpts.length>0) {
  810. genePanel($('#soul-table-contextmenu-wrapper'), e.clientX, e.clientY, curColsOpts, $(this), options[name].box, options[name].tag, options[name].isBody);
  811. return false
  812. } else if (options[name].opts === false) {
  813. return false
  814. } else if (options[name].opts && options[name].opts.length>0) {
  815. genePanel($('#soul-table-contextmenu-wrapper'), e.clientX, e.clientY, options[name].opts, $(this), options[name].box, options[name].tag, options[name].isBody);
  816. return false
  817. }
  818. })
  819. })(name)
  820. }
  821. $('body').on('click', function () {
  822. $('#soul-table-contextmenu-wrapper').remove();
  823. })
  824. function genePanel($parent, left, top, options, $this, box, tag, isBody) {
  825. var html = [], i;
  826. html.push('<ul class="soul-table-contextmenu">');
  827. for (i = 0; i < options.length; i++) {
  828. html.push('<li data-index="'+i+'" class="'+(options[i].children && options[i].children.length>0 ? 'contextmenu-children' : '')+'">')
  829. if (options[i].icon) {
  830. html.push('<i class="prefixIcon '+options[i].icon+'" />')
  831. } else {
  832. html.push('<i class="prefixIcon" />')
  833. }
  834. html.push(options[i].name)
  835. if (options[i].children && options[i].children.length>0) {
  836. html.push('<i class="endIcon layui-icon layui-icon-right" />')
  837. }
  838. html.push('</li>')
  839. }
  840. html.push('</ul>');
  841. $parent.append(html.join(''));
  842. var $curPanel = $parent.children().last();
  843. if (top + $curPanel.outerHeight() > $('body').prop('scrollHeight')) {
  844. top = top - $curPanel.outerHeight()
  845. if (top < 0) {
  846. top = 0
  847. }
  848. }
  849. if ($parent.parent().data('direction') === 'left' && ($parent.offset().left - $curPanel.outerWidth()) > 0) {
  850. left = - $curPanel.outerWidth();
  851. $curPanel.data('direction', 'left')
  852. } else if (left + $curPanel.outerWidth() + $parent.offset().left > $('body').prop('scrollWidth')) {
  853. left = left - $curPanel.outerWidth() - $parent.outerWidth()
  854. if (left + $parent.offset().left < 0) {
  855. left = - $parent.offset().left
  856. }
  857. $curPanel.data('direction', 'left')
  858. }
  859. $curPanel.css({
  860. top: top + 'px',
  861. left: left + 'px'
  862. })
  863. for (i = 0; i < options.length; i++) {
  864. if (typeof options[i].click === "function") {
  865. (function (i) {
  866. $parent.children('.soul-table-contextmenu:last').children('li[data-index="'+i+'"]').on('click', function () {
  867. var index = $this.parents('tr:eq(0)').data('index'),
  868. tr = box.find('tr[data-index="'+ index +'"]'),
  869. row = layui.table.cache[tableId][index];
  870. options[i].click.call(myTable, {
  871. cell: $this,
  872. elem: tag === 'th' ? $this : isBody ? box.children('tbody').children('tr[data-index="'+index+'"]').children('[data-key="'+$this.data('key')+'"]') : box.find('[data-key="'+$this.data('key')+'"]'),
  873. trElem: box.children('tbody').children('tr[data-index="'+index+'"]'),
  874. text: $this.text(),
  875. field: $this.data('field'),
  876. del: !isBody? '' : function() {
  877. table.cache[tableId][index] = [];
  878. tr.remove();
  879. table.resize(tableId);
  880. },
  881. update: !isBody?'':function(fields) {
  882. fields = fields || {};
  883. layui.each(fields, function(key, value){
  884. if(key in row){
  885. var templet, td = tr.children('td[data-field="'+ key +'"]');
  886. row[key] = value;
  887. table.eachCols(tableId, function(i, item2){
  888. if(item2.field == key && item2.templet){
  889. templet = item2.templet;
  890. }
  891. });
  892. td.children('.layui-table-cell').html(function(){
  893. return templet ? function(){
  894. return typeof templet === 'function'
  895. ? templet(row)
  896. : layui.laytpl($(templet).html() || value).render(row)
  897. }() : value;
  898. }());
  899. td.data('content', value);
  900. }
  901. });
  902. },
  903. row: isBody ? row : {},
  904. })
  905. $('#soul-table-contextmenu-wrapper').remove();
  906. })
  907. })(i)
  908. }
  909. }
  910. $parent.children('.soul-table-contextmenu:last').children('li').on('mouseenter', function (e) {
  911. e.stopPropagation()
  912. $(this).siblings('.contextmenu-children').children('ul').remove();
  913. if ($(this).hasClass('contextmenu-children')) {
  914. genePanel($(this), $(this).outerWidth(), $(this).position().top, options[$(this).data('index')].children, $this, box, tag, isBody)
  915. }
  916. })
  917. }
  918. },
  919. fixTotal: function (myTable) {
  920. var $table = $(myTable.elem),
  921. $total = $table.next().children('.layui-table-total'),
  922. style = $table.next().find('style')[0],
  923. sheet = style.sheet || style.styleSheet || {};
  924. if ($total.length > 0) {
  925. var $tableBox = $table.next().children('.layui-table-box'),
  926. $fixedLeft = $tableBox.children('.layui-table-fixed-l').children('.layui-table-body').children('table').children('tbody').children('tr:eq(0)').children('td'),
  927. $fixedRight = $tableBox.children('.layui-table-fixed-r').children('.layui-table-body').children('table').children('tbody').children('tr:eq(0)').children('td'),
  928. html = [];
  929. $total.children('.layui-table-total-fixed').remove()
  930. if ($fixedLeft.length > 0) {
  931. this.addCSSRule(sheet, '.layui-table-total-fixed-l .layui-table-patch', 'display: none')
  932. $table.next().css('position', 'relative');
  933. html.push('<table style="position: absolute;background-color: #fff;left: 0;top: '+ ($total.position().top + 1) +'px" cellspacing="0" cellpadding="0" border="0" class="layui-table layui-table-total-fixed layui-table-total-fixed-l"><tbody><tr>');
  934. $fixedLeft.each(function () {
  935. if ($(this).data('key')) {
  936. html.push($total.children('table:eq(0)').find('[data-key="' + $(this).data('key') + '"]').prop("outerHTML"))
  937. }
  938. })
  939. html.push('</tr></tbody></table>');
  940. $total.append(html.join(''))
  941. }
  942. if ($fixedRight.length > 0) {
  943. this.addCSSRule(sheet, '.layui-table-total-fixed-r td:first-child', 'border-left:1px solid #e6e6e6')
  944. this.addCSSRule(sheet, '.layui-table-total-fixed-r td:last-child', 'border-left: none')
  945. $table.next().css('position', 'relative');
  946. html = [];
  947. html.push('<table style="position: absolute;background-color: #fff;right: 0;top: '+ ($total.position().top + 1) +'px" cellspacing="0" cellpadding="0" border="0" class="layui-table layui-table-total-fixed layui-table-total-fixed-r"><tbody><tr>');
  948. $fixedRight.each(function () {
  949. html.push($total.children('table:eq(0)').find('[data-key="' + $(this).data('key') + '"]').prop("outerHTML"))
  950. })
  951. html.push('</tr></tbody></table>')
  952. $total.append(html.join(''))
  953. }
  954. }
  955. },
  956. fixResizeRightFixed: function (myTable) {
  957. var _this = this,
  958. $table = $(myTable.elem),
  959. $tableBox = $table.next().children('.layui-table-box'),
  960. $fixedHead = $tableBox.children('.layui-table-fixed-r').children('.layui-table-header').children('table'),
  961. dict = {},_BODY = $('body'),_DOC = $(document), resizing, ELEM_SORT='layui-table-sort', ELEM_NO_SORT='layui-table-sort-invalid';
  962. if ($fixedHead.length>0) {
  963. $fixedHead.find('th').off('mousemove').on('mousemove', function (e) {
  964. var othis = $(this)
  965. ,oLeft = othis.offset().left
  966. ,pLeft = e.clientX - oLeft;
  967. if(othis.data('unresize') || dict.resizeStart){
  968. return;
  969. }
  970. if (othis.width() - pLeft <= 10) {
  971. _BODY.css('cursor', 'initial')
  972. }
  973. dict.allowResize = pLeft <= 10; //是否处于拖拽允许区域
  974. _BODY.css('cursor', (dict.allowResize ? 'col-resize' : ''));
  975. }).off('mousedown').on('mousedown', function (e) {
  976. var othis = $(this);
  977. if(dict.allowResize){
  978. othis.find('.'+ELEM_SORT).removeClass(ELEM_SORT).addClass(ELEM_NO_SORT)
  979. var key = othis.data('key');
  980. e.preventDefault();
  981. dict.resizeStart = true; //开始拖拽
  982. dict.offset = [e.clientX, e.clientY]; //记录初始坐标
  983. _this.getCssRule(myTable, key, function(item){
  984. var width = item.style.width || othis.outerWidth();
  985. dict.rule = item;
  986. dict.ruleWidth = parseFloat(width);
  987. dict.othis = othis;
  988. dict.minWidth = othis.data('minwidth') || myTable.cellMinWidth;
  989. });
  990. }
  991. });
  992. //拖拽中
  993. _DOC.on('mousemove', function(e){
  994. if(dict.resizeStart){
  995. layui.soulTable.fixTableRemember(myTable, dict)
  996. e.preventDefault();
  997. if(dict.rule){
  998. var setWidth = dict.ruleWidth - e.clientX + dict.offset[0];
  999. if(setWidth < dict.minWidth) setWidth = dict.minWidth;
  1000. dict.rule.style.width = setWidth + 'px';
  1001. }
  1002. resizing = 1
  1003. }
  1004. }).on('mouseup', function(e){
  1005. if(dict.resizeStart){
  1006. setTimeout(function () {
  1007. dict.othis.find('.'+ELEM_NO_SORT).removeClass(ELEM_NO_SORT).addClass(ELEM_SORT)
  1008. _BODY.css('cursor', '');
  1009. dict = {};
  1010. _this.scrollPatch(myTable);
  1011. }, 30)
  1012. }
  1013. if(resizing === 2){
  1014. resizing = null;
  1015. }
  1016. });
  1017. }
  1018. },
  1019. fixFixedScroll: function (myTable) {
  1020. var $table = $(myTable.elem),
  1021. layFixed = $table.next().children('.layui-table-box').children('.layui-table-fixed'),
  1022. layMain = $table.next().children('.layui-table-box').children('.layui-table-main');
  1023. layFixed.on('mouseenter', function () {
  1024. $(this).children('.layui-table-body').addClass('soul-fixed-scroll').on('scroll', function () {
  1025. var scrollTop = $(this).scrollTop()
  1026. // layFixed.children('.layui-table-body[class!="soul-fixed-scroll"]').scrollTop(scrollTop);
  1027. layMain.scrollTop(scrollTop);
  1028. })
  1029. }).on('mouseleave', function () {
  1030. $(this).children('.layui-table-body').removeClass('soul-fixed-scroll').off('scroll');
  1031. })
  1032. },
  1033. scrollPatch: function (myTable) {
  1034. var $table = $(myTable.elem),
  1035. layHeader = $table.next().children('.layui-table-box').children('.layui-table-header'),
  1036. layTotal = $table.next().children('.layui-table-total'),
  1037. layMain = $table.next().children('.layui-table-box').children('.layui-table-main'),
  1038. layFixed = $table.next().children('.layui-table-box').children('.layui-table-fixed'),
  1039. layFixRight = $table.next().children('.layui-table-box').children('.layui-table-fixed-r'),
  1040. layMainTable = layMain.children('table'),
  1041. scollWidth = layMain.width() - layMain.prop('clientWidth'),
  1042. scollHeight = layMain.height() - layMain.prop('clientHeight'),
  1043. outWidth = layMainTable.outerWidth() - layMain.width() //表格内容器的超出宽度
  1044. //添加补丁
  1045. ,addPatch = function(elem){
  1046. if(scollWidth && scollHeight){
  1047. elem = elem.eq(0);
  1048. if(!elem.find('.layui-table-patch')[0]){
  1049. var patchElem = $('<th class="layui-table-patch"><div class="layui-table-cell"></div></th>'); //补丁元素
  1050. patchElem.find('div').css({
  1051. width: scollWidth
  1052. });
  1053. elem.find('tr').append(patchElem);
  1054. }
  1055. } else {
  1056. elem.find('.layui-table-patch').remove();
  1057. }
  1058. }
  1059. addPatch(layHeader);
  1060. addPatch(layTotal);
  1061. //固定列区域高度
  1062. var mainHeight = layMain.height()
  1063. ,fixHeight = mainHeight - scollHeight;
  1064. layFixed.find('.layui-table-body').css('height', layMainTable.height() >= fixHeight ? fixHeight : 'auto');
  1065. //表格宽度小于容器宽度时,隐藏固定列
  1066. layFixRight[outWidth > 0 ? 'removeClass' : 'addClass'](HIDE);
  1067. //操作栏
  1068. layFixRight.css('right', scollWidth - 1);
  1069. },
  1070. /**
  1071. * 一键粘贴
  1072. * @param {String} text [需要 copy 的属性,默认是 innerText,主要用途例如赋值 a 标签上的 href 链接]
  1073. *
  1074. * range + selection
  1075. *
  1076. * 1.创建一个 range
  1077. * 2.把内容放入 range
  1078. * 3.把 range 放入 selection
  1079. *
  1080. * 注意:参数 attr 不能是自定义属性
  1081. * 注意:对于 user-select: none 的元素无效
  1082. * 注意:当 id 为 false 且 attr 不会空,会直接复制 attr 的内容
  1083. */
  1084. copy: function (text) {
  1085. var target;
  1086. if (text) {
  1087. target = document.createElement('div');
  1088. target.id = 'tempTarget';
  1089. target.style.opacity = '0';
  1090. target.innerText = text;
  1091. document.body.appendChild(target);
  1092. } else {
  1093. target = document.querySelector('#' + id);
  1094. }
  1095. try {
  1096. var range = document.createRange();
  1097. range.selectNode(target);
  1098. window.getSelection().removeAllRanges();
  1099. window.getSelection().addRange(range);
  1100. document.execCommand('copy');
  1101. window.getSelection().removeAllRanges();
  1102. } catch (e) {
  1103. console.log('复制失败')
  1104. }
  1105. if (text) {
  1106. // remove temp target
  1107. target.parentElement.removeChild(target);
  1108. }
  1109. },
  1110. addCSSRule: function(sheet, selector, rules, index) {
  1111. if ('insertRule' in sheet) {
  1112. sheet.insertRule(selector + '{' + rules + '}', index)
  1113. } else if ('addRule' in sheet) {
  1114. sheet.addRule(selector, rules, index)
  1115. }
  1116. },
  1117. deepStringify: function (obj) {
  1118. var JSON_SERIALIZE_FIX = {
  1119. PREFIX : "[[JSON_FUN_PREFIX_",
  1120. SUFFIX : "_JSON_FUN_SUFFIX]]"
  1121. };
  1122. return JSON.stringify(obj,function(key, value){
  1123. if(typeof value === 'function'){
  1124. return JSON_SERIALIZE_FIX.PREFIX+value.toString()+JSON_SERIALIZE_FIX.SUFFIX;
  1125. }
  1126. return value;
  1127. });
  1128. },
  1129. deepParse: function (str) {
  1130. var JSON_SERIALIZE_FIX = {
  1131. PREFIX : "[[JSON_FUN_PREFIX_",
  1132. SUFFIX : "_JSON_FUN_SUFFIX]]"
  1133. };
  1134. return JSON.parse(str,function(key, value){
  1135. if(typeof value === 'string' &&
  1136. value.indexOf(JSON_SERIALIZE_FIX.SUFFIX)>0 && value.indexOf(JSON_SERIALIZE_FIX.PREFIX)===0){
  1137. return eval("("+value.replace(JSON_SERIALIZE_FIX.PREFIX,"").replace(JSON_SERIALIZE_FIX.SUFFIX,"")+")");
  1138. }
  1139. return value;
  1140. })||{};
  1141. },
  1142. clearFilter: function (myTable) {
  1143. tableFilter.clearFilter(myTable);
  1144. },
  1145. cache: tableFilter.cache
  1146. }
  1147. // 输出
  1148. exports('soulTable', mod);
  1149. });