table.js 65 KB

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