table.js 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963
  1. /**
  2. @Name:layui.table 表格操作
  3. @Author:贤心
  4. @License:MIT
  5. */
  6. layui.define(['laytpl', 'laypage', 'layer', 'form', 'util'], function(exports){
  7. "use strict";
  8. var $ = layui.$
  9. ,laytpl = layui.laytpl
  10. ,laypage = layui.laypage
  11. ,layer = layui.layer
  12. ,form = layui.form
  13. ,util = layui.util
  14. ,hint = layui.hint()
  15. ,device = layui.device()
  16. //外部接口
  17. ,table = {
  18. config: {
  19. checkName: 'LAY_CHECKED' //是否选中状态的字段名
  20. ,indexName: 'LAY_TABLE_INDEX' //下标索引名
  21. } //全局配置项
  22. ,cache: {} //数据缓存
  23. ,index: layui.table ? (layui.table.index + 10000) : 0
  24. //设置全局项
  25. ,set: function(options){
  26. var that = this;
  27. that.config = $.extend({}, that.config, options);
  28. return that;
  29. }
  30. //事件监听
  31. ,on: function(events, callback){
  32. return layui.onevent.call(this, MOD_NAME, events, callback);
  33. }
  34. }
  35. //操作当前实例
  36. ,thisTable = function(){
  37. var that = this
  38. ,options = that.config
  39. ,id = options.id || options.index;
  40. if(id){
  41. thisTable.that[id] = that; //记录当前实例对象
  42. thisTable.config[id] = options; //记录当前实例配置项
  43. }
  44. return {
  45. config: options
  46. ,reload: function(options){
  47. that.reload.call(that, options);
  48. }
  49. ,setColsWidth: function(){
  50. that.setColsWidth.call(that);
  51. }
  52. ,resize: function(){ //重置表格尺寸/结构
  53. that.resize.call(that);
  54. }
  55. }
  56. }
  57. //获取当前实例配置项
  58. ,getThisTableConfig = function(id){
  59. var config = thisTable.config[id];
  60. if(!config) hint.error('The ID option was not found in the table instance');
  61. return config || null;
  62. }
  63. //解析自定义模板数据
  64. ,parseTempData = function(item3, content, tplData, text){
  65. var str = item3.templet ? function(){
  66. return typeof item3.templet === 'function'
  67. ? item3.templet(tplData)
  68. : laytpl($(item3.templet).html() || String(content)).render(tplData)
  69. }() : content;
  70. return text ? $('<div>'+ str +'</div>').text() : str;
  71. }
  72. //字符常量
  73. ,MOD_NAME = 'table', ELEM = '.layui-table', THIS = 'layui-this', SHOW = 'layui-show', HIDE = 'layui-hide', DISABLED = 'layui-disabled', NONE = 'layui-none'
  74. ,ELEM_VIEW = 'layui-table-view', ELEM_TOOL = '.layui-table-tool', ELEM_BOX = '.layui-table-box', ELEM_INIT = '.layui-table-init', ELEM_HEADER = '.layui-table-header', ELEM_BODY = '.layui-table-body', ELEM_MAIN = '.layui-table-main', ELEM_FIXED = '.layui-table-fixed', ELEM_FIXL = '.layui-table-fixed-l', ELEM_FIXR = '.layui-table-fixed-r', ELEM_TOTAL = '.layui-table-total', ELEM_PAGE = '.layui-table-page', ELEM_SORT = '.layui-table-sort', ELEM_EDIT = 'layui-table-edit', ELEM_HOVER = 'layui-table-hover'
  75. //thead区域模板
  76. ,TPL_HEADER = function(options){
  77. var rowCols = '{{#if(item2.colspan){}} colspan="{{item2.colspan}}"{{#} if(item2.rowspan){}} rowspan="{{item2.rowspan}}"{{#}}}';
  78. options = options || {};
  79. return ['<table cellspacing="0" cellpadding="0" border="0" class="layui-table" '
  80. ,'{{# if(d.data.skin){ }}lay-skin="{{d.data.skin}}"{{# } }} {{# if(d.data.size){ }}lay-size="{{d.data.size}}"{{# } }} {{# if(d.data.even){ }}lay-even{{# } }}>'
  81. ,'<thead>'
  82. ,'{{# layui.each(d.data.cols, function(i1, item1){ }}'
  83. ,'<tr>'
  84. ,'{{# layui.each(item1, function(i2, item2){ }}'
  85. ,'{{# if(item2.fixed && item2.fixed !== "right"){ left = true; } }}'
  86. ,'{{# if(item2.fixed === "right"){ right = true; } }}'
  87. ,function(){
  88. if(options.fixed && options.fixed !== 'right'){
  89. return '{{# if(item2.fixed && item2.fixed !== "right"){ }}';
  90. }
  91. if(options.fixed === 'right'){
  92. return '{{# if(item2.fixed === "right"){ }}';
  93. }
  94. return '';
  95. }()
  96. ,'{{# var isSort = !(item2.colGroup) && item2.sort; }}'
  97. ,'<th data-field="{{ item2.field||i2 }}" data-key="{{d.index}}-{{i1}}-{{i2}}" {{# if( item2.parentKey){ }}data-parentkey="{{ item2.parentKey }}"{{# } }} {{# if(item2.minWidth){ }}data-minwidth="{{item2.minWidth}}"{{# } }} '+ rowCols +' {{# if(item2.unresize || item2.colGroup){ }}data-unresize="true"{{# } }} class="{{# if(item2.hide){ }}layui-hide{{# } }}{{# if(isSort){ }} layui-unselect{{# } }}{{# if(!item2.field){ }} layui-table-col-special{{# } }}">'
  98. ,'<div class="layui-table-cell laytable-cell-'
  99. ,'{{# if(item2.colGroup){ }}'
  100. ,'group'
  101. ,'{{# } else { }}'
  102. ,'{{d.index}}-{{i1}}-{{i2}}'
  103. ,'{{# if(item2.type !== "normal"){ }}'
  104. ,' laytable-cell-{{ item2.type }}'
  105. ,'{{# } }}'
  106. ,'{{# } }}'
  107. ,'" {{#if(item2.align){}}align="{{item2.align}}"{{#}}}>'
  108. ,'{{# if(item2.type === "checkbox"){ }}' //复选框
  109. ,'<input type="checkbox" name="layTableCheckbox" lay-skin="primary" lay-filter="layTableAllChoose" {{# if(item2[d.data.checkName]){ }}checked{{# }; }}>'
  110. ,'{{# } else { }}'
  111. ,'<span>{{item2.title||""}}</span>'
  112. ,'{{# if(isSort){ }}'
  113. ,'<span class="layui-table-sort layui-inline"><i class="layui-edge layui-table-sort-asc" title="升序"></i><i class="layui-edge layui-table-sort-desc" title="降序"></i></span>'
  114. ,'{{# } }}'
  115. ,'{{# } }}'
  116. ,'</div>'
  117. ,'</th>'
  118. ,(options.fixed ? '{{# }; }}' : '')
  119. ,'{{# }); }}'
  120. ,'</tr>'
  121. ,'{{# }); }}'
  122. ,'</thead>'
  123. ,'</table>'].join('');
  124. }
  125. //tbody区域模板
  126. ,TPL_BODY = ['<table cellspacing="0" cellpadding="0" border="0" class="layui-table" '
  127. ,'{{# if(d.data.skin){ }}lay-skin="{{d.data.skin}}"{{# } }} {{# if(d.data.size){ }}lay-size="{{d.data.size}}"{{# } }} {{# if(d.data.even){ }}lay-even{{# } }}>'
  128. ,'<tbody></tbody>'
  129. ,'</table>'].join('')
  130. //主模板
  131. ,TPL_MAIN = ['<div class="layui-form layui-border-box {{d.VIEW_CLASS}}" lay-filter="LAY-table-{{d.index}}" lay-id="{{ d.data.id }}" style="{{# if(d.data.width){ }}width:{{d.data.width}}px;{{# } }} {{# if(d.data.height){ }}height:{{d.data.height}}px;{{# } }}">'
  132. ,'{{# if(d.data.toolbar){ }}'
  133. ,'<div class="layui-table-tool">'
  134. ,'<div class="layui-table-tool-temp"></div>'
  135. ,'<div class="layui-table-tool-self"></div>'
  136. ,'</div>'
  137. ,'{{# } }}'
  138. ,'<div class="layui-table-box">'
  139. ,'{{# if(d.data.loading){ }}'
  140. ,'<div class="layui-table-init" style="background-color: #fff;">'
  141. ,'<i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop"></i>'
  142. ,'</div>'
  143. ,'{{# } }}'
  144. ,'{{# var left, right; }}'
  145. ,'<div class="layui-table-header">'
  146. ,TPL_HEADER()
  147. ,'</div>'
  148. ,'<div class="layui-table-body layui-table-main">'
  149. ,TPL_BODY
  150. ,'</div>'
  151. ,'{{# if(left){ }}'
  152. ,'<div class="layui-table-fixed layui-table-fixed-l">'
  153. ,'<div class="layui-table-header">'
  154. ,TPL_HEADER({fixed: true})
  155. ,'</div>'
  156. ,'<div class="layui-table-body">'
  157. ,TPL_BODY
  158. ,'</div>'
  159. ,'</div>'
  160. ,'{{# }; }}'
  161. ,'{{# if(right){ }}'
  162. ,'<div class="layui-table-fixed layui-table-fixed-r">'
  163. ,'<div class="layui-table-header">'
  164. ,TPL_HEADER({fixed: 'right'})
  165. ,'<div class="layui-table-mend"></div>'
  166. ,'</div>'
  167. ,'<div class="layui-table-body">'
  168. ,TPL_BODY
  169. ,'</div>'
  170. ,'</div>'
  171. ,'{{# }; }}'
  172. ,'</div>'
  173. ,'{{# if(d.data.totalRow){ }}'
  174. ,'<div class="layui-table-total">'
  175. ,'<table cellspacing="0" cellpadding="0" border="0" class="layui-table" '
  176. ,'{{# if(d.data.skin){ }}lay-skin="{{d.data.skin}}"{{# } }} {{# if(d.data.size){ }}lay-size="{{d.data.size}}"{{# } }} {{# if(d.data.even){ }}lay-even{{# } }}>'
  177. ,'<tbody><tr><td><div class="layui-table-cell" style="visibility: hidden;">Total</div></td></tr></tbody>'
  178. , '</table>'
  179. ,'</div>'
  180. ,'{{# } }}'
  181. ,'{{# if(d.data.page){ }}'
  182. ,'<div class="layui-table-page">'
  183. ,'<div id="layui-table-page{{d.index}}"></div>'
  184. ,'</div>'
  185. ,'{{# } }}'
  186. ,'<style>'
  187. ,'{{# layui.each(d.data.cols, function(i1, item1){'
  188. ,'layui.each(item1, function(i2, item2){ }}'
  189. ,'.laytable-cell-{{d.index}}-{{i1}}-{{i2}}{ '
  190. ,'{{# if(item2.width){ }}'
  191. ,'width: {{item2.width}}px;'
  192. ,'{{# } }}'
  193. ,' }'
  194. ,'{{# });'
  195. ,'}); }}'
  196. ,'</style>'
  197. ,'</div>'].join('')
  198. ,_WIN = $(window)
  199. ,_DOC = $(document)
  200. //构造器
  201. ,Class = function(options){
  202. var that = this;
  203. that.index = ++table.index;
  204. that.config = $.extend({}, that.config, table.config, options);
  205. that.render();
  206. };
  207. //默认配置
  208. Class.prototype.config = {
  209. limit: 10 //每页显示的数量
  210. ,loading: true //请求数据时,是否显示loading
  211. ,cellMinWidth: 60 //所有单元格默认最小宽度
  212. ,defaultToolbar: ['filter', 'exports', 'print'] //工具栏右侧图标
  213. ,autoSort: true //是否前端自动排序。如果否,则需自主排序(通常为服务端处理好排序)
  214. ,text: {
  215. none: '无数据'
  216. }
  217. };
  218. //表格渲染
  219. Class.prototype.render = function(){
  220. var that = this
  221. ,options = that.config;
  222. options.elem = $(options.elem);
  223. options.where = options.where || {};
  224. options.id = options.id || options.elem.attr('id') || that.index;
  225. //请求参数的自定义格式
  226. options.request = $.extend({
  227. pageName: 'page'
  228. ,limitName: 'limit'
  229. }, options.request)
  230. //响应数据的自定义格式
  231. options.response = $.extend({
  232. statusName: 'code'
  233. ,statusCode: 0
  234. ,msgName: 'msg'
  235. ,dataName: 'data'
  236. ,countName: 'count'
  237. }, options.response);
  238. //如果 page 传入 laypage 对象
  239. if(typeof options.page === 'object'){
  240. options.limit = options.page.limit || options.limit;
  241. options.limits = options.page.limits || options.limits;
  242. that.page = options.page.curr = options.page.curr || 1;
  243. delete options.page.elem;
  244. delete options.page.jump;
  245. }
  246. if(!options.elem[0]) return that;
  247. //高度铺满:full-差距值
  248. if(options.height && /^full-\d+$/.test(options.height)){
  249. that.fullHeightGap = options.height.split('-')[1];
  250. options.height = _WIN.height() - that.fullHeightGap;
  251. }
  252. //初始化一些参数
  253. that.setInit();
  254. //开始插入替代元素
  255. var othis = options.elem
  256. ,hasRender = othis.next('.' + ELEM_VIEW)
  257. //主容器
  258. ,reElem = that.elem = $(laytpl(TPL_MAIN).render({
  259. VIEW_CLASS: ELEM_VIEW
  260. ,data: options
  261. ,index: that.index //索引
  262. }));
  263. options.index = that.index;
  264. that.key = options.id || options.index;
  265. //生成替代元素
  266. hasRender[0] && hasRender.remove(); //如果已经渲染,则Rerender
  267. othis.after(reElem);
  268. //各级容器
  269. that.layTool = reElem.find(ELEM_TOOL);
  270. that.layBox = reElem.find(ELEM_BOX);
  271. that.layHeader = reElem.find(ELEM_HEADER);
  272. that.layMain = reElem.find(ELEM_MAIN);
  273. that.layBody = reElem.find(ELEM_BODY);
  274. that.layFixed = reElem.find(ELEM_FIXED);
  275. that.layFixLeft = reElem.find(ELEM_FIXL);
  276. that.layFixRight = reElem.find(ELEM_FIXR);
  277. that.layTotal = reElem.find(ELEM_TOTAL);
  278. that.layPage = reElem.find(ELEM_PAGE);
  279. //初始化工具栏
  280. that.renderToolbar();
  281. //让表格平铺
  282. that.fullSize();
  283. //如果多级表头,则填补表头高度
  284. if(options.cols.length > 1){
  285. //补全高度
  286. var th = that.layFixed.find(ELEM_HEADER).find('th');
  287. th.height(that.layHeader.height() - 1 - parseFloat(th.css('padding-top')) - parseFloat(th.css('padding-bottom')));
  288. }
  289. that.pullData(that.page); //请求数据
  290. that.events(); //事件
  291. };
  292. //根据列类型,定制化参数
  293. Class.prototype.initOpts = function(item){
  294. var that = this
  295. ,options = that.config
  296. ,initWidth = {
  297. checkbox: 48
  298. ,radio: 48
  299. ,space: 15
  300. ,numbers: 40
  301. };
  302. //让 type 参数兼容旧版本
  303. if(item.checkbox) item.type = "checkbox";
  304. if(item.space) item.type = "space";
  305. if(!item.type) item.type = "normal";
  306. if(item.type !== "normal"){
  307. item.unresize = true;
  308. item.width = item.width || initWidth[item.type];
  309. }
  310. };
  311. //初始化一些参数
  312. Class.prototype.setInit = function(type){
  313. var that = this
  314. ,options = that.config;
  315. options.clientWidth = options.width || function(){ //获取容器宽度
  316. //如果父元素宽度为0(一般为隐藏元素),则继续查找上层元素,直到找到真实宽度为止
  317. var getWidth = function(parent){
  318. var width, isNone;
  319. parent = parent || options.elem.parent()
  320. width = parent.width();
  321. try {
  322. isNone = parent.css('display') === 'none';
  323. } catch(e){}
  324. if(parent[0] && (!width || isNone)) return getWidth(parent.parent());
  325. return width;
  326. };
  327. return getWidth();
  328. }();
  329. if(type === 'width') return options.clientWidth;
  330. //初始化列参数
  331. layui.each(options.cols, function(i1, item1){
  332. layui.each(item1, function(i2, item2){
  333. //如果列参数为空,则移除
  334. if(!item2){
  335. item1.splice(i2, 1);
  336. return;
  337. }
  338. item2.key = i1 + '-' + i2;
  339. item2.hide = item2.hide || false;
  340. //设置列的父列索引
  341. //如果是组合列,则捕获对应的子列
  342. if(item2.colGroup || item2.colspan > 1){
  343. var childIndex = 0;
  344. layui.each(options.cols[i1 + 1], function(i22, item22){
  345. //如果子列已经被标注为{HAS_PARENT},或者子列累计 colspan 数等于父列定义的 colspan,则跳出当前子列循环
  346. if(item22.HAS_PARENT || (childIndex > 1 && childIndex == item2.colspan)) return;
  347. item22.HAS_PARENT = true;
  348. item22.parentKey = i1 + '-' + i2;
  349. childIndex = childIndex + parseInt(item22.colspan > 1 ? item22.colspan : 1);
  350. });
  351. item2.colGroup = true; //标注是组合列
  352. }
  353. //根据列类型,定制化参数
  354. that.initOpts(item2);
  355. });
  356. });
  357. };
  358. //初始工具栏
  359. Class.prototype.renderToolbar = function(){
  360. var that = this
  361. ,options = that.config
  362. //添加工具栏左侧模板
  363. var leftDefaultTemp = [
  364. '<div class="layui-inline" lay-event="add"><i class="layui-icon layui-icon-add-1"></i></div>'
  365. ,'<div class="layui-inline" lay-event="update"><i class="layui-icon layui-icon-edit"></i></div>'
  366. ,'<div class="layui-inline" lay-event="delete"><i class="layui-icon layui-icon-delete"></i></div>'
  367. ].join('')
  368. ,elemToolTemp = that.layTool.find('.layui-table-tool-temp');
  369. if(options.toolbar === 'default'){
  370. elemToolTemp.html(leftDefaultTemp);
  371. } else if(typeof options.toolbar === 'string'){
  372. var toolbarHtml = $(options.toolbar).html() || '';
  373. toolbarHtml && elemToolTemp.html(
  374. laytpl(toolbarHtml).render(options)
  375. );
  376. }
  377. //添加工具栏右侧面板
  378. var layout = {
  379. filter: {
  380. title: '筛选列'
  381. ,layEvent: 'LAYTABLE_COLS'
  382. ,icon: 'layui-icon-cols'
  383. }
  384. ,exports: {
  385. title: '导出'
  386. ,layEvent: 'LAYTABLE_EXPORT'
  387. ,icon: 'layui-icon-export'
  388. }
  389. ,print: {
  390. title: '打印'
  391. ,layEvent: 'LAYTABLE_PRINT'
  392. ,icon: 'layui-icon-print'
  393. }
  394. }, iconElem = [];
  395. if(typeof options.defaultToolbar === 'object'){
  396. layui.each(options.defaultToolbar, function(i, item){
  397. var thisItem = layout[item];
  398. if(thisItem){
  399. iconElem.push('<div class="layui-inline" title="'+ thisItem.title +'" lay-event="'+ thisItem.layEvent +'">'
  400. +'<i class="layui-icon '+ thisItem.icon +'"></i>'
  401. +'</div>');
  402. }
  403. });
  404. }
  405. that.layTool.find('.layui-table-tool-self').html(iconElem.join(''));
  406. }
  407. //同步表头父列的相关值
  408. Class.prototype.setParentCol = function(hide, parentKey){
  409. var that = this
  410. ,options = that.config
  411. ,parentTh = that.layHeader.find('th[data-key="'+ options.index +'-'+ parentKey +'"]') //获取父列元素
  412. ,parentColspan = parseInt(parentTh.attr('colspan')) || 0;
  413. if(parentTh[0]){
  414. var arrParentKey = parentKey.split('-')
  415. ,getThisCol = options.cols[arrParentKey[0]][arrParentKey[1]];
  416. hide ? parentColspan-- : parentColspan++;
  417. parentTh.attr('colspan', parentColspan);
  418. parentTh[parentColspan < 1 ? 'addClass' : 'removeClass'](HIDE);
  419. getThisCol.colspan = parentColspan; //同步 colspan 参数
  420. getThisCol.hide = parentColspan < 1; //同步 hide 参数
  421. //递归,继续往上查询是否有父列
  422. var nextParentKey = parentTh.data('parentkey');
  423. nextParentKey && that.setParentCol(hide, nextParentKey);
  424. }
  425. };
  426. //多级表头补丁
  427. Class.prototype.setColsPatch = function(){
  428. var that = this
  429. ,options = that.config
  430. //同步表头父列的相关值
  431. layui.each(options.cols, function(i1, item1){
  432. layui.each(item1, function(i2, item2){
  433. if(item2.hide){
  434. that.setParentCol(item2.hide, item2.parentKey);
  435. }
  436. });
  437. });
  438. };
  439. //动态分配列宽
  440. Class.prototype.setColsWidth = function(){
  441. var that = this
  442. ,options = that.config
  443. ,colNums = 0 //列个数
  444. ,autoColNums = 0 //自动列宽的列个数
  445. ,autoWidth = 0 //自动列分配的宽度
  446. ,countWidth = 0 //所有列总宽度和
  447. ,cntrWidth = that.setInit('width');
  448. //统计列个数
  449. that.eachCols(function(i, item){
  450. item.hide || colNums++;
  451. });
  452. //减去边框差和滚动条宽
  453. cntrWidth = cntrWidth - function(){
  454. return (options.skin === 'line' || options.skin === 'nob') ? 2 : colNums + 1;
  455. }() - that.getScrollWidth(that.layMain[0]) - 1;
  456. //计算自动分配的宽度
  457. var getAutoWidth = function(back){
  458. //遍历所有列
  459. layui.each(options.cols, function(i1, item1){
  460. layui.each(item1, function(i2, item2){
  461. var width = 0
  462. ,minWidth = item2.minWidth || options.cellMinWidth; //最小宽度
  463. if(!item2){
  464. item1.splice(i2, 1);
  465. return;
  466. }
  467. if(item2.colGroup || item2.hide) return;
  468. if(!back){
  469. width = item2.width || 0;
  470. if(/\d+%$/.test(width)){ //列宽为百分比
  471. width = Math.floor((parseFloat(width) / 100) * cntrWidth);
  472. width < minWidth && (width = minWidth);
  473. } else if(!width){ //列宽未填写
  474. item2.width = width = 0;
  475. autoColNums++;
  476. }
  477. } else if(autoWidth && autoWidth < minWidth){
  478. autoColNums--;
  479. width = minWidth;
  480. }
  481. if(item2.hide) width = 0;
  482. countWidth = countWidth + width;
  483. });
  484. });
  485. //如果未填充满,则将剩余宽度平分
  486. (cntrWidth > countWidth && autoColNums) && (
  487. autoWidth = (cntrWidth - countWidth) / autoColNums
  488. );
  489. }
  490. getAutoWidth();
  491. getAutoWidth(true); //重新检测分配的宽度是否低于最小列宽
  492. //记录自动列数
  493. that.autoColNums = autoColNums;
  494. //设置列宽
  495. that.eachCols(function(i3, item3){
  496. var minWidth = item3.minWidth || options.cellMinWidth;
  497. if(item3.colGroup || item3.hide) return;
  498. //给位分配宽的列平均分配宽
  499. if(item3.width === 0){
  500. that.getCssRule(options.index +'-'+ item3.key, function(item){
  501. item.style.width = Math.floor(autoWidth >= minWidth ? autoWidth : minWidth) + 'px';
  502. });
  503. }
  504. //给设定百分比的列分配列宽
  505. else if(/\d+%$/.test(item3.width)){
  506. that.getCssRule(options.index +'-'+ item3.key, function(item){
  507. item.style.width = Math.floor((parseFloat(item3.width) / 100) * cntrWidth) + 'px';
  508. });
  509. }
  510. });
  511. //填补 Math.floor 造成的数差
  512. var patchNums = that.layMain.width() - that.getScrollWidth(that.layMain[0])
  513. - that.layMain.children('table').outerWidth();
  514. if(that.autoColNums && patchNums >= -colNums && patchNums <= colNums){
  515. var getEndTh = function(th){
  516. var field;
  517. th = th || that.layHeader.eq(0).find('thead th:last-child')
  518. field = th.data('field');
  519. if(!field && th.prev()[0]){
  520. return getEndTh(th.prev())
  521. }
  522. return th
  523. }
  524. ,th = getEndTh()
  525. ,key = th.data('key');
  526. that.getCssRule(key, function(item){
  527. var width = item.style.width || th.outerWidth();
  528. item.style.width = (parseFloat(width) + patchNums) + 'px';
  529. //二次校验,如果仍然出现横向滚动条(通常是 1px 的误差导致)
  530. if(that.layMain.height() - that.layMain.prop('clientHeight') > 0){
  531. item.style.width = (parseFloat(item.style.width) - 1) + 'px';
  532. }
  533. });
  534. }
  535. that.loading(!0);
  536. };
  537. //重置表格尺寸/结构
  538. Class.prototype.resize = function(){
  539. var that = this;
  540. that.fullSize(); //让表格铺满
  541. that.setColsWidth(); //自适应列宽
  542. that.scrollPatch(); //滚动条补丁
  543. };
  544. //表格重载
  545. Class.prototype.reload = function(options){
  546. var that = this;
  547. options = options || {};
  548. delete that.haveInit;
  549. if(options.data && options.data.constructor === Array) delete that.config.data;
  550. that.config = $.extend(true, {}, that.config, options);
  551. that.render();
  552. };
  553. //异常提示
  554. Class.prototype.errorView = function(html){
  555. var that = this
  556. ,elemNone = that.layMain.find('.'+ NONE)
  557. ,layNone = $('<div class="'+ NONE +'">'+ (html || 'Error') +'</div>');
  558. if(elemNone[0]){
  559. that.layNone.remove();
  560. elemNone.remove();
  561. }
  562. that.layFixed.addClass(HIDE);
  563. that.layMain.find('tbody').html('');
  564. that.layMain.append(that.layNone = layNone);
  565. table.cache[that.key] = []; //格式化缓存数据
  566. };
  567. //页码
  568. Class.prototype.page = 1;
  569. //获得数据
  570. Class.prototype.pullData = function(curr){
  571. var that = this
  572. ,options = that.config
  573. ,request = options.request
  574. ,response = options.response
  575. ,sort = function(){
  576. if(typeof options.initSort === 'object'){
  577. that.sort(options.initSort.field, options.initSort.type);
  578. }
  579. };
  580. that.startTime = new Date().getTime(); //渲染开始时间
  581. if(options.url){ //Ajax请求
  582. var params = {};
  583. params[request.pageName] = curr;
  584. params[request.limitName] = options.limit;
  585. //参数
  586. var data = $.extend(params, options.where);
  587. if(options.contentType && options.contentType.indexOf("application/json") == 0){ //提交 json 格式
  588. data = JSON.stringify(data);
  589. }
  590. that.loading();
  591. $.ajax({
  592. type: options.method || 'get'
  593. ,url: options.url
  594. ,contentType: options.contentType
  595. ,data: data
  596. ,dataType: 'json'
  597. ,headers: options.headers || {}
  598. ,success: function(res){
  599. //如果有数据解析的回调,则获得其返回的数据
  600. if(typeof options.parseData === 'function'){
  601. res = options.parseData(res) || res;
  602. }
  603. //检查数据格式是否符合规范
  604. if(res[response.statusName] != response.statusCode){
  605. that.renderForm();
  606. that.errorView(
  607. res[response.msgName] ||
  608. ('返回的数据不符合规范,正确的成功状态码应为:"'+ response.statusName +'": '+ response.statusCode)
  609. );
  610. } else {
  611. that.renderData(res, curr, res[response.countName]), sort();
  612. options.time = (new Date().getTime() - that.startTime) + ' ms'; //耗时(接口请求+视图渲染)
  613. }
  614. that.setColsWidth();
  615. typeof options.done === 'function' && options.done(res, curr, res[response.countName]);
  616. }
  617. ,error: function(e, m){
  618. that.errorView('数据接口请求异常:'+ m);
  619. that.renderForm();
  620. that.setColsWidth();
  621. }
  622. });
  623. } else if(options.data && options.data.constructor === Array){ //已知数据
  624. var res = {}
  625. ,startLimit = curr*options.limit - options.limit
  626. res[response.dataName] = options.data.concat().splice(startLimit, options.limit);
  627. res[response.countName] = options.data.length;
  628. that.renderData(res, curr, res[response.countName]), sort();
  629. that.setColsWidth();
  630. typeof options.done === 'function' && options.done(res, curr, res[response.countName]);
  631. }
  632. };
  633. //遍历表头
  634. Class.prototype.eachCols = function(callback){
  635. var that = this;
  636. table.eachCols(null, callback, that.config.cols);
  637. return that;
  638. };
  639. //数据渲染
  640. Class.prototype.renderData = function(res, curr, count, sort){
  641. var that = this
  642. ,options = that.config
  643. ,data = res[options.response.dataName] || []
  644. ,trs = []
  645. ,trs_fixed = []
  646. ,trs_fixed_r = []
  647. //渲染视图
  648. ,render = function(){ //后续性能提升的重点
  649. var thisCheckedRowIndex;
  650. if(!sort && that.sortKey){
  651. return that.sort(that.sortKey.field, that.sortKey.sort, true);
  652. }
  653. layui.each(data, function(i1, item1){
  654. var tds = [], tds_fixed = [], tds_fixed_r = []
  655. ,numbers = i1 + options.limit*(curr - 1) + 1; //序号
  656. if(item1.length === 0) return;
  657. if(!sort){
  658. item1[table.config.indexName] = i1;
  659. }
  660. that.eachCols(function(i3, item3){
  661. var field = item3.field || i3
  662. ,key = options.index + '-' + item3.key
  663. ,content = item1[field];
  664. if(content === undefined || content === null) content = '';
  665. if(item3.colGroup) return;
  666. //td内容
  667. var td = ['<td data-field="'+ field +'" data-key="'+ key +'" '+ function(){ //追加各种属性
  668. var attr = [];
  669. if(item3.edit) attr.push('data-edit="'+ item3.edit +'"'); //是否允许单元格编辑
  670. if(item3.align) attr.push('align="'+ item3.align +'"'); //对齐方式
  671. if(item3.templet) attr.push('data-content="'+ content +'"'); //自定义模板
  672. if(item3.toolbar) attr.push('data-off="true"'); //行工具列关闭单元格事件
  673. if(item3.event) attr.push('lay-event="'+ item3.event +'"'); //自定义事件
  674. if(item3.style) attr.push('style="'+ item3.style +'"'); //自定义样式
  675. if(item3.minWidth) attr.push('data-minwidth="'+ item3.minWidth +'"'); //单元格最小宽度
  676. return attr.join(' ');
  677. }() +' class="'+ function(){ //追加样式
  678. var classNames = [];
  679. if(item3.hide) classNames.push(HIDE); //插入隐藏列样式
  680. if(!item3.field) classNames.push('layui-table-col-special'); //插入特殊列样式
  681. return classNames.join(' ');
  682. }() +'">'
  683. ,'<div class="layui-table-cell laytable-cell-'+ function(){ //返回对应的CSS类标识
  684. return item3.type === 'normal' ? key
  685. : (key + ' laytable-cell-' + item3.type);
  686. }() +'">' + function(){
  687. var tplData = $.extend(true, {
  688. LAY_INDEX: numbers
  689. }, item1)
  690. ,checkName = table.config.checkName;
  691. //渲染不同风格的列
  692. switch(item3.type){
  693. case 'checkbox':
  694. return '<input type="checkbox" name="layTableCheckbox" lay-skin="primary" '+ function(){
  695. //如果是全选
  696. if(item3[checkName]){
  697. item1[checkName] = item3[checkName];
  698. return item3[checkName] ? 'checked' : '';
  699. }
  700. return tplData[checkName] ? 'checked' : '';
  701. }() +'>';
  702. break;
  703. case 'radio':
  704. if(tplData[checkName]){
  705. thisCheckedRowIndex = i1;
  706. }
  707. return '<input type="radio" name="layTableRadio_'+ options.index +'" '
  708. + (tplData[checkName] ? 'checked' : '') +' lay-type="layTableRadio">';
  709. break;
  710. case 'numbers':
  711. return numbers;
  712. break;
  713. };
  714. //解析工具列模板
  715. if(item3.toolbar){
  716. return laytpl($(item3.toolbar).html()||'').render(tplData);
  717. }
  718. return parseTempData(item3, content, tplData);
  719. }()
  720. ,'</div></td>'].join('');
  721. tds.push(td);
  722. if(item3.fixed && item3.fixed !== 'right') tds_fixed.push(td);
  723. if(item3.fixed === 'right') tds_fixed_r.push(td);
  724. });
  725. trs.push('<tr data-index="'+ i1 +'">'+ tds.join('') + '</tr>');
  726. trs_fixed.push('<tr data-index="'+ i1 +'">'+ tds_fixed.join('') + '</tr>');
  727. trs_fixed_r.push('<tr data-index="'+ i1 +'">'+ tds_fixed_r.join('') + '</tr>');
  728. });
  729. that.layBody.scrollTop(0);
  730. that.layMain.find('.'+ NONE).remove();
  731. that.layMain.find('tbody').html(trs.join(''));
  732. that.layFixLeft.find('tbody').html(trs_fixed.join(''));
  733. that.layFixRight.find('tbody').html(trs_fixed_r.join(''));
  734. that.renderForm();
  735. typeof thisCheckedRowIndex === 'number' && that.setThisRowChecked(thisCheckedRowIndex);
  736. that.syncCheckAll();
  737. //滚动条补丁
  738. that.haveInit ? that.scrollPatch() : setTimeout(function(){
  739. that.scrollPatch();
  740. }, 50);
  741. that.haveInit = true;
  742. layer.close(that.tipsIndex);
  743. //同步表头父列的相关值
  744. options.HAS_SET_COLS_PATCH || that.setColsPatch();
  745. options.HAS_SET_COLS_PATCH = true;
  746. };
  747. table.cache[that.key] = data; //记录数据
  748. //显示隐藏分页栏
  749. that.layPage[(count == 0 || (data.length === 0 && curr == 1)) ? 'addClass' : 'removeClass'](HIDE);
  750. //排序
  751. if(sort){
  752. return render();
  753. }
  754. if(data.length === 0){
  755. that.renderForm();
  756. return that.errorView(options.text.none);
  757. } else {
  758. that.layFixed.removeClass(HIDE);
  759. }
  760. render(); //渲染数据
  761. that.renderTotal(data); //数据合计
  762. //同步分页状态
  763. if(options.page){
  764. options.page = $.extend({
  765. elem: 'layui-table-page' + options.index
  766. ,count: count
  767. ,limit: options.limit
  768. ,limits: options.limits || [10,20,30,40,50,60,70,80,90]
  769. ,groups: 3
  770. ,layout: ['prev', 'page', 'next', 'skip', 'count', 'limit']
  771. ,prev: '<i class="layui-icon">&#xe603;</i>'
  772. ,next: '<i class="layui-icon">&#xe602;</i>'
  773. ,jump: function(obj, first){
  774. if(!first){
  775. //分页本身并非需要做以下更新,下面参数的同步,主要是因为其它处理统一用到了它们
  776. //而并非用的是 options.page 中的参数(以确保分页未开启的情况仍能正常使用)
  777. that.page = obj.curr; //更新页码
  778. options.limit = obj.limit; //更新每页条数
  779. that.pullData(obj.curr);
  780. }
  781. }
  782. }, options.page);
  783. options.page.count = count; //更新总条数
  784. laypage.render(options.page);
  785. }
  786. };
  787. //数据合计行
  788. Class.prototype.renderTotal = function(data){
  789. var that = this
  790. ,options = that.config
  791. ,totalNums = {};
  792. if(!options.totalRow) return;
  793. layui.each(data, function(i1, item1){
  794. if(item1.length === 0) return;
  795. that.eachCols(function(i3, item3){
  796. var field = item3.field || i3
  797. ,content = item1[field];
  798. if(item3.totalRow){
  799. totalNums[field] = (totalNums[field] || 0) + (parseFloat(content) || 0);
  800. }
  801. });
  802. });
  803. var tds = [];
  804. that.eachCols(function(i3, item3){
  805. var field = item3.field || i3;
  806. //td内容
  807. var td = ['<td data-field="'+ field +'" data-key="'+ options.index + '-'+ item3.key +'" '+ function(){
  808. var attr = [];
  809. if(item3.align) attr.push('align="'+ item3.align +'"'); //对齐方式
  810. if(item3.style) attr.push('style="'+ item3.style +'"'); //自定义样式
  811. if(item3.minWidth) attr.push('data-minwidth="'+ item3.minWidth +'"'); //单元格最小宽度
  812. return attr.join(' ');
  813. }() +' class="'+ function(){ //追加样式
  814. var classNames = [];
  815. if(item3.hide) classNames.push(HIDE); //插入隐藏列样式
  816. if(!item3.field) classNames.push('layui-table-col-special'); //插入特殊列样式
  817. return classNames.join(' ');
  818. }() +'">'
  819. ,'<div class="layui-table-cell laytable-cell-'+ function(){ //返回对应的CSS类标识
  820. var str = (options.index + '-' + item3.key);
  821. return item3.type === 'normal' ? str
  822. : (str + ' laytable-cell-' + item3.type);
  823. }() +'">' + function(){
  824. var text = item3.totalRowText || '';
  825. return item3.totalRow ? (parseFloat(totalNums[field]).toFixed(2) || text) : text;
  826. }()
  827. ,'</div></td>'].join('');
  828. tds.push(td);
  829. });
  830. that.layTotal.find('tbody').html('<tr>' + tds.join('') + '</tr>');
  831. };
  832. //找到对应的列元素
  833. Class.prototype.getColElem = function(parent, key){
  834. var that = this
  835. ,options = that.config;
  836. return parent.eq(0).find('.laytable-cell-'+ (options.index + '-' + key) + ':eq(0)');
  837. };
  838. //渲染表单
  839. Class.prototype.renderForm = function(type){
  840. form.render(type, 'LAY-table-'+ this.index);
  841. };
  842. //标记当前行选中状态
  843. Class.prototype.setThisRowChecked = function(index){
  844. var that = this
  845. ,options = that.config
  846. ,ELEM_CLICK = 'layui-table-click'
  847. ,tr = that.layBody.find('tr[data-index="'+ index +'"]');
  848. tr.addClass(ELEM_CLICK).siblings('tr').removeClass(ELEM_CLICK);
  849. };
  850. //数据排序
  851. Class.prototype.sort = function(th, type, pull, formEvent){
  852. var that = this
  853. ,field
  854. ,res = {}
  855. ,options = that.config
  856. ,filter = options.elem.attr('lay-filter')
  857. ,data = table.cache[that.key], thisData;
  858. //字段匹配
  859. if(typeof th === 'string'){
  860. that.layHeader.find('th').each(function(i, item){
  861. var othis = $(this)
  862. ,_field = othis.data('field');
  863. if(_field === th){
  864. th = othis;
  865. field = _field;
  866. return false;
  867. }
  868. });
  869. }
  870. try {
  871. var field = field || th.data('field')
  872. ,key = th.data('key');
  873. //如果欲执行的排序已在状态中,则不执行渲染
  874. if(that.sortKey && !pull){
  875. if(field === that.sortKey.field && type === that.sortKey.sort){
  876. return;
  877. }
  878. }
  879. var elemSort = that.layHeader.find('th .laytable-cell-'+ key).find(ELEM_SORT);
  880. that.layHeader.find('th').find(ELEM_SORT).removeAttr('lay-sort'); //清除其它标题排序状态
  881. elemSort.attr('lay-sort', type || null);
  882. that.layFixed.find('th')
  883. } catch(e){
  884. return hint.error('Table modules: Did not match to field');
  885. }
  886. //记录排序索引和类型
  887. that.sortKey = {
  888. field: field
  889. ,sort: type
  890. };
  891. //默认为前端自动排序。如果否,则需自主排序(通常为服务端处理好排序)
  892. if(options.autoSort){
  893. if(type === 'asc'){ //升序
  894. thisData = layui.sort(data, field);
  895. } else if(type === 'desc'){ //降序
  896. thisData = layui.sort(data, field, true);
  897. } else { //清除排序
  898. thisData = layui.sort(data, table.config.indexName);
  899. delete that.sortKey;
  900. }
  901. }
  902. res[options.response.dataName] = thisData || data;
  903. that.renderData(res, that.page, that.count, true);
  904. if(formEvent){
  905. layui.event.call(th, MOD_NAME, 'sort('+ filter +')', {
  906. field: field
  907. ,type: type
  908. });
  909. }
  910. };
  911. //请求loading
  912. Class.prototype.loading = function(hide){
  913. var that = this
  914. ,options = that.config;
  915. if(options.loading){
  916. if(hide){
  917. that.layInit && that.layInit.remove();
  918. delete that.layInit;
  919. that.layBox.find(ELEM_INIT).remove();
  920. } else {
  921. that.layInit = $(['<div class="layui-table-init">'
  922. ,'<i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop"></i>'
  923. ,'</div>'].join(''));
  924. that.layBox.append(that.layInit);
  925. }
  926. }
  927. };
  928. //同步选中值状态
  929. Class.prototype.setCheckData = function(index, checked){
  930. var that = this
  931. ,options = that.config
  932. ,thisData = table.cache[that.key];
  933. if(!thisData[index]) return;
  934. if(thisData[index].constructor === Array) return;
  935. thisData[index][options.checkName] = checked;
  936. };
  937. //同步全选按钮状态
  938. Class.prototype.syncCheckAll = function(){
  939. var that = this
  940. ,options = that.config
  941. ,checkAllElem = that.layHeader.find('input[name="layTableCheckbox"]')
  942. ,syncColsCheck = function(checked){
  943. that.eachCols(function(i, item){
  944. if(item.type === 'checkbox'){
  945. item[options.checkName] = checked;
  946. }
  947. });
  948. return checked;
  949. };
  950. if(!checkAllElem[0]) return;
  951. if(table.checkStatus(that.key).isAll){
  952. if(!checkAllElem[0].checked){
  953. checkAllElem.prop('checked', true);
  954. that.renderForm('checkbox');
  955. }
  956. syncColsCheck(true);
  957. } else {
  958. if(checkAllElem[0].checked){
  959. checkAllElem.prop('checked', false);
  960. that.renderForm('checkbox');
  961. }
  962. syncColsCheck(false);
  963. }
  964. };
  965. //获取cssRule
  966. Class.prototype.getCssRule = function(key, callback){
  967. var that = this
  968. ,style = that.elem.find('style')[0]
  969. ,sheet = style.sheet || style.styleSheet || {}
  970. ,rules = sheet.cssRules || sheet.rules;
  971. layui.each(rules, function(i, item){
  972. if(item.selectorText === ('.laytable-cell-'+ key)){
  973. return callback(item), true;
  974. }
  975. });
  976. };
  977. //让表格铺满
  978. Class.prototype.fullSize = function(){
  979. var that = this
  980. ,options = that.config
  981. ,height = options.height, bodyHeight;
  982. if(that.fullHeightGap){
  983. height = _WIN.height() - that.fullHeightGap;
  984. if(height < 135) height = 135;
  985. that.elem.css('height', height);
  986. }
  987. if(!height) return;
  988. //减去列头区域的高度
  989. bodyHeight = parseFloat(height) - (that.layHeader.outerHeight() || 38); //此处的数字常量是为了防止容器处在隐藏区域无法获得高度的问题,暂时只对默认尺寸的表格做支持。
  990. //减去工具栏的高度
  991. if(options.toolbar){
  992. bodyHeight = bodyHeight - (that.layTool.outerHeight() || 50);
  993. }
  994. //减去统计朗的高度
  995. if(options.totalRow){
  996. bodyHeight = bodyHeight - (that.layTotal.outerHeight() || 40);
  997. }
  998. //减去分页栏的高度
  999. if(options.page){
  1000. bodyHeight = bodyHeight - (that.layPage.outerHeight() || 41);
  1001. }
  1002. that.layMain.css('height', bodyHeight - 2);
  1003. };
  1004. //获取滚动条宽度
  1005. Class.prototype.getScrollWidth = function(elem){
  1006. var width = 0;
  1007. if(elem){
  1008. width = elem.offsetWidth - elem.clientWidth;
  1009. } else {
  1010. elem = document.createElement('div');
  1011. elem.style.width = '100px';
  1012. elem.style.height = '100px';
  1013. elem.style.overflowY = 'scroll';
  1014. document.body.appendChild(elem);
  1015. width = elem.offsetWidth - elem.clientWidth;
  1016. document.body.removeChild(elem);
  1017. }
  1018. return width;
  1019. };
  1020. //滚动条补丁
  1021. Class.prototype.scrollPatch = function(){
  1022. var that = this
  1023. ,layMainTable = that.layMain.children('table')
  1024. ,scollWidth = that.layMain.width() - that.layMain.prop('clientWidth') //纵向滚动条宽度
  1025. ,scollHeight = that.layMain.height() - that.layMain.prop('clientHeight') //横向滚动条高度
  1026. ,getScrollWidth = that.getScrollWidth(that.layMain[0]) //获取主容器滚动条宽度,如果有的话
  1027. ,outWidth = layMainTable.outerWidth() - that.layMain.width() //表格内容器的超出宽度
  1028. //添加补丁
  1029. ,addPatch = function(elem){
  1030. if(scollWidth && scollHeight){
  1031. elem = elem.eq(0);
  1032. if(!elem.find('.layui-table-patch')[0]){
  1033. var patchElem = $('<th class="layui-table-patch"><div class="layui-table-cell"></div></th>'); //补丁元素
  1034. patchElem.find('div').css({
  1035. width: scollWidth
  1036. });
  1037. elem.find('tr').append(patchElem);
  1038. }
  1039. } else {
  1040. elem.find('.layui-table-patch').remove();
  1041. }
  1042. }
  1043. addPatch(that.layHeader);
  1044. addPatch(that.layTotal);
  1045. //固定列区域高度
  1046. var mainHeight = that.layMain.height()
  1047. ,fixHeight = mainHeight - scollHeight;
  1048. that.layFixed.find(ELEM_BODY).css('height', layMainTable.height() >= fixHeight ? fixHeight : 'auto');
  1049. //表格宽度小于容器宽度时,隐藏固定列
  1050. that.layFixRight[outWidth > 0 ? 'removeClass' : 'addClass'](HIDE);
  1051. //操作栏
  1052. that.layFixRight.css('right', scollWidth - 1);
  1053. };
  1054. //事件处理
  1055. Class.prototype.events = function(){
  1056. var that = this
  1057. ,options = that.config
  1058. ,_BODY = $('body')
  1059. ,dict = {}
  1060. ,th = that.layHeader.find('th')
  1061. ,resizing
  1062. ,ELEM_CELL = '.layui-table-cell'
  1063. ,filter = options.elem.attr('lay-filter');
  1064. //工具栏操作事件
  1065. that.layTool.on('click', '*[lay-event]', function(e){
  1066. var othis = $(this)
  1067. ,events = othis.attr('lay-event')
  1068. ,openPanel = function(sets){
  1069. var list = $(sets.list)
  1070. ,panel = $('<ul class="layui-table-tool-panel"></ul>');
  1071. panel.html(list);
  1072. //限制最大高度
  1073. if(options.height){
  1074. panel.css('max-height', options.height - (that.layTool.outerHeight() || 50));
  1075. }
  1076. //插入元素
  1077. othis.find('.layui-table-tool-panel')[0] || othis.append(panel);
  1078. that.renderForm();
  1079. panel.on('click', function(e){
  1080. layui.stope(e);
  1081. });
  1082. sets.done && sets.done(panel, list)
  1083. };
  1084. layui.stope(e);
  1085. _DOC.trigger('table.tool.panel.remove');
  1086. layer.close(that.tipsIndex);
  1087. switch(events){
  1088. case 'LAYTABLE_COLS': //筛选列
  1089. openPanel({
  1090. list: function(){
  1091. var lis = [];
  1092. that.eachCols(function(i, item){
  1093. if(item.field && item.type == 'normal'){
  1094. lis.push('<li><input type="checkbox" name="'+ item.field +'" data-key="'+ item.key +'" data-parentkey="'+ (item.parentKey||'') +'" lay-skin="primary" '+ (item.hide ? '' : 'checked') +' title="'+ (item.title || item.field) +'" lay-filter="LAY_TABLE_TOOL_COLS"></li>');
  1095. }
  1096. });
  1097. return lis.join('');
  1098. }()
  1099. ,done: function(){
  1100. form.on('checkbox(LAY_TABLE_TOOL_COLS)', function(obj){
  1101. var othis = $(obj.elem)
  1102. ,checked = this.checked
  1103. ,key = othis.data('key')
  1104. ,parentKey = othis.data('parentkey');
  1105. layui.each(options.cols, function(i1, item1){
  1106. layui.each(item1, function(i2, item2){
  1107. if(i1+ '-'+ i2 === key){
  1108. var hide = item2.hide;
  1109. //同步勾选列的 hide 值和隐藏样式
  1110. item2.hide = !checked;
  1111. that.elem.find('*[data-key="'+ options.index +'-'+ key +'"]')
  1112. [checked ? 'removeClass' : 'addClass'](HIDE);
  1113. //根据列的显示隐藏,同步多级表头的父级相关属性值
  1114. if(hide != item2.hide){
  1115. that.setParentCol(!checked, parentKey);
  1116. }
  1117. //重新适配尺寸
  1118. that.resize();
  1119. }
  1120. });
  1121. });
  1122. });
  1123. }
  1124. });
  1125. break;
  1126. case 'LAYTABLE_EXPORT': //导出
  1127. if(device.ie){
  1128. layer.tips('导出功能不支持 IE,请用 Chrome 等高级浏览器导出', this, {
  1129. tips: 3
  1130. })
  1131. } else {
  1132. openPanel({
  1133. list: function(){
  1134. return [
  1135. '<li data-type="csv">导出到 Csv 文件</li>'
  1136. ,'<li data-type="xls">导出到 Excel 文件</li>'
  1137. ].join('')
  1138. }()
  1139. ,done: function(panel, list){
  1140. list.on('click', function(){
  1141. var type = $(this).data('type')
  1142. table.exportFile(options.id, null, type);
  1143. });
  1144. }
  1145. });
  1146. }
  1147. break;
  1148. case 'LAYTABLE_PRINT': //打印
  1149. var printWin = window.open('打印窗口', '_blank')
  1150. ,style = ['<style>'
  1151. ,'body{font-size: 12px; color: #666;}'
  1152. ,'table{width: 100%; border-collapse: collapse; border-spacing: 0;}'
  1153. ,'th,td{line-height: 20px; padding: 9px 15px; border: 1px solid #ccc; text-align: left; font-size: 12px; color: #666;}'
  1154. ,'a{color: #666; text-decoration:none;}'
  1155. ,'*.layui-hide{display: none}'
  1156. ,'</style>'].join('')
  1157. ,html = $(that.layHeader.html()); //输出表头
  1158. html.append(that.layMain.find('table').html()); //输出表体
  1159. html.append(that.layTotal.find('table').html()) //输出合计行
  1160. html.find('th.layui-table-patch').remove(); //移除补丁
  1161. html.find('.layui-table-col-special').remove(); //移除特殊列
  1162. printWin.document.write(style + html.prop('outerHTML'));
  1163. printWin.document.close();
  1164. printWin.print();
  1165. printWin.close();
  1166. break;
  1167. }
  1168. layui.event.call(this, MOD_NAME, 'toolbar('+ filter +')', $.extend({
  1169. event: events
  1170. ,config: options
  1171. },{}));
  1172. });
  1173. //拖拽调整宽度
  1174. th.on('mousemove', function(e){
  1175. var othis = $(this)
  1176. ,oLeft = othis.offset().left
  1177. ,pLeft = e.clientX - oLeft;
  1178. if(othis.data('unresize') || dict.resizeStart){
  1179. return;
  1180. }
  1181. dict.allowResize = othis.width() - pLeft <= 10; //是否处于拖拽允许区域
  1182. _BODY.css('cursor', (dict.allowResize ? 'col-resize' : ''));
  1183. }).on('mouseleave', function(){
  1184. var othis = $(this);
  1185. if(dict.resizeStart) return;
  1186. _BODY.css('cursor', '');
  1187. }).on('mousedown', function(e){
  1188. var othis = $(this);
  1189. if(dict.allowResize){
  1190. var key = othis.data('key');
  1191. e.preventDefault();
  1192. dict.resizeStart = true; //开始拖拽
  1193. dict.offset = [e.clientX, e.clientY]; //记录初始坐标
  1194. that.getCssRule(key, function(item){
  1195. var width = item.style.width || othis.outerWidth();
  1196. dict.rule = item;
  1197. dict.ruleWidth = parseFloat(width);
  1198. dict.minWidth = othis.data('minwidth') || options.cellMinWidth;
  1199. });
  1200. }
  1201. });
  1202. //拖拽中
  1203. _DOC.on('mousemove', function(e){
  1204. if(dict.resizeStart){
  1205. e.preventDefault();
  1206. if(dict.rule){
  1207. var setWidth = dict.ruleWidth + e.clientX - dict.offset[0];
  1208. if(setWidth < dict.minWidth) setWidth = dict.minWidth;
  1209. dict.rule.style.width = setWidth + 'px';
  1210. layer.close(that.tipsIndex);
  1211. }
  1212. resizing = 1
  1213. }
  1214. }).on('mouseup', function(e){
  1215. if(dict.resizeStart){
  1216. dict = {};
  1217. _BODY.css('cursor', '');
  1218. that.scrollPatch();
  1219. }
  1220. if(resizing === 2){
  1221. resizing = null;
  1222. }
  1223. });
  1224. //排序
  1225. th.on('click', function(e){
  1226. var othis = $(this)
  1227. ,elemSort = othis.find(ELEM_SORT)
  1228. ,nowType = elemSort.attr('lay-sort')
  1229. ,type;
  1230. if(!elemSort[0] || resizing === 1) return resizing = 2;
  1231. if(nowType === 'asc'){
  1232. type = 'desc';
  1233. } else if(nowType === 'desc'){
  1234. type = null;
  1235. } else {
  1236. type = 'asc';
  1237. }
  1238. that.sort(othis, type, null, true);
  1239. }).find(ELEM_SORT+' .layui-edge ').on('click', function(e){
  1240. var othis = $(this)
  1241. ,index = othis.index()
  1242. ,field = othis.parents('th').eq(0).data('field')
  1243. layui.stope(e);
  1244. if(index === 0){
  1245. that.sort(field, 'asc', null, true);
  1246. } else {
  1247. that.sort(field, 'desc', null, true);
  1248. }
  1249. });
  1250. //数据行中的事件监听返回的公共对象成员
  1251. var commonMember = function(sets){
  1252. var othis = $(this)
  1253. ,index = othis.parents('tr').eq(0).data('index')
  1254. ,tr = that.layBody.find('tr[data-index="'+ index +'"]')
  1255. ,data = table.cache[that.key] || [];
  1256. data = data[index] || {};
  1257. return $.extend({
  1258. tr: tr //行元素
  1259. ,data: table.clearCacheKey(data) //当前行数据
  1260. ,del: function(){ //删除行数据
  1261. table.cache[that.key][index] = [];
  1262. tr.remove();
  1263. that.scrollPatch();
  1264. }
  1265. ,update: function(fields){ //修改行数据
  1266. fields = fields || {};
  1267. layui.each(fields, function(key, value){
  1268. if(key in data){
  1269. var templet, td = tr.children('td[data-field="'+ key +'"]');
  1270. data[key] = value;
  1271. that.eachCols(function(i, item2){
  1272. if(item2.field == key && item2.templet){
  1273. templet = item2.templet;
  1274. }
  1275. });
  1276. td.children(ELEM_CELL).html(function(){
  1277. return templet ? function(){
  1278. return typeof templet === 'function'
  1279. ? templet(data)
  1280. : laytpl($(templet).html() || value).render(data)
  1281. }() : value;
  1282. }());
  1283. td.data('content', value);
  1284. }
  1285. });
  1286. }
  1287. }, sets);
  1288. };
  1289. //复选框选择
  1290. that.elem.on('click', 'input[name="layTableCheckbox"]+', function(){ //替代元素的 click 事件
  1291. var checkbox = $(this).prev()
  1292. ,childs = that.layBody.find('input[name="layTableCheckbox"]')
  1293. ,index = checkbox.parents('tr').eq(0).data('index')
  1294. ,checked = checkbox[0].checked
  1295. ,isAll = checkbox.attr('lay-filter') === 'layTableAllChoose';
  1296. //全选
  1297. if(isAll){
  1298. childs.each(function(i, item){
  1299. item.checked = checked;
  1300. that.setCheckData(i, checked);
  1301. });
  1302. that.syncCheckAll();
  1303. that.renderForm('checkbox');
  1304. } else {
  1305. that.setCheckData(index, checked);
  1306. that.syncCheckAll();
  1307. }
  1308. layui.event.call(checkbox[0], MOD_NAME, 'checkbox('+ filter +')', commonMember.call(checkbox[0], {
  1309. checked: checked
  1310. ,type: isAll ? 'all' : 'one'
  1311. }));
  1312. });
  1313. //单选框选择
  1314. that.elem.on('click', 'input[lay-type="layTableRadio"]+', function(){
  1315. var radio = $(this).prev()
  1316. ,checked = radio[0].checked
  1317. ,thisData = table.cache[that.key]
  1318. ,index = radio.parents('tr').eq(0).data('index');
  1319. //重置数据单选属性
  1320. layui.each(thisData, function(i, item){
  1321. if(index === i){
  1322. item.LAY_CHECKED = true;
  1323. } else {
  1324. delete item.LAY_CHECKED;
  1325. }
  1326. });
  1327. that.setThisRowChecked(index);
  1328. layui.event.call(this, MOD_NAME, 'radio('+ filter +')', commonMember.call(this, {
  1329. checked: checked
  1330. }));
  1331. });
  1332. //行事件
  1333. that.layBody.on('mouseenter', 'tr', function(){ //鼠标移入行
  1334. var othis = $(this)
  1335. ,index = othis.index();
  1336. if(othis.data('off')) return; //不触发事件
  1337. that.layBody.find('tr:eq('+ index +')').addClass(ELEM_HOVER)
  1338. }).on('mouseleave', 'tr', function(){ //鼠标移出行
  1339. var othis = $(this)
  1340. ,index = othis.index();
  1341. if(othis.data('off')) return; //不触发事件
  1342. that.layBody.find('tr:eq('+ index +')').removeClass(ELEM_HOVER)
  1343. }).on('click', 'tr', function(){ //单击行
  1344. setRowEvent.call(this, 'row');
  1345. }).on('dblclick', 'tr', function(){ //双击行
  1346. setRowEvent.call(this, 'rowDouble');
  1347. });
  1348. //创建行单击、双击事件监听
  1349. var setRowEvent = function(eventType){
  1350. var othis = $(this);
  1351. if(othis.data('off')) return; //不触发事件
  1352. layui.event.call(this,
  1353. MOD_NAME, eventType + '('+ filter +')'
  1354. ,commonMember.call(othis.children('td')[0])
  1355. );
  1356. };
  1357. //单元格编辑
  1358. that.layBody.on('change', '.'+ELEM_EDIT, function(){
  1359. var othis = $(this)
  1360. ,value = this.value
  1361. ,field = othis.parent().data('field')
  1362. ,index = othis.parents('tr').eq(0).data('index')
  1363. ,data = table.cache[that.key][index];
  1364. data[field] = value; //更新缓存中的值
  1365. layui.event.call(this, MOD_NAME, 'edit('+ filter +')', commonMember.call(this, {
  1366. value: value
  1367. ,field: field
  1368. }));
  1369. }).on('blur', '.'+ELEM_EDIT, function(){
  1370. var templet
  1371. ,othis = $(this)
  1372. ,thisElem = this
  1373. ,field = othis.parent().data('field')
  1374. ,index = othis.parents('tr').eq(0).data('index')
  1375. ,data = table.cache[that.key][index];
  1376. that.eachCols(function(i, item){
  1377. if(item.field == field && item.templet){
  1378. templet = item.templet;
  1379. }
  1380. });
  1381. othis.siblings(ELEM_CELL).html(function(value){
  1382. return templet ? function(){
  1383. return typeof templet === 'function'
  1384. ? templet(data)
  1385. : laytpl($(templet).html() || thisElem.value).render(data)
  1386. }() : value;
  1387. }(thisElem.value));
  1388. othis.parent().data('content', thisElem.value);
  1389. othis.remove();
  1390. });
  1391. //单元格单击事件
  1392. that.layBody.on('click', 'td', function(e){
  1393. var othis = $(this)
  1394. ,field = othis.data('field')
  1395. ,editType = othis.data('edit')
  1396. ,elemCell = othis.children(ELEM_CELL);
  1397. if(othis.data('off')) return; //不触发事件
  1398. //显示编辑表单
  1399. if(editType){
  1400. var input = $('<input class="layui-input '+ ELEM_EDIT +'">');
  1401. input[0].value = othis.data('content') || elemCell.text();
  1402. othis.find('.'+ELEM_EDIT)[0] || othis.append(input);
  1403. input.focus();
  1404. layui.stope(e);
  1405. return;
  1406. }
  1407. }).on('mouseenter', 'td', function(){
  1408. gridExpand.call(this)
  1409. }).on('mouseleave', 'td', function(){
  1410. gridExpand.call(this, 'hide');
  1411. });
  1412. //单元格展开图标
  1413. var ELEM_GRID = 'layui-table-grid', ELEM_GRID_DOWN = 'layui-table-grid-down', ELEM_GRID_PANEL = 'layui-table-grid-panel'
  1414. ,gridExpand = function(hide){
  1415. var othis = $(this)
  1416. ,elemCell = othis.children(ELEM_CELL);
  1417. if(othis.data('off')) return; //不触发事件
  1418. if(hide){
  1419. othis.find('.layui-table-grid-down').remove();
  1420. } else if(elemCell.prop('scrollWidth') > elemCell.outerWidth()){
  1421. if(elemCell.find('.'+ ELEM_GRID_DOWN)[0]) return;
  1422. othis.append('<div class="'+ ELEM_GRID_DOWN +'"><i class="layui-icon layui-icon-down"></i></div>');
  1423. }
  1424. };
  1425. //单元格展开事件
  1426. that.layBody.on('click', '.'+ ELEM_GRID_DOWN, function(e){
  1427. var othis = $(this)
  1428. ,td = othis.parent()
  1429. ,elemCell = td.children(ELEM_CELL);
  1430. that.tipsIndex = layer.tips([
  1431. '<div class="layui-table-tips-main" style="margin-top: -'+ (elemCell.height() + 16) +'px;'+ function(){
  1432. if(options.size === 'sm'){
  1433. return 'padding: 4px 15px; font-size: 12px;';
  1434. }
  1435. if(options.size === 'lg'){
  1436. return 'padding: 14px 15px;';
  1437. }
  1438. return '';
  1439. }() +'">'
  1440. ,elemCell.html()
  1441. ,'</div>'
  1442. ,'<i class="layui-icon layui-table-tips-c layui-icon-close"></i>'
  1443. ].join(''), elemCell[0], {
  1444. tips: [3, '']
  1445. ,time: -1
  1446. ,anim: -1
  1447. ,maxWidth: (device.ios || device.android) ? 300 : that.elem.width()/2
  1448. ,isOutAnim: false
  1449. ,skin: 'layui-table-tips'
  1450. ,success: function(layero, index){
  1451. layero.find('.layui-table-tips-c').on('click', function(){
  1452. layer.close(index);
  1453. });
  1454. }
  1455. });
  1456. layui.stope(e);
  1457. });
  1458. //行工具条操作事件
  1459. that.layBody.on('click', '*[lay-event]', function(){
  1460. var othis = $(this)
  1461. ,index = othis.parents('tr').eq(0).data('index');
  1462. layui.event.call(this, MOD_NAME, 'tool('+ filter +')', commonMember.call(this, {
  1463. event: othis.attr('lay-event')
  1464. }));
  1465. that.setThisRowChecked(index);
  1466. });
  1467. //同步滚动条
  1468. that.layMain.on('scroll', function(){
  1469. var othis = $(this)
  1470. ,scrollLeft = othis.scrollLeft()
  1471. ,scrollTop = othis.scrollTop();
  1472. that.layHeader.scrollLeft(scrollLeft);
  1473. that.layTotal.scrollLeft(scrollLeft);
  1474. that.layFixed.find(ELEM_BODY).scrollTop(scrollTop);
  1475. layer.close(that.tipsIndex);
  1476. });
  1477. //全局点击
  1478. _DOC.on('click', function(){
  1479. _DOC.trigger('table.remove.tool.panel');
  1480. });
  1481. //工具面板移除事件
  1482. _DOC.on('table.remove.tool.panel', function(){
  1483. $('.layui-table-tool-panel').remove();
  1484. });
  1485. //自适应
  1486. _WIN.on('resize', function(){
  1487. that.resize();
  1488. });
  1489. };
  1490. //初始化
  1491. table.init = function(filter, settings){
  1492. settings = settings || {};
  1493. var that = this
  1494. ,elemTable = filter ? $('table[lay-filter="'+ filter +'"]') : $(ELEM + '[lay-data]')
  1495. ,errorTips = 'Table element property lay-data configuration item has a syntax error: ';
  1496. //遍历数据表格
  1497. elemTable.each(function(){
  1498. var othis = $(this), tableData = othis.attr('lay-data');
  1499. try{
  1500. tableData = new Function('return '+ tableData)();
  1501. } catch(e){
  1502. hint.error(errorTips + tableData)
  1503. }
  1504. var cols = [], options = $.extend({
  1505. elem: this
  1506. ,cols: []
  1507. ,data: []
  1508. ,skin: othis.attr('lay-skin') //风格
  1509. ,size: othis.attr('lay-size') //尺寸
  1510. ,even: typeof othis.attr('lay-even') === 'string' //偶数行背景
  1511. }, table.config, settings, tableData);
  1512. filter && othis.hide();
  1513. //获取表头数据
  1514. othis.find('thead>tr').each(function(i){
  1515. options.cols[i] = [];
  1516. $(this).children().each(function(ii){
  1517. var th = $(this), itemData = th.attr('lay-data');
  1518. try{
  1519. itemData = new Function('return '+ itemData)();
  1520. } catch(e){
  1521. return hint.error(errorTips + itemData)
  1522. }
  1523. var row = $.extend({
  1524. title: th.text()
  1525. ,colspan: th.attr('colspan') || 0 //列单元格
  1526. ,rowspan: th.attr('rowspan') || 0 //行单元格
  1527. }, itemData);
  1528. if(row.colspan < 2) cols.push(row);
  1529. options.cols[i].push(row);
  1530. });
  1531. });
  1532. //获取表体数据
  1533. othis.find('tbody>tr').each(function(i1){
  1534. var tr = $(this), row = {};
  1535. //如果定义了字段名
  1536. tr.children('td').each(function(i2, item2){
  1537. var td = $(this)
  1538. ,field = td.data('field');
  1539. if(field){
  1540. return row[field] = td.html();
  1541. }
  1542. });
  1543. //如果未定义字段名
  1544. layui.each(cols, function(i3, item3){
  1545. var td = tr.children('td').eq(i3);
  1546. row[item3.field] = td.html();
  1547. });
  1548. options.data[i1] = row;
  1549. });
  1550. table.render(options);
  1551. });
  1552. return that;
  1553. };
  1554. //记录所有实例
  1555. thisTable.that = {}; //记录所有实例对象
  1556. thisTable.config = {}; //记录所有实例配置项
  1557. //遍历表头
  1558. table.eachCols = function(id, callback, cols){
  1559. var config = thisTable.config[id] || {}
  1560. ,arrs = [], index = 0;
  1561. cols = $.extend(true, [], cols || config.cols);
  1562. //重新整理表头结构
  1563. layui.each(cols, function(i1, item1){
  1564. layui.each(item1, function(i2, item2){
  1565. //如果是组合列,则捕获对应的子列
  1566. if(item2.colGroup){
  1567. var childIndex = 0;
  1568. index++
  1569. item2.CHILD_COLS = [];
  1570. layui.each(cols[i1 + 1], function(i22, item22){
  1571. //如果子列已经被标注为{PARENT_COL_INDEX},或者子列累计 colspan 数等于父列定义的 colspan,则跳出当前子列循环
  1572. if(item22.PARENT_COL_INDEX || (childIndex > 1 && childIndex == item2.colspan)) return;
  1573. item22.PARENT_COL_INDEX = index;
  1574. item2.CHILD_COLS.push(item22);
  1575. childIndex = childIndex + parseInt(item22.colspan > 1 ? item22.colspan : 1);
  1576. });
  1577. }
  1578. if(item2.PARENT_COL_INDEX) return; //如果是子列,则不进行追加,因为已经存储在父列中
  1579. arrs.push(item2)
  1580. });
  1581. });
  1582. //重新遍历列,如果有子列,则进入递归
  1583. var eachArrs = function(obj){
  1584. layui.each(obj || arrs, function(i, item){
  1585. if(item.CHILD_COLS) return eachArrs(item.CHILD_COLS);
  1586. typeof callback === 'function' && callback(i, item);
  1587. });
  1588. };
  1589. eachArrs();
  1590. };
  1591. //表格选中状态
  1592. table.checkStatus = function(id){
  1593. var nums = 0
  1594. ,invalidNum = 0
  1595. ,arr = []
  1596. ,data = table.cache[id] || [];
  1597. //计算全选个数
  1598. layui.each(data, function(i, item){
  1599. if(item.constructor === Array){
  1600. invalidNum++; //无效数据,或已删除的
  1601. return;
  1602. }
  1603. if(item[table.config.checkName]){
  1604. nums++;
  1605. arr.push(table.clearCacheKey(item));
  1606. }
  1607. });
  1608. return {
  1609. data: arr //选中的数据
  1610. ,isAll: data.length ? (nums === (data.length - invalidNum)) : false //是否全选
  1611. };
  1612. };
  1613. //表格导出
  1614. table.exportFile = function(id, data, type){
  1615. data = data || table.clearCacheKey(table.cache[id]);
  1616. type = type || 'csv';
  1617. var config = thisTable.config[id] || {}
  1618. ,textType = ({
  1619. csv: 'text/csv'
  1620. ,xls: 'application/vnd.ms-excel'
  1621. })[type]
  1622. ,alink = document.createElement("a");
  1623. if(device.ie) return hint.error('IE_NOT_SUPPORT_EXPORTS');
  1624. alink.href = 'data:'+ textType +';charset=utf-8,\ufeff'+ encodeURIComponent(function(){
  1625. var dataTitle = [], dataMain = [];
  1626. layui.each(data, function(i1, item1){
  1627. var vals = [];
  1628. if(typeof id === 'object'){ //ID直接为表头数据
  1629. layui.each(id, function(i, item){
  1630. i1 == 0 && dataTitle.push(item || '');
  1631. });
  1632. layui.each(table.clearCacheKey(item1), function(i2, item2){
  1633. vals.push('"'+ (item2 || '') +'"');
  1634. });
  1635. } else {
  1636. table.eachCols(id, function(i3, item3){
  1637. if(item3.field && item3.type == 'normal' && !item3.hide){
  1638. i1 == 0 && dataTitle.push(item3.title || '');
  1639. vals.push('"'+ parseTempData(item3, item1[item3.field], item1, 'text') + '"');
  1640. }
  1641. });
  1642. }
  1643. dataMain.push(vals.join(','))
  1644. });
  1645. return dataTitle.join(',') + '\r\n' + dataMain.join('\r\n');
  1646. }());
  1647. alink.download = (config.title || 'table_'+ (config.index || '')) + '.' + type;
  1648. document.body.appendChild(alink);
  1649. alink.click();
  1650. document.body.removeChild(alink);
  1651. };
  1652. //重置表格尺寸结构
  1653. table.resize = function(id){
  1654. //如果指定表格唯一 id,则只执行该 id 对应的表格实例
  1655. if(id){
  1656. var config = getThisTableConfig(id); //获取当前实例配置项
  1657. if(!config) return;
  1658. thisTable.that[id].resize();
  1659. } else { //否则重置所有表格实例尺寸
  1660. layui.each(thisTable.that, function(){
  1661. this.resize();
  1662. });
  1663. }
  1664. };
  1665. //表格重载
  1666. table.reload = function(id, options){
  1667. var config = getThisTableConfig(id); //获取当前实例配置项
  1668. if(!config) return;
  1669. var that = thisTable.that[id];
  1670. that.reload(options);
  1671. return thisTable.call(that);
  1672. };
  1673. //核心入口
  1674. table.render = function(options){
  1675. var inst = new Class(options);
  1676. return thisTable.call(inst);
  1677. };
  1678. //清除临时Key
  1679. table.clearCacheKey = function(data){
  1680. data = $.extend({}, data);
  1681. delete data[table.config.checkName];
  1682. delete data[table.config.indexName];
  1683. return data;
  1684. };
  1685. //自动完成渲染
  1686. table.init();
  1687. exports(MOD_NAME, table);
  1688. });