treeGrid.js 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. /**
  2. @Name:layui.treeGrid 树表格操作
  3. @Author:luonianqing
  4. @License:public,
  5. 首先感谢Layui的原码分项,本源码是在table.js的基面上改进的,如有BUG,请直接在FLY上留言,代码存在冗余,因为看得不够细,没时间,项目需要,Layui没有,我就写了
  6. 去除了排序,因为树型不支持表头排序
  7. 去除了全选,因为没有意义,不支持分页,因为也没有意义,也支除了记录条数限制,下级分支有多少条就展示多少条,只支持动态数据,不支持静态
  8. 增加了行选功能.即只要单击,其它的全不选,只有结点会被选中,
  9. 增加了treeDataCache,因为树是多级目录关系
  10. 不支持,树枝选择,下面全部被选中,也许将来会支持,现在满足项目需求即可,新增了注代码
  11. layui.use(['element', 'layer', 'form', 'upload', 'treeGrid'], function () {
  12. var treeGrid = layui.treeGrid; //很重要
  13. var treeTable = treeGrid.render({
  14. elem: '#treeTable'
  15. ,id:'treeTable' //这一行一定需要,和上一行不一样,这个是需要用来作数据修改和定位的
  16. ,levelField:'level' //表示树的级别字段,从1开始,从数据Json传回时必须是1,2,3,4,5...
  17. ,paramName:'parentId' //表示树向下展开时向后端传弟的参数名称,值就是作用的paramValue
  18. ,paramValue:'id' //表示树向下展开时向后端传弟的参数名称,一定是{url}+{paramName}={paramValue},现在就是{url}+parentId={id}
  19. ,scopeField:'baseCode' //表示树的图标作用在哪个字段前面,并表示父亲需向下传递的参数,一定是{url}+{parentParame}={baseCode},该字段一定要显示
  20. ,url: '/data.jsp'
  21. ,cols: [[
  22. {checkbox: true, fixed: true}
  23. ,{field:'baseCode', title: '编号', width:140}
  24. ,{field:'id', title: 'ID', width:80}
  25. ,{field:'keyName', width:140 ,title: '键名称'}
  26. ,{field:'keyShortName', width:140 ,title: '键简称'}
  27. ,{field:'keyValue', width:140 ,title: '键值'}
  28. ,{field:'typeValue', width:140 ,title: '类型值'}
  29. ,{field:'note', width:140 ,title: '备注'}
  30. ,{field:'sort', width:100 ,title: '排序',fixed: 'right'}
  31. ,{field:'isPublic', width:100 ,title: '状态',fixed: 'right'}
  32. ]]
  33. ,limit:300
  34. ,page: false
  35. ,height: 'full-200'
  36. });
  37. });
  38. */
  39. layui.define(['laytpl', 'layer', 'form'], function(exports){
  40. "use strict";
  41. var treeDataCache;
  42. var $ = layui.$
  43. ,laytpl = layui.laytpl
  44. ,layer = layui.layer
  45. ,form = layui.form
  46. ,hint = layui.hint()
  47. ,device = layui.device()
  48. //外部接口
  49. ,treeGrid = {
  50. config: {
  51. checkName: 'LAY_CHECKED' //是否选中状态的字段名
  52. ,indexName: 'LAY_TABLE_INDEX' //下标索引名
  53. } //全局配置项
  54. ,cache: {} //数据缓存
  55. ,index: layui.treeGrid ? (layui.treeGrid.index + 10000) : 0
  56. //设置全局项
  57. ,set: function(options){
  58. var that = this;
  59. that.config = $.extend({}, that.config, options);
  60. return that;
  61. }
  62. //事件监听
  63. ,on: function(events, callback){
  64. return layui.onevent.call(this, MOD_NAME, events, callback);
  65. }
  66. }
  67. //操作当前实例
  68. ,thisTable = function(){
  69. var that = this
  70. ,options = that.config
  71. ,id = options.id;
  72. id && (thisTable.config[id] = options);
  73. return {
  74. reload: function(options){
  75. that.reload.call(that, options);
  76. }
  77. ,config: options
  78. }
  79. }
  80. //字符常量
  81. ,MOD_NAME = 'treeGrid', ELEM = '.layui-table', THIS = 'layui-this', SHOW = 'layui-show', HIDE = 'layui-hide', DISABLED = 'layui-disabled', NONE = 'layui-none'
  82. ,ELEM_VIEW = 'layui-table-view', 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_TOOL = '.layui-table-tool', ELEM_PAGE = '.layui-table-page', ELEM_SORT = '.layui-table-sort', ELEM_EDIT = 'layui-table-edit', ELEM_HOVER = 'layui-table-hover'
  83. //thead区域模板
  84. ,TPL_HEADER = function(options){
  85. var rowCols = '{{#if(item2.colspan){}} colspan="{{item2.colspan}}"{{#} if(item2.rowspan){}} rowspan="{{item2.rowspan}}"{{#}}}';
  86. options = options || {};
  87. return ['<table cellspacing="0" cellpadding="0" border="0" class="layui-table" '
  88. , '{{# if(d.data.skin){ }}lay-skin="{{d.data.skin}}"{{# } }} {{# if(d.data.size){ }}lay-size="{{d.data.size}}"{{# } }} lay-even>'//{{# if(d.data.even){ }}lay-even{{# } }}
  89. ,'<thead>'
  90. ,'{{# layui.each(d.data.cols, function(i1, item1){ }}'
  91. ,'<tr>'
  92. ,'{{# layui.each(item1, function(i2, item2){ }}'
  93. ,'{{# if(item2.fixed && item2.fixed !== "right"){ left = true; } }}'
  94. ,'{{# if(item2.fixed === "right"){ right = true; } }}'
  95. ,function(){
  96. if(options.fixed && options.fixed !== 'right'){
  97. return '{{# if(item2.fixed && item2.fixed !== "right"){ }}';
  98. }
  99. if(options.fixed === 'right'){
  100. return '{{# if(item2.fixed === "right"){ }}';
  101. }
  102. return '';
  103. }()
  104. ,'<th data-field="{{ item2.field||i2 }}" {{# if(item2.minWidth){ }}data-minwidth="{{item2.minWidth}}"{{# } }} '+ rowCols +' {{# if(item2.unresize){ }}data-unresize="true"{{# } }}>'
  105. ,'<div class="layui-table-cell laytable-cell-'
  106. ,'{{# if(item2.colspan > 1){ }}'
  107. ,'group'
  108. ,'{{# } else { }}'
  109. ,'{{d.index}}-{{item2.field || i2}}'
  110. ,'{{# if(item2.type !== "normal"){ }}'
  111. ,' laytable-cell-{{ item2.type }}'
  112. ,'{{# } }}'
  113. ,'{{# } }}'
  114. ,'" {{#if(item2.align){}}align="{{item2.align}}"{{#}}}>'
  115. ,'{{# if(item2.type === "checkbox"){ }}' //复选框
  116. ,''
  117. ,'{{# } else { }}'
  118. ,'<span>{{item2.title||""}}</span>'
  119. ,'{{# } }}'
  120. ,'</div>'
  121. ,'</th>'
  122. ,(options.fixed ? '{{# }; }}' : '')
  123. ,'{{# }); }}'
  124. ,'</tr>'
  125. ,'{{# }); }}'
  126. ,'</thead>'
  127. ,'</table>'].join('');
  128. }
  129. //tbody区域模板
  130. ,TPL_BODY = ['<table cellspacing="0" cellpadding="0" border="0" class="layui-table" '
  131. ,'{{# if(d.data.skin){ }}lay-skin="{{d.data.skin}}"{{# } }} {{# if(d.data.size){ }}lay-size="{{d.data.size}}"{{# } }} {{# if(d.data.even){ }}lay-even{{# } }}>'
  132. ,'<tbody></tbody>'
  133. ,'</table>'].join('')
  134. //主模板
  135. ,TPL_MAIN = ['<div class="layui-form layui-border-box {{d.VIEW_CLASS}}" lay-filter="LAY-table-{{d.index}}" style="{{# if(d.data.width){ }}width:{{d.data.width}}px;{{# } }} {{# if(d.data.height){ }}height:{{d.data.height}}px;{{# } }}">'
  136. ,'{{# if(d.data.toolbar){ }}'
  137. ,'<div class="layui-table-tool"></div>'
  138. ,'{{# } }}'
  139. ,'<div class="layui-table-box">'
  140. ,'{{# var left, right; }}'
  141. ,'<div class="layui-table-header">'
  142. ,TPL_HEADER()
  143. ,'</div>'
  144. ,'<div class="layui-table-body layui-table-main">'
  145. ,TPL_BODY
  146. ,'</div>'
  147. ,'{{# if(left){ }}'
  148. ,'<div class="layui-table-fixed layui-table-fixed-l">'
  149. ,'<div class="layui-table-header">'
  150. ,TPL_HEADER({fixed: true})
  151. ,'</div>'
  152. ,'<div class="layui-table-body">'
  153. ,TPL_BODY
  154. ,'</div>'
  155. ,'</div>'
  156. ,'{{# }; }}'
  157. ,'{{# if(right){ }}'
  158. ,'<div class="layui-table-fixed layui-table-fixed-r">'
  159. ,'<div class="layui-table-header">'
  160. ,TPL_HEADER({fixed: 'right'})
  161. ,'<div class="layui-table-mend"></div>'
  162. ,'</div>'
  163. ,'<div class="layui-table-body">'
  164. ,TPL_BODY
  165. ,'</div>'
  166. ,'</div>'
  167. ,'{{# }; }}'
  168. ,'</div>'
  169. ,'<style>'
  170. ,'{{# layui.each(d.data.cols, function(i1, item1){'
  171. ,'layui.each(item1, function(i2, item2){ }}'
  172. ,'.laytable-cell-{{d.index}}-{{item2.field||i2}}{ '
  173. ,'{{# if(item2.width){ }}'
  174. ,'width: {{item2.width}}px;'
  175. ,'{{# } }}'
  176. ,' }'
  177. ,'{{# });'
  178. ,'}); }}'
  179. ,'</style>'
  180. ,'</div>'].join('')
  181. ,_WIN = $(window)
  182. ,_DOC = $(document)
  183. //构造器
  184. ,Class = function(options){
  185. var that = this;
  186. that.index = ++treeGrid.index;
  187. that.config = $.extend({}, that.config, treeGrid.config, options);
  188. that.render();
  189. };
  190. //默认配置
  191. Class.prototype.config = {
  192. limit: 10 //每页显示的数量
  193. ,loading: true //请求数据时,是否显示loading
  194. ,cellMinWidth: 60 //所有单元格默认最小宽度
  195. };
  196. //表格渲染
  197. Class.prototype.render = function(){
  198. var that = this
  199. ,options = that.config;
  200. options.elem = $(options.elem);
  201. options.where = options.where || {};
  202. options.id = options.id || options.elem.attr('id');
  203. //请求参数的自定义格式
  204. options.request = $.extend({}, options.request)
  205. //响应数据的自定义格式
  206. options.response = $.extend({
  207. statusName: 'code'
  208. ,statusCode: 0
  209. ,msgName: 'msg'
  210. ,dataName: 'data'
  211. ,countName: 'count'
  212. }, options.response);
  213. if(!options.elem[0]) return that;
  214. that.setArea(); //动态分配列宽高
  215. //开始插入替代元素
  216. var othis = options.elem
  217. ,hasRender = othis.next('.' + ELEM_VIEW)
  218. //主容器
  219. ,reElem = that.elem = $(laytpl(TPL_MAIN).render({
  220. VIEW_CLASS: ELEM_VIEW
  221. ,data: options
  222. ,index: that.index //索引
  223. }));
  224. options.index = that.index;
  225. //生成替代元素
  226. hasRender[0] && hasRender.remove(); //如果已经渲染,则Rerender
  227. if(options.refresh)
  228. {
  229. }
  230. else
  231. {
  232. othis.after(reElem);
  233. }
  234. // console.log(othis);
  235. //各级容器
  236. that.layHeader = reElem.find(ELEM_HEADER);
  237. that.layMain = reElem.find(ELEM_MAIN);
  238. that.layBody = reElem.find(ELEM_BODY);
  239. that.layFixed = reElem.find(ELEM_FIXED);
  240. that.layFixLeft = reElem.find(ELEM_FIXL);
  241. that.layFixRight = reElem.find(ELEM_FIXR);
  242. that.layTool = reElem.find(ELEM_TOOL);
  243. that.layPage = reElem.find(ELEM_PAGE);
  244. that.layTool.html(
  245. laytpl($(options.toolbar).html()||'').render(options)
  246. );
  247. if(options.height) that.fullSize(); //设置body区域高度
  248. //如果多级表头,则填补表头高度
  249. if(options.cols.length > 1){
  250. var th = that.layFixed.find(ELEM_HEADER).find('th');
  251. th.height(that.layHeader.height() - 1 - parseFloat(th.css('padding-top')) - parseFloat(th.css('padding-bottom')));
  252. }
  253. //请求数据
  254. that.pullData();
  255. that.events();
  256. };
  257. //根据列类型,定制化参数
  258. Class.prototype.initOpts = function(item){
  259. var that = this,
  260. options = that.config
  261. ,initWidth = {
  262. checkbox: 48
  263. ,space: 15
  264. };
  265. //让 type 参数兼容旧版本
  266. if(item.checkbox) item.type = "checkbox";
  267. if(item.space) item.type = "space";
  268. if(!item.type) item.type = "normal";
  269. if(item.type !== "normal"){
  270. item.unresize = true;
  271. item.width = item.width || initWidth[item.type];
  272. }
  273. };
  274. //动态分配列宽高
  275. Class.prototype.setArea = function(){
  276. var that = this,
  277. options = that.config
  278. ,colNums = 0 //列个数
  279. ,autoColNums = 0 //自动列宽的列个数
  280. ,autoWidth = 0 //自动列分配的宽度
  281. ,countWidth = 0 //所有列总宽度和
  282. ,cntrWidth = options.width || function(){ //获取容器宽度
  283. //如果父元素宽度为0(一般为隐藏元素),则继续查找上层元素,直到找到真实宽度为止
  284. var getWidth = function(parent){
  285. var width, isNone;
  286. parent = parent || options.elem.parent()
  287. width = parent.width();
  288. try {
  289. isNone = parent.css('display') === 'none';
  290. } catch(e){}
  291. if(parent[0] && (!width || isNone)) return getWidth(parent.parent());
  292. return width;
  293. };
  294. return getWidth();
  295. }();
  296. //统计列个数
  297. that.eachCols(function(){
  298. colNums++;
  299. });
  300. //减去边框差
  301. cntrWidth = cntrWidth - function(){
  302. return (options.skin === 'line' || options.skin === 'nob') ? 2 : colNums + 1;
  303. }();
  304. //遍历所有列
  305. layui.each(options.cols, function(i1, item1){
  306. layui.each(item1, function(i2, item2){
  307. var width;
  308. if(!item2){
  309. item1.splice(i2, 1);
  310. return;
  311. }
  312. that.initOpts(item2);
  313. width = item2.width || 0;
  314. if(item2.colspan > 1) return;
  315. if(/\d+%$/.test(width)){
  316. item2.width = width = Math.floor((parseFloat(width) / 100) * cntrWidth);
  317. } else if(!width){ //列宽未填写
  318. item2.width = width = 0;
  319. autoColNums++;
  320. }
  321. countWidth = countWidth + width;
  322. });
  323. });
  324. that.autoColNums = autoColNums; //记录自动列数
  325. //如果未填充满,则将剩余宽度平分。否则,给未设定宽度的列赋值一个默认宽
  326. (cntrWidth > countWidth && autoColNums) && (
  327. autoWidth = (cntrWidth - countWidth) / autoColNums
  328. );
  329. layui.each(options.cols, function(i1, item1){
  330. layui.each(item1, function(i2, item2){
  331. var minWidth = item2.minWidth || options.cellMinWidth;
  332. if(item2.colspan > 1) return;
  333. if(item2.width === 0){
  334. item2.width = Math.floor(autoWidth >= minWidth ? autoWidth : minWidth); //不能低于设定的最小宽度
  335. }
  336. });
  337. });
  338. //高度铺满:full-差距值
  339. if(options.height && /^full-\d+$/.test(options.height)){
  340. that.fullHeightGap = options.height.split('-')[1];
  341. options.height = _WIN.height() - that.fullHeightGap;
  342. }
  343. };
  344. //表格重载
  345. Class.prototype.reload = function(options){
  346. var that = this;
  347. if(that.config.data && that.config.data.constructor === Array) delete that.config.data;
  348. that.config = $.extend({}, that.config, options);
  349. that.render();
  350. };
  351. //获得数据
  352. Class.prototype.pullData = function(curr, loadIndex){
  353. var that = this
  354. ,options = that.config
  355. ,request = options.request
  356. ,response = options.response;
  357. that.startTime = new Date().getTime(); //渲染开始时间
  358. var parentId=0;
  359. if(options.url){ //Ajax请求
  360. if(options.refresh)
  361. {
  362. var refreshId=options.refresh.parentNodeId;
  363. var jsonArray =$.parseJSON("{\""+options.paramName+"\":"+refreshId+"}");
  364. options.refresh=jsonArray;
  365. parentId=refreshId;
  366. }
  367. var params = {};
  368. $.ajax({
  369. type: options.method || 'get'
  370. ,url: options.url
  371. ,data: $.extend(params,options.refresh, options.where)
  372. ,dataType: 'json'
  373. ,success: function(res){
  374. if(res[response.statusName] != response.statusCode){
  375. that.renderForm();
  376. return that.layMain.html('<div class="'+ NONE +'">'+ (res[response.msgName] || '返回的数据状态异常') +'</div>');
  377. }
  378. //console.log(options);
  379. that.renderData(parentId,res);
  380. options.time = (new Date().getTime() - that.startTime) + ' ms'; //耗时(接口请求+视图渲染)
  381. loadIndex && layer.close(loadIndex);
  382. typeof options.done === 'function' && options.done(res, curr, res[response.countName]);
  383. }
  384. ,error: function(e, m){
  385. that.layMain.html('<div class="'+ NONE +'">数据接口请求异常</div>');
  386. that.renderForm();
  387. loadIndex && layer.close(loadIndex);
  388. }
  389. });
  390. } else if(options.data && options.data.constructor === Array){ //已知数据
  391. var res = {}
  392. ,startLimit = curr*options.limit - options.limit
  393. res[response.dataName] = options.data.concat().splice(startLimit, options.limit);
  394. res[response.countName] = options.data.length;
  395. that.renderData(parentId,res);
  396. typeof options.done === 'function' && options.done(res, curr, res[response.countName]);
  397. }
  398. };
  399. //遍历表头
  400. Class.prototype.eachCols = function(callback){
  401. var cols = $.extend(true, [], this.config.cols)
  402. ,arrs = [], index = 0;
  403. //重新整理表头结构
  404. layui.each(cols, function(i1, item1){
  405. layui.each(item1, function(i2, item2){
  406. //如果是组合列,则捕获对应的子列
  407. if(item2.colspan > 1){
  408. var childIndex = 0;
  409. index++
  410. item2.CHILD_COLS = [];
  411. layui.each(cols[i1 + 1], function(i22, item22){
  412. if(item22.PARENT_COL || childIndex == item2.colspan) return;
  413. item22.PARENT_COL = index;
  414. item2.CHILD_COLS.push(item22);
  415. childIndex = childIndex + (item22.colspan > 1 ? item22.colspan : 1);
  416. });
  417. }
  418. if(item2.PARENT_COL) return; //如果是子列,则不进行追加,因为已经存储在父列中
  419. arrs.push(item2)
  420. });
  421. });
  422. //重新遍历列,如果有子列,则进入递归
  423. var eachArrs = function(obj){
  424. layui.each(obj || arrs, function(i, item){
  425. if(item.CHILD_COLS) return eachArrs(item.CHILD_COLS);
  426. callback(i, item);
  427. });
  428. };
  429. eachArrs();
  430. };
  431. //展开或者收起树
  432. Class.prototype.exportTree=function(parentId,isExport)
  433. {
  434. var that = this;
  435. // var dbClickI=that.layMain.find('tr[data-id="'+ parentId +'"]').find('.layui-tree-head');
  436. var dbClickI = that.layMain.find('tr[data-id="' + parentId + '"]').find('.treeHead');
  437. var dbHtml = dbClickI.html();
  438. if (dbHtml == undefined || (dbHtml.indexOf("folder") < 0)) {
  439. return;
  440. }
  441. if(isExport)
  442. {
  443. dbClickI.attr('isExport',"true");
  444. //dbClickI.html('&#xe625;');
  445. dbClickI.html("<i class='fa fa-folder-open'></i>");
  446. }
  447. else
  448. {
  449. dbClickI.attr('isExport', "false");
  450. //dbClickI.html('&#xe623;');
  451. dbClickI.html("<i class='fa fa-folder'></i>");
  452. that.layMain.find('tr[data-parent-id="'+ parentId +'"]').each(
  453. function()
  454. {
  455. var tempParentId=$(this).attr("data-id");
  456. $(this).hide();
  457. that.exportTree(tempParentId,false);
  458. });
  459. that.layFixLeft.find('tr[data-parent-id="'+ parentId +'"]').each(
  460. function()
  461. {
  462. var tempParentId=$(this).attr("data-id");
  463. $(this).hide();
  464. that.exportTree(tempParentId,false);
  465. });
  466. that.layFixRight.find('tr[data-parent-id="'+ parentId +'"]').each(
  467. function()
  468. {
  469. var tempParentId=$(this).attr("data-id");
  470. $(this).hide();
  471. that.exportTree(tempParentId,false);
  472. });
  473. }
  474. };
  475. //数据渲染
  476. Class.prototype.renderData = function(parentId,res){
  477. if(treeDataCache==null)
  478. {
  479. treeDataCache=new Object();
  480. treeDataCache[parentId]=res[this.config.response.dataName];
  481. }
  482. else
  483. {
  484. treeDataCache[parentId]=res[this.config.response.dataName];
  485. }
  486. var that = this
  487. ,options = that.config
  488. ,data = treeDataCache|| [] //
  489. ,trs = []
  490. ,trs_fixed = []
  491. ,trs_fixed_r = []
  492. //渲染视图
  493. ,render = function(){ //后续性能提升的重点
  494. //treeHead内容
  495. var levelField=options.levelField
  496. ,paramName=options.paramName
  497. ,paramValue=options.paramValue
  498. ,scopeField=options.scopeField;
  499. //处理展开还是隐藏
  500. var childIsExport=false;
  501. //var dbClickI=that.layMain.find('tr[data-id="'+ parentId +'"]').find('.layui-tree-head');
  502. var dbClickI = that.layMain.find('tr[data-id="' + parentId + '"]').find('.treeHead');
  503. if(dbClickI.length>0)
  504. {
  505. if(dbClickI.attr('isExport')=='true')//原来是展开,现在要收起
  506. {
  507. childIsExport=false;
  508. }
  509. else
  510. {
  511. childIsExport=true;
  512. }
  513. }
  514. else
  515. {
  516. childIsExport=true;
  517. }
  518. layui.each(data[parentId], function(i1, item1){
  519. var tds = [], tds_fixed = [], tds_fixed_r = [];
  520. if(item1.length === 0) return;
  521. that.eachCols(function(i3, item3){
  522. var field = item3.field || i3, content = item1[field]
  523. ,cell = that.getColElem(that.layHeader, field);
  524. if(content === undefined || content === null) content = '';
  525. if(item3.colspan > 1) return;
  526. var treeHeadImg="";
  527. if(field===scopeField)
  528. {
  529. var level=item1[options.levelField];
  530. var temp="<i>"
  531. for(var i=1;i<level;i++) {
  532. temp = temp + "&nbsp;&nbsp;";
  533. }
  534. temp=temp+"</i>";
  535. //treeHeadImg = temp + "<i class='layui-icon layui-tree-head' id=treeHead" + item1[paramValue] + " isExport='false'>&#xe623;</i>";
  536. treeHeadImg = temp + "<i class='treeHead' id=treeHead" + item1[paramValue] + " isExport='false'>" + (item1["HasChild"] ? "<i class='fa fa-folder'></i>" : "<i class='fa fa-folder-o'></i>") + " </i>";
  537. }
  538. //td内容
  539. var td = ['<td data-field="'+ field +'" '+ function(){
  540. var attr = [];
  541. if(item3.edit) attr.push('data-edit="'+ item3.edit +'"'); //是否允许单元格编辑
  542. if(item3.align) attr.push('align="'+ item3.align +'"'); //对齐方式
  543. if(item3.templet) attr.push('data-content="'+ content +'"'); //自定义模板
  544. if(item3.toolbar) attr.push('data-off="true"'); //自定义模板
  545. if(item3.event) attr.push('lay-event="'+ item3.event +'"'); //自定义事件
  546. if(item3.style) attr.push('style="'+ item3.style +'"'); //自定义样式
  547. if(item3.minWidth) attr.push('data-minwidth="'+ item3.minWidth +'"'); //单元格最小宽度
  548. return attr.join(' ');
  549. }() +'>'
  550. ,'<div class="layui-table-cell laytable-cell-'+ function(){ //返回对应的CSS类标识
  551. var str = (options.index + '-' + field);
  552. return item3.type === 'normal' ? str
  553. : (str + ' laytable-cell-' + item3.type);
  554. }() +'">' + function(){
  555. var tplData = $.extend(true,item1);
  556. //渲染复选框列视图
  557. if(item3.type === 'checkbox'){
  558. return '<input type="checkbox" name="layTableCheckbox" id="tableCheckBox'+ parentId +'_'+i1+'" lay-skin="primary" '+ function(){
  559. var checkName = treeGrid.config.checkName;
  560. return tplData[checkName] ? 'checked' : '';
  561. }() +'>';
  562. }
  563. //解析工具列模板
  564. if(item3.toolbar){
  565. return laytpl($(item3.toolbar).html()||'').render(tplData);
  566. }
  567. return item3.templet ? treeHeadImg+(laytpl($(item3.templet).html() || String(content)).render(tplData)) : treeHeadImg+content+"";
  568. }()
  569. ,'</div></td>'].join('');
  570. tds.push(td);
  571. if(item3.fixed && item3.fixed !== 'right') tds_fixed.push(td);
  572. if(item3.fixed === 'right') tds_fixed_r.push(td);
  573. });
  574. var tr = '<tr data-index="' + parentId + '_' + i1 + '" data-id="' + item1[paramValue] + '" data-parent-id="' + parentId + '"';
  575. var level =Number(item1[levelField]);
  576. //console.log(level);
  577. //if (level>1) {
  578. // if (level % 2 === 0) {
  579. // tr += ' style="background-image: linear-gradient(rgba(250,250,250,0.1),rgba(250,250,250,,0.1))"';
  580. // } else {
  581. // tr += ' style="background-image: linear-gradient(rgba(0,0,0,0.1),rgba(0,0,0,0.1))"';
  582. // }
  583. //}
  584. //if (item1[levelField] == 2) {
  585. // tr += ' style="background-image: linear-gradient(rgba(250,250,250,0.1),rgba(250,250,250,,0.1))"';
  586. //} else if (item1[levelField] == 3) {
  587. // tr += ' style="background-image: linear-gradient(rgba(0,0,0,0.1),rgba(0,0,0,0.1))"';
  588. //} else if (item1[levelField] == 4) {
  589. // tr += ' style="background-image: linear-gradient(rgba(0,0,0,0.3),rgba(0,0,0,0.3))"';
  590. //}
  591. tr += '>';
  592. ;
  593. trs.push(tr + tds.join('') + '</tr>');
  594. trs_fixed.push(tr + tds_fixed.join('') + '</tr>');
  595. trs_fixed_r.push(tr + tds_fixed_r.join('') + '</tr>');
  596. });
  597. if(parentId==0)
  598. {
  599. that.layBody.scrollTop(0);
  600. that.layMain.find('.'+ NONE).remove();
  601. that.layMain.find('tbody').html(trs.join(''));
  602. that.layFixLeft.find('tbody').html(trs_fixed.join(''));
  603. that.layFixRight.find('tbody').html(trs_fixed_r.join(''));
  604. }
  605. else
  606. {
  607. that.layMain.find('tr[data-parent-id="'+ parentId +'"]').remove();
  608. that.layFixLeft.find('tr[data-parent-id="'+ parentId +'"]').remove();
  609. that.layFixRight.find('tr[data-parent-id="'+ parentId +'"]').remove();
  610. that.layMain.find('tr[data-id="'+ parentId +'"]').after(trs.join(''));
  611. that.layFixLeft.find('tr[data-id="'+ parentId +'"]').after(trs_fixed.join(''));
  612. that.layFixRight.find('tr[data-id="'+ parentId +'"]').after(trs_fixed_r.join(''));
  613. }
  614. that.renderForm();
  615. that.haveInit ? that.scrollPatch() : setTimeout(function(){
  616. that.scrollPatch();
  617. }, 50);
  618. that.haveInit = true;
  619. layer.close(that.tipsIndex);
  620. that.exportTree(parentId,childIsExport);
  621. };
  622. that.key = options.id || options.index;
  623. treeGrid.cache[that.key] = treeDataCache; //记录数据
  624. if(data.length === 0){
  625. that.renderForm();
  626. that.layFixed.remove();
  627. that.layMain.find('tbody').html('');
  628. that.layMain.find('.'+ NONE).remove();
  629. return that.layMain.append('<div class="'+ NONE +'">无数据</div>');
  630. }
  631. render();
  632. };
  633. //找到对应的列元素
  634. Class.prototype.getColElem = function(parent, field){
  635. var that = this
  636. ,options = that.config;
  637. return parent.eq(0).find('.laytable-cell-'+ (options.index + '-' + field) + ':eq(0)');
  638. };
  639. //渲染表单
  640. Class.prototype.renderForm = function(type){
  641. form.render(type, 'LAY-table-'+ this.index);
  642. }
  643. //请求loading
  644. Class.prototype.loading = function(){
  645. var that = this
  646. ,options = that.config;
  647. if(options.loading && options.url){
  648. return layer.msg('数据请求中', {
  649. icon: 16
  650. ,offset: [
  651. that.elem.offset().top + that.elem.height()/2 - 35 - _WIN.scrollTop() + 'px'
  652. ,that.elem.offset().left + that.elem.width()/2 - 90 - _WIN.scrollLeft() + 'px'
  653. ]
  654. ,time: -1
  655. ,anim: -1
  656. ,fixed: false
  657. });
  658. }
  659. };
  660. //同步选中值状态
  661. Class.prototype.setCheckData = function(index, checked){
  662. var that = this
  663. ,options = that.config
  664. ,thisData = treeGrid.cache[that.key];
  665. if(!thisData[index.split("_")[0]][index.split("_")[1]]) return;
  666. if(thisData[index.split("_")[0]][index.split("_")[1]].constructor === Array)
  667. return;
  668. else {
  669. thisData[index.split("_")[0]][index.split("_")[1]][options.checkName]=checked;
  670. }
  671. };
  672. //获取cssRule
  673. Class.prototype.getCssRule = function(field, callback){
  674. var that = this
  675. ,style = that.elem.find('style')[0]
  676. ,sheet = style.sheet || style.styleSheet || {}
  677. ,rules = sheet.cssRules || sheet.rules;
  678. layui.each(rules, function(i, item){
  679. if(item.selectorText === ('.laytable-cell-'+ that.index +'-'+ field)){
  680. return callback(item), true;
  681. }
  682. });
  683. };
  684. //铺满表格主体高度
  685. Class.prototype.fullSize = function(){
  686. var that = this
  687. ,options = that.config
  688. ,height = options.height, bodyHeight;
  689. if(that.fullHeightGap){
  690. height = _WIN.height() - that.fullHeightGap;
  691. if(height < 135) height = 135;
  692. that.elem.css('height', height);
  693. }
  694. //tbody区域高度
  695. bodyHeight = parseFloat(height) - parseFloat(that.layHeader.height()) - 1;
  696. if(options.toolbar){
  697. bodyHeight = bodyHeight - that.layTool.outerHeight();
  698. }
  699. that.layMain.css('height', bodyHeight);
  700. };
  701. //获取滚动条宽度
  702. Class.prototype.getScrollWidth = function(elem){
  703. var width = 0;
  704. if(elem){
  705. width = elem.offsetWidth - elem.clientWidth;
  706. } else {
  707. elem = document.createElement('div');
  708. elem.style.width = '100px';
  709. elem.style.height = '100px';
  710. elem.style.overflowY = 'scroll';
  711. document.body.appendChild(elem);
  712. width = elem.offsetWidth - elem.clientWidth;
  713. document.body.removeChild(elem);
  714. }
  715. return width;
  716. };
  717. //滚动条补丁
  718. Class.prototype.scrollPatch = function(){
  719. var that = this
  720. ,layMainTable = that.layMain.children('table')
  721. ,scollWidth = that.layMain.width() - that.layMain.prop('clientWidth') //纵向滚动条宽度
  722. ,scollHeight = that.layMain.height() - that.layMain.prop('clientHeight') //横向滚动条高度
  723. ,getScrollWidth = that.getScrollWidth(that.layMain[0]) //获取主容器滚动条宽度,如果有的话
  724. ,outWidth = layMainTable.outerWidth() - that.layMain.width(); //表格内容器的超出宽度
  725. //如果存在自动列宽,则要保证绝对填充满,并且不能出现横向滚动条
  726. if(that.autoColNums && outWidth < 5 && !that.scrollPatchWStatus){
  727. var th = that.layHeader.eq(0).find('thead th:last-child')
  728. ,field = th.data('field');
  729. that.getCssRule(field, function(item){
  730. var width = item.style.width || th.outerWidth();
  731. item.style.width = (parseFloat(width) - getScrollWidth - outWidth) + 'px';
  732. //二次校验,如果仍然出现横向滚动条
  733. if(that.layMain.height() - that.layMain.prop('clientHeight') > 0){
  734. item.style.width = parseFloat(item.style.width) - 1 + 'px';
  735. }
  736. that.scrollPatchWStatus = true;
  737. });
  738. }
  739. if(scollWidth && scollHeight){
  740. if(!that.elem.find('.layui-table-patch')[0]){
  741. var patchElem = $('<th class="layui-table-patch"><div class="layui-table-cell"></div></th>'); //补丁元素
  742. patchElem.find('div').css({
  743. width: scollWidth
  744. });
  745. that.layHeader.eq(0).find('thead tr').append(patchElem)
  746. }
  747. } else {
  748. that.layHeader.eq(0).find('.layui-table-patch').remove();
  749. }
  750. //固定列区域高度
  751. var mainHeight = that.layMain.height()
  752. ,fixHeight = mainHeight - scollHeight;
  753. that.layFixed.find(ELEM_BODY).css('height', layMainTable.height() > fixHeight ? fixHeight : 'auto');
  754. //表格宽度小于容器宽度时,隐藏固定列
  755. that.layFixRight[outWidth > 0 ? 'removeClass' : 'addClass'](HIDE);
  756. //操作栏
  757. that.layFixRight.css('right', scollWidth - 1);
  758. };
  759. //事件处理
  760. Class.prototype.events = function(){
  761. var that = this
  762. ,options = that.config
  763. ,_BODY = $('body')
  764. ,dict = {}
  765. ,th = that.layHeader.find('th')
  766. ,resizing
  767. ,ELEM_CELL = '.layui-table-cell'
  768. ,filter = options.elem.attr('lay-filter');
  769. //拖拽调整宽度
  770. th.on('mousemove', function(e){
  771. var othis = $(this)
  772. ,oLeft = othis.offset().left
  773. ,pLeft = e.clientX - oLeft;
  774. if(othis.attr('colspan') > 1 || othis.data('unresize') || dict.resizeStart){
  775. return;
  776. }
  777. dict.allowResize = othis.width() - pLeft <= 10; //是否处于拖拽允许区域
  778. _BODY.css('cursor', (dict.allowResize ? 'col-resize' : ''));
  779. }).on('mouseleave', function(){
  780. var othis = $(this);
  781. if(dict.resizeStart) return;
  782. _BODY.css('cursor', '');
  783. }).on('mousedown', function(e){
  784. var othis = $(this);
  785. if(dict.allowResize){
  786. var field = othis.data('field');
  787. e.preventDefault();
  788. dict.resizeStart = true; //开始拖拽
  789. dict.offset = [e.clientX, e.clientY]; //记录初始坐标
  790. that.getCssRule(field, function(item){
  791. var width = item.style.width || othis.outerWidth();
  792. dict.rule = item;
  793. dict.ruleWidth = parseFloat(width);
  794. dict.minWidth = othis.data('minwidth') || options.cellMinWidth;
  795. });
  796. }
  797. });
  798. //拖拽中
  799. _DOC.on('mousemove', function(e){
  800. if(dict.resizeStart){
  801. e.preventDefault();
  802. if(dict.rule){
  803. var setWidth = dict.ruleWidth + e.clientX - dict.offset[0];
  804. if(setWidth < dict.minWidth) setWidth = dict.minWidth;
  805. dict.rule.style.width = setWidth + 'px';
  806. layer.close(that.tipsIndex);
  807. }
  808. resizing = 1
  809. }
  810. }).on('mouseup', function(e){
  811. if(dict.resizeStart){
  812. dict = {};
  813. _BODY.css('cursor', '');
  814. that.scrollPatch();
  815. }
  816. if(resizing === 2){
  817. resizing = null;
  818. }
  819. });
  820. //复选框选择
  821. that.elem.on('click', 'input[name="layTableCheckbox"]+', function(){
  822. var checkbox = $(this).prev()
  823. ,index = checkbox.parents('tr').eq(0).data('index')
  824. ,checked = checkbox[0].checked;
  825. //单选
  826. that.setCheckData(index, checked);
  827. layui.event.call(this, MOD_NAME, 'checkbox('+ filter +')', {
  828. checked: checked
  829. ,data: treeGrid.cache[that.key] ? (treeGrid.cache[that.key][index.split("_")[0]][index.split("_")[1]] || {}) : {}
  830. ,type: 'one'
  831. });
  832. });
  833. // that.elem.on('click', 'i.layui-tree-head', function()
  834. that.elem.on('click', 'i.treeHead', function () {
  835. var othis = $(this),
  836. othisHtml = othis.html();
  837. if (othisHtml.indexOf("folder") > 0&& !othis.find(".fa").hasClass('fa-folder-o') ) {
  838. var index = othis.parents('tr').eq(0).data('index')
  839. , tr = that.layBody.find('tr[data-index="' + index + '"]')
  840. , request = options.request
  841. , response = options.response
  842. , data = treeGrid.cache[that.key][index.split("_")[0]][index.split("_")[1]];
  843. var levelField = options.levelField
  844. , paramName = options.paramName
  845. , paramValue = options.paramValue
  846. , scopeField = options.scopeField;
  847. var tempValue = data[paramValue];
  848. $.ajax({
  849. type: options.method || 'get'
  850. , url: options.url
  851. , data: paramName + "=" + tempValue
  852. , dataType: 'json'
  853. , success: function (res) {
  854. if (res[response.statusName] != response.statusCode) {
  855. that.renderForm();
  856. return that.layMain.html('<div class="' + NONE + '">' + (res[response.msgName] || '返回的数据状态异常') + '</div>');
  857. }
  858. that.renderData(data[paramValue], res);
  859. options.time = (new Date().getTime() - that.startTime) + ' ms'; //耗时(接口请求+视图渲染)
  860. }
  861. , error: function (e, m) {
  862. that.layMain.html('<div class="' + NONE + '">数据接口请求异常</div>');
  863. that.renderForm();
  864. }
  865. });
  866. }
  867. });
  868. //单行点击事件 luo
  869. that.elem.on('click', 'td', function()
  870. {
  871. var othis = $(this);
  872. var fieldIndex= othis.attr("data-field");
  873. if(!(/^\+?[1-9][0-9]*$/.test(fieldIndex))) //排除checkBox点击事件, 因为td 点击只能如果只要不是checkBox点击,checkBox 点击一定是数字
  874. {
  875. var childs = that.layBody.find('input[name="layTableCheckbox"]');
  876. var index = othis.parents('tr').eq(0).data('index');
  877. childs.each(function(i, item)
  878. {
  879. if(item.id==="tableCheckBox"+index)
  880. {
  881. item.checked = true;
  882. treeGrid.cache[that.key][index.split("_")[0]][index.split("_")[1]][options.checkName]=true;
  883. }
  884. else
  885. {
  886. var otherItemId=item.id.substring("tableCheckBox".length,item.id.length);
  887. item.checked = false;
  888. treeGrid.cache[that.key][otherItemId.split("_")[0]][otherItemId.split("_")[1]][options.checkName]=false;
  889. }
  890. });
  891. that.renderForm();
  892. layui.event.call(this, MOD_NAME, 'checkbox('+ filter +')', {
  893. checked: true
  894. ,data: treeGrid.cache[that.key] ? (treeGrid.cache[that.key][index.split("_")[0]][index.split("_")[1]] || {}) : {}
  895. ,type: 'one'
  896. });
  897. }
  898. });
  899. that.elem.on('dblclick', 'td', function() {
  900. var othis = $(this), fieldIndex= othis.attr("data-field");
  901. if ((/^\+?[1-9][0-9]*$/.test(fieldIndex))) return;
  902. if (othis.closest("tr").find("i.treeHead").find("i.fa").hasClass('fa-folder-o')) return;
  903. var index = othis.parents('tr').eq(0).data('index')
  904. ,tr = that.layBody.find('tr[data-index="'+ index +'"]')
  905. ,request = options.request
  906. ,response = options.response
  907. ,data = treeGrid.cache[that.key][index.split("_")[0]][index.split("_")[1]];
  908. var levelField=options.levelField
  909. ,paramName=options.paramName
  910. ,paramValue=options.paramValue
  911. ,scopeField=options.scopeField;
  912. var tempValue=data[paramValue];
  913. $.ajax({
  914. type: options.method || 'get'
  915. ,url: options.url
  916. ,data: paramName+"="+tempValue
  917. ,dataType: 'json'
  918. ,success: function(res){
  919. if(res[response.statusName] != response.statusCode){
  920. that.renderForm();
  921. return that.layMain.html('<div class="'+ NONE +'">'+ (res[response.msgName] || '返回的数据状态异常') +'</div>');
  922. }
  923. that.renderData(data[paramValue],res);
  924. options.time = (new Date().getTime() - that.startTime) + ' ms'; //耗时(接口请求+视图渲染)
  925. }
  926. ,error: function(e, m){
  927. that.layMain.html('<div class="'+ NONE +'">数据接口请求异常</div>');
  928. that.renderForm();
  929. }
  930. });
  931. });
  932. //行事件
  933. that.layBody.on('mouseenter', 'tr', function(){
  934. var othis = $(this)
  935. ,index = othis.index();
  936. that.layBody.find('tr:eq('+ index +')').addClass(ELEM_HOVER)
  937. }).on('mouseleave', 'tr', function(){
  938. var othis = $(this)
  939. ,index = othis.index();
  940. that.layBody.find('tr:eq('+ index +')').removeClass(ELEM_HOVER)
  941. });
  942. //单元格编辑
  943. that.layBody.on('change', '.'+ELEM_EDIT, function(){
  944. var othis = $(this)
  945. ,value = this.value
  946. ,field = othis.parent().data('field')
  947. ,index = othis.parents('tr').eq(0).data('index')
  948. ,data = treeGrid.cache[that.key][index.split("_")[0]][index.split("_")[1]];
  949. data[field] = value; //更新缓存中的值
  950. layui.event.call(this, MOD_NAME, 'edit('+ filter +')', {
  951. value: value
  952. ,data: data
  953. ,field: field
  954. });
  955. }).on('blur', '.'+ELEM_EDIT, function(){
  956. var templet
  957. ,othis = $(this)
  958. ,field = othis.parent().data('field')
  959. ,index = othis.parents('tr').eq(0).data('index')
  960. ,data = treeGrid.cache[that.key][index];
  961. that.eachCols(function(i, item){
  962. if(item.field == field && item.templet){
  963. templet = item.templet;
  964. }
  965. });
  966. othis.siblings(ELEM_CELL).html(
  967. templet ? laytpl($(templet).html() || this.value).render(data) : this.value
  968. );
  969. othis.parent().data('content', this.value);
  970. othis.remove();
  971. });
  972. //单元格事件
  973. that.layBody.on('click', 'td', function(){
  974. var othis = $(this)
  975. ,field = othis.data('field')
  976. ,editType = othis.data('edit')
  977. ,elemCell = othis.children(ELEM_CELL);
  978. layer.close(that.tipsIndex);
  979. if(othis.data('off')) return;
  980. //显示编辑表单
  981. if(editType){
  982. if(editType === 'select') { //选择框
  983. //var select = $('<select class="'+ ELEM_EDIT +'" lay-ignore><option></option></select>');
  984. //othis.find('.'+ELEM_EDIT)[0] || othis.append(select);
  985. } else { //输入框
  986. var input = $('<input class="layui-input '+ ELEM_EDIT +'">');
  987. input[0].value = othis.data('content') || elemCell.text();
  988. othis.find('.'+ELEM_EDIT)[0] || othis.append(input);
  989. input.focus();
  990. }
  991. return;
  992. }
  993. //如果出现省略,则可查看更多
  994. if(elemCell.find('.layui-form-switch,.layui-form-checkbox')[0]) return; //限制不出现更多(暂时)
  995. if(Math.round(elemCell.prop('scrollWidth')) > Math.round(elemCell.outerWidth())){
  996. that.tipsIndex = layer.tips([
  997. '<div class="layui-table-tips-main" style="margin-top: -'+ (elemCell.height() + 16) +'px;'+ function(){
  998. if(options.size === 'sm'){
  999. return 'padding: 4px 15px; font-size: 12px;';
  1000. }
  1001. if(options.size === 'lg'){
  1002. return 'padding: 14px 15px;';
  1003. }
  1004. return '';
  1005. }() +'">'
  1006. ,elemCell.html()
  1007. ,'</div>'
  1008. ,'<i class="layui-icon layui-table-tips-c">&#x1006;</i>'
  1009. ].join(''), elemCell[0], {
  1010. tips: [3, '']
  1011. ,time: -1
  1012. ,anim: -1
  1013. ,maxWidth: (device.ios || device.android) ? 300 : 600
  1014. ,isOutAnim: false
  1015. ,skin: 'layui-table-tips'
  1016. ,success: function(layero, index){
  1017. layero.find('.layui-table-tips-c').on('click', function(){
  1018. layer.close(index);
  1019. });
  1020. }
  1021. });
  1022. }
  1023. });
  1024. //工具条操作事件
  1025. that.layBody.on('click', '*[lay-event]', function(){
  1026. var othis = $(this)
  1027. ,index = othis.parents('tr').eq(0).data('index')
  1028. ,tr = that.layBody.find('tr[data-index="'+ index +'"]')
  1029. ,ELEM_CLICK = 'layui-table-click'
  1030. ,data = treeGrid.cache[that.key][index.split("_")[0]][index.split("_")[1]];
  1031. layui.event.call(this, MOD_NAME, 'tool('+ filter +')', {
  1032. data: treeGrid.clearCacheKey(data)
  1033. ,event: othis.attr('lay-event')
  1034. ,tr: tr
  1035. ,del: function(){
  1036. treeGrid.cache[that.key][index] = [];
  1037. tr.remove();
  1038. that.scrollPatch();
  1039. }
  1040. ,update: function(fields){
  1041. fields = fields || {};
  1042. layui.each(fields, function(key, value){
  1043. if(key in data){
  1044. var templet, td = tr.children('td[data-field="'+ key +'"]');
  1045. data[key] = value;
  1046. that.eachCols(function(i, item2){
  1047. if(item2.field == key && item2.templet){
  1048. templet = item2.templet;
  1049. }
  1050. });
  1051. td.children(ELEM_CELL).html(
  1052. templet ? laytpl($(templet).html() || value).render(data) : value
  1053. );
  1054. td.data('content', value);
  1055. }
  1056. });
  1057. }
  1058. });
  1059. tr.addClass(ELEM_CLICK).siblings('tr').removeClass(ELEM_CLICK);
  1060. });
  1061. //同步滚动条
  1062. that.layMain.on('scroll', function(){
  1063. var othis = $(this)
  1064. ,scrollLeft = othis.scrollLeft()
  1065. ,scrollTop = othis.scrollTop();
  1066. that.layHeader.scrollLeft(scrollLeft);
  1067. that.layFixed.find(ELEM_BODY).scrollTop(scrollTop);
  1068. layer.close(that.tipsIndex);
  1069. });
  1070. _WIN.on('resize', function(){ //自适应
  1071. that.fullSize();
  1072. that.scrollPatch();
  1073. });
  1074. };
  1075. //初始化
  1076. treeGrid.init = function(filter, settings){
  1077. settings = settings || {};
  1078. var that = this
  1079. ,elemTable = filter ? $('table[lay-filter="'+ filter +'"]') : $(ELEM + '[lay-data]')
  1080. ,errorTips = 'Table element property lay-data configuration item has a syntax error: ';
  1081. //遍历数据表格
  1082. elemTable.each(function(){
  1083. var othis = $(this), treeGridData = othis.attr('lay-data');
  1084. try{
  1085. treeGridData = new Function('return '+ treeGridData)();
  1086. } catch(e){
  1087. hint.error(errorTips + treeGridData)
  1088. }
  1089. var cols = [], options = $.extend({
  1090. elem: this
  1091. ,cols: []
  1092. ,data: []
  1093. ,skin: othis.attr('lay-skin') //风格
  1094. ,size: othis.attr('lay-size') //尺寸
  1095. ,even: typeof othis.attr('lay-even') === 'string' //偶数行背景
  1096. }, treeGrid.config, settings, treeGridData);
  1097. filter && othis.hide();
  1098. //获取表头数据
  1099. othis.find('thead>tr').each(function(i){
  1100. options.cols[i] = [];
  1101. $(this).children().each(function(ii){
  1102. var th = $(this), itemData = th.attr('lay-data');
  1103. try{
  1104. itemData = new Function('return '+ itemData)();
  1105. } catch(e){
  1106. return hint.error(errorTips + itemData)
  1107. }
  1108. var row = $.extend({
  1109. title: th.text()
  1110. ,colspan: th.attr('colspan') || 0 //列单元格
  1111. ,rowspan: th.attr('rowspan') || 0 //行单元格
  1112. }, itemData);
  1113. if(row.colspan < 2) cols.push(row);
  1114. options.cols[i].push(row);
  1115. });
  1116. });
  1117. //获取表体数据
  1118. othis.find('tbody>tr').each(function(i1){
  1119. var tr = $(this), row = {};
  1120. //如果定义了字段名
  1121. tr.children('td').each(function(i2, item2){
  1122. var td = $(this)
  1123. ,field = td.data('field');
  1124. if(field){
  1125. return row[field] = td.html();
  1126. }
  1127. });
  1128. //如果未定义字段名
  1129. layui.each(cols, function(i3, item3){
  1130. var td = tr.children('td').eq(i3);
  1131. row[item3.field] = td.html();
  1132. });
  1133. options.data[i1] = row;
  1134. });
  1135. treeGrid.render(options);
  1136. });
  1137. return that;
  1138. };
  1139. treeGrid.refreshNode = function (id, parentId, options) {
  1140. $("#treeHead"+parentId).attr("isexport","false");
  1141. $("#treeHead"+parentId).click();
  1142. };
  1143. //表格选中状态
  1144. treeGrid.checkStatus = function(id){
  1145. var nums = 0
  1146. ,invalidNum = 0
  1147. ,arr = []
  1148. ,data = treeGrid.cache[id] || [];
  1149. //计算全选个数
  1150. layui.each(data, function(i, item)
  1151. {
  1152. layui.each(item, function(j, itemTemp)
  1153. {
  1154. if (itemTemp.constructor === Array) {
  1155. invalidNum++; //无效数据,或已删除的
  1156. return;
  1157. }
  1158. if (itemTemp[treeGrid.config.checkName]) {
  1159. nums++;
  1160. arr.push(treeGrid.clearCacheKey(itemTemp));
  1161. }
  1162. });
  1163. });
  1164. return {
  1165. data: arr //选中的数据
  1166. ,isAll: data.length ? (nums === (data.length - invalidNum)) : false //是否全选
  1167. };
  1168. };
  1169. //表格重载
  1170. thisTable.config = {};
  1171. treeGrid.reload = function(id, options){
  1172. var config = thisTable.config[id];
  1173. options = options || {};
  1174. if(!config) return hint.error('The ID option was not found in the treeGrid instance');
  1175. if(options.data && options.data.constructor === Array) delete config.data;
  1176. return treeGrid.render($.extend(true, {}, config, options));
  1177. };
  1178. //核心入口
  1179. treeGrid.render = function(options){
  1180. var inst = new Class(options);
  1181. return thisTable.call(inst);
  1182. };
  1183. //清除临时Key
  1184. treeGrid.clearCacheKey = function(data){
  1185. data = $.extend({}, data);
  1186. delete data[treeGrid.config.checkName];
  1187. delete data[treeGrid.config.indexName];
  1188. return data;
  1189. };
  1190. //自动完成渲染
  1191. treeGrid.init();
  1192. exports(MOD_NAME, treeGrid);
  1193. });