1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182 |
- layui.define(['laytpl', 'laypage', 'layer', 'form'], function(exports){
- "use strict";
- var $ = layui.$
- ,laytpl = layui.laytpl
- ,laypage = layui.laypage
- ,layer = layui.layer
- ,form = layui.form
- ,hint = layui.hint()
- ,device = layui.device()
-
- ,table = {
- claObj:{}
- ,obj:{}
- ,config: {
- checkName: 'LAY_CHECKED'
- ,indexName: 'LAY_TABLE_INDEX'
- }
-
- ,cache: {
- tableId:{
- data:{
- list:[]
- ,map:{}
- ,treeList:[]
- }
- }
- ,cla:{
- claIds:{
- tableId:true
- }
- ,claObjs:{
- tableId:{}
- }
- }
- ,selectcode:{
- demokey:[
- {
- key:{value:''}
- }
- ]
- }
- }
- ,index: layui.table ? (layui.table.index + 10000) : 0
-
- ,set: function(options){
- var that = this;
- that.config = $.extend({}, that.config, options);
- return that;
- }
-
- ,on: function(events, callback){
- return layui.onevent.call(this, MOD_NAME, events, callback);
- }
- ,getClass:function (tableId) {
- return table.cache.cla.claObjs[tableId];;
- }
- ,pushClass:function (tableId,that) {
- table.cache.cla.claObjs[tableId]=that;
- }
- ,isCalss:function (tableId) {
- var ids=this.cache.cla.claIds||{};
- return ids.hasOwnProperty(tableId)||false;
- }
- ,isClassYes:function (tableId) {
- var ids=this.cache.cla.claIds||{};
- return ids[tableId]||false;
- }
- ,pushClassIds:function (tableId,is) {
- this.cache.cla.claIds[tableId]=is;
- }
- ,setObj:function (tableId,key,o) {
- if(!this.obj[tableId])this.obj[tableId]={};
- this.obj[tableId][key]=o;
- }
- ,getObj:function (tableId, key) {
- return this.obj[tableId]?this.obj[tableId][key]:null;
- }
-
- ,getDataList:function (tableId) {
- if(table.cache[tableId]){
- return table.cache[tableId].data.list;
- }
- return [];
- }
-
- ,setDataList:function (tableId, list) {
- if(!table.cache[tableId])table.cache[tableId]={};
- if(!table.cache[tableId].data)table.cache[tableId].data={};
- if(!table.cache[tableId].data.list)table.cache[tableId].data.list=[];
- table.cache[tableId].data.list=list;
- }
-
- ,getDataMap:function (tableId) {
- if(table.cache[tableId]){
- return table.cache[tableId].data.map;
- }
- return {};
- }
-
- ,setDataMap:function (tableId, map) {
- if(!table.cache[tableId])table.cache[tableId]={};
- if(!table.cache[tableId].data)table.cache[tableId].data={};
- if(!table.cache[tableId].data.map)table.cache[tableId].data.map={};
- table.cache[tableId].data.map=map;
- }
-
- ,getDataTreeList:function (tableId) {
- if(table.cache[tableId]){
- return table.cache[tableId].data.treeList;
- }
- return [];
- }
-
- ,setDataTreeList:function (tableId, treeList) {
- if(!table.cache[tableId])table.cache[tableId]={};
- if(!table.cache[tableId].data)table.cache[tableId].data={};
- if(!table.cache[tableId].data.treeList)table.cache[tableId].data.treeList={};
- table.cache[tableId].data.treeList=treeList;
- }
-
- ,init:function (filter, settings) {
- settings = settings || {};
- var that = this
- ,elemTable = filter ? $('table[lay-filter="'+ filter +'"]') : $(ELEM + '[lay-data]')
- ,errorTips = 'Table element property lay-data configuration item has a syntax error: ';
-
- elemTable.each(function(){
- var othis = $(this), tableData = othis.attr('lay-data');
- try{
- tableData = new Function('return '+ tableData)();
- } catch(e){
- hint.error(errorTips + tableData)
- }
- var cols = [], options = $.extend({
- elem: this
- ,cols: []
- ,data: []
- ,skin: othis.attr('lay-skin')
- ,size: othis.attr('lay-size')
- ,even: typeof othis.attr('lay-even') === 'string'
- }, table.config, settings, tableData);
- filter && othis.hide();
-
- othis.find('thead>tr').each(function(i){
- options.cols[i] = [];
- $(this).children().each(function(ii){
- var th = $(this), itemData = th.attr('lay-data');
- try{
- itemData = new Function('return '+ itemData)();
- } catch(e){
- return hint.error(errorTips + itemData)
- }
- var row = $.extend({
- title: th.text()
- ,colspan: th.attr('colspan') || 0
- ,rowspan: th.attr('rowspan') || 0
- }, itemData);
- if(row.colspan < 2) cols.push(row);
- options.cols[i].push(row);
- });
- });
-
- othis.find('tbody>tr').each(function(i1){
- var tr = $(this), row = {};
-
- tr.children('td').each(function(i2, item2){
- var td = $(this)
- ,field = td.data('field');
- if(field){
- return row[field] = td.html();
- }
- });
-
- layui.each(cols, function(i3, item3){
- var td = tr.children('td').eq(i3);
- row[item3.field] = td.html();
- });
- options.data[i1] = row;
- });
- table.render(options);
- });
- return that;
- }
-
- ,render:function (options) {
- table.pushClassIds(options.id);
- var inst = new Class(options);
- return thisTable.call(inst);
- }
-
- ,ready:function (tableId,fn) {
- var is=false;
- var myDate=new Date();
- function isReady() {
- if(tableId){
- var that=table.getClass(tableId);
- if(that&&that.hasOwnProperty('layBody')){
- fn(that);
- is=true;
- }else{
- var myDate2=new Date();
- var i=myDate2.getTime()-myDate.getTime();
- if(i<=(1000*10)&&!is){
- setTimeout(isReady,50);
- }
- }
- }
- }
- if(tableId&&fn){
- setTimeout(isReady,50);
- }
- }
-
- ,checkStatus:function (tableId) {
- var nums = 0
- ,invalidNum = 0
- ,arr = []
- ,data = table.getDataList(tableId) || [];
-
- layui.each(data, function(i, item){
- if(item.constructor === Array){
- invalidNum++;
- return;
- }
- if(item[table.config.checkName]){
- nums++;
- arr.push(table.clearCacheKey(item));
- }
- });
- return {
- data: arr
- ,isAll: data.length ? (nums === (data.length - invalidNum)) : false
- };
- }
-
- ,setCheckStatus:function(tableId, fildName, ids){
- var retObj=null;
- var that=table.getClass(tableId)
- ,invalidNum = 0
- ,arr = []
- ,data = table.getDataList(tableId) || []
- ,childs = that.layBody.find('input[name="layTableCheckbox"]')
- ;
- if(fildName&&ids){
- var idsarr=ids.split(',');
- idsarr.forEach(function (o) {
-
- var temo=null;
- data.forEach(function (e) {
- var b1=e[fildName]+"";
- var b2=o+"";
- if(b1==b2){
- temo=e;
- return;
- };
- });
- if(temo){
- var v=temo[table.config.indexName];
- that.layBody.find('input[name="layTableCheckbox"][value="'+v+'"]').prop("checked",true);
- that.setCheckData(v, true);
- }
- });
- that.syncCheckAll();
- that.renderForm('checkbox');
- }
- return retObj;
- }
-
- ,radioStatus:function (tableId) {
- var retObj=null;
- var nums = 0
- ,invalidNum = 0
- ,arr = []
- ,data = table.getDataList(tableId) || [];
- var v=$("input[name='"+TABLE_RADIO_ID+tableId+"']:checked").val();
- v=parseInt(v);
- data.forEach(function (e) {
- if(e[table.config.indexName]==v){
- retObj=e;
- return;
- };
- });
- return retObj;
- }
-
- ,setRadioStatus:function (tableId,fildName,value) {
- var retObj=null;
- var nums = 0
- ,invalidNum = 0
- ,arr = []
- ,data = table.getDataList(tableId) || [];
- if(fildName&&value){
- data.forEach(function (e) {
- var b1=e[fildName]+"";
- var b2=value+"";
- if(b1==b2){
- retObj=e;
- return;
- };
- });
- if(retObj){
- var v=retObj[table.config.indexName];
- $("input:radio[name='"+TABLE_RADIO_ID+tableId+"'][value='"+v+"']").prop("checked",true);
- form.render('radio');
- }
- }
- return retObj;
- }
-
- ,clearCacheKey:function (data) {
- data = $.extend({}, data);
- delete data[table.config.checkName];
- delete data[table.config.indexName];
- return data;
- }
-
- ,query:function (tableId, options) {
- var that= table.getClass(tableId);
- that.renderTdCss();
- that.pullData(that.page, that.loading());
- }
-
- ,reload:function (tableId, options) {
- var config = thisTable.config[tableId];
- options = options || {};
- if(!config) return hint.error('The ID option was not found in the table instance');
- if(options.data && options.data.constructor === Array) delete config.data;
- return table.render($.extend(true, {}, config, options));
- }
-
- ,addRow:function (tableId, index, data) {
- var that=table.getClass(tableId)
- ,options=that.config
- ,invalidNum = 0
- ,arr = []
- ,list = table.getDataList(tableId) || [];
-
- list.splice(index,0,data);
- table.restNumbers(list);
- that.resetData(list);
-
- if(options.isTree) {
-
- var uo=that.treeFindUpData(data);
- if(uo) {
- that.treeNodeOpen(uo, true);
- that.renderTreeConvertShowName(uo);
- }
- }
-
- var tds=that.renderTr(data,data[table.config.indexName]);
- var trs='<tr data-index="'+ data[table.config.indexName] +'"'+that.renderTrUpids(data)+'>'+ tds.join('') + '</tr>';
- if(index==0){
- var tbody=that.layBody.find('table tbody');
- $(tbody).prepend(trs);
- that.layBody.find(".layui-none").remove();
- }else{
- var o=that.layBody.find('[data-index='+(index-1)+']');
- $(o).after(trs);
- }
- that.renderPage(that.config.page.count+1);
- that.restNumbers();
- }
-
- ,delRow:function (tableId, data) {
-
- var that=table.getClass(tableId)
- ,options=that.config
- ,list=table.getDataList(tableId);
- var sonList=[];
- var delIds={};
- var delDatas=[];
- if(!that||!data)return;
- if(table.kit.isArray(data)){
- delDatas=data;
- }else{
- delDatas[0]=data;
- }
- sonList=options.isTree?table.treeFindSonList(that.config.id,delDatas):delDatas;
-
- sonList.forEach(function (temo) {
- var index=temo[table.config.indexName];
- delIds[index]=index;
- var tr = that.layBody.find('tr[data-index="'+ index +'"]');
- tr.remove();
- });
-
- that.restNumbers();
- var newList=[];
- for (var i=0,len=list.length;i<len;i++) {
- var isP=true;
- var temo1=null;
- sonList.forEach(function (temo) {
- if (temo[table.config.indexName] === list[i][table.config.indexName]) {
- isP = false;
- }
- });
- if(isP){
- newList.push(list[i]);
- }
- }
- table.restNumbers(newList);
- that.resetData(newList);
- if(options.page)that.renderPage(that.config.page.count-Object.keys(delIds).length);
- }
- ,restNumbers:function (list) {
- if(!list)return;
- var i=0;
- list.forEach(function (o) {
- o[table.config.indexName]=i;
- i++;
- });
- }
-
- ,treeFindSonList:function (tableId,data) {
- var that=table.getClass(tableId);
- if(!that||!data)return [];
- var delDatas=[];
- var sonList=[];
- var delIds={};
- if(table.kit.isArray(data)){
- delDatas=data;
- }else{
- delDatas[0]=data;
- }
- delDatas.forEach(function (temo) {
- if(temo.children.length>0){
- var temSonList=that.treeFindSonData(temo);
- temSonList.forEach(function (temii) {
- if(!delIds[temii[table.config.indexName]]){
- sonList.push(temii);
- delIds[temii[table.config.indexName]]=temii[table.config.indexName];
- }
- });
- }
- sonList.push(temo);
- delIds[temo[table.config.indexName]]=temo[table.config.indexName];
- });
- return sonList;
- }
-
- ,treeFindSonIds:function (tableId,data) {
- var delIds=[];
- var sonList=table.treeFindSonList(tableId,data);
- sonList.forEach(function (temo) {
- delIds.push([table.config.indexName]);
- });
- return delIds;
- }
-
- ,treeFindSonIdFields:function (tableId,data) {
- var idField=[];
- var that=table.getClass(tableId);
- var sonList=table.treeFindSonList(tableId,data);
- sonList.forEach(function (temo) {
- idField.push(temo[that.config.idField]);
- });
- return idField;
- }
-
- ,kit:{
- isArray:function (o) {
- return Object.prototype.toString.call(o) === '[object Array]';
- }
- }
- }
-
- ,thisTable = function(){
- var that = this
- ,options = that.config
- ,id = options.id;
- id && (thisTable.config[id] = options);
- return {
- reload: function(options){
- that.reload.call(that, options);
- }
- ,config: options
- }
- }
-
- ,MOD_NAME = 'dltable', ELEM = '.layui-table', THIS = 'layui-this', SHOW = 'layui-show', HIDE = 'layui-hide', DISABLED = 'layui-disabled', NONE = 'layui-none'
- ,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'
- ,TABLE_RADIO_ID='table_radio_'
- ,ELEM_FILTER='.layui-table-filter'
- ,TREE_ID='treeId',TREE_UPID='treeUpId',TREE_SHOW_NAME='treeShowName',TREE_KEY_MAP='tree_key_map'
-
- ,TPL_HEADER = function(options){
- var rowCols = '{{#if(item2.colspan){}} colspan="{{item2.colspan}}"{{#} if(item2.rowspan){}} rowspan="{{item2.rowspan}}"{{#}}}';
- options = options || {};
- return ['<table cellspacing="0" cellpadding="0" border="0" class="layui-table" '
- ,'{{# if(d.data.skin){ }}lay-skin="{{d.data.skin}}"{{# } }} {{# if(d.data.size){ }}lay-size="{{d.data.size}}"{{# } }} {{# if(d.data.even){ }}lay-even{{# } }}>'
- ,'<thead>'
- ,'{{# layui.each(d.data.cols, function(i1, item1){ }}'
- ,'<tr>'
- ,'{{# layui.each(item1, function(i2, item2){ }}'
- ,'{{# if(item2.fixed && item2.fixed !== "right"){ left = true; } }}'
- ,'{{# if(item2.fixed === "right"){ right = true; } }}'
- ,function(){
- if(options.fixed && options.fixed !== 'right'){
- return '{{# if(item2.fixed && item2.fixed !== "right"){ }}';
- }
- if(options.fixed === 'right'){
- return '{{# if(item2.fixed === "right"){ }}';
- }
- return '';
- }()
- ,'<th data-field="{{ item2.field||i2 }}" {{# if(item2.minWidth){ }}data-minwidth="{{item2.minWidth}}"{{# } }} '+ rowCols +' {{# if(item2.unresize){ }}data-unresize="true"{{# } }}>'
- ,'<div class="layui-table-cell laytable-cell-'
- ,'{{# if(item2.colspan > 1){ }}'
- ,'group'
- ,'{{# } else { }}'
- ,'{{d.index}}-{{item2.field || i2}}'
- ,'{{# if(item2.type !== "normal"){ }}'
- ,' laytable-cell-{{ item2.type }}'
- ,'{{# } }}'
- ,'{{# } }}'
- ,'" {{#if(item2.align){}}align="{{item2.align}}"{{#}}}>'
- ,'{{# if(item2.type === "checkbox"){ }}'
- ,'<input type="checkbox" name="layTableCheckbox" lay-skin="primary" lay-filter="layTableAllChoose" {{# if(item2[d.data.checkName]){ }}checked{{# }; }}>'
- ,'{{# } else { }}'
- ,'<span>{{item2.title||""}}</span>'
- ,'{{# if(!(item2.colspan > 1) && item2.sort){ }}'
- ,'<span class="layui-table-sort layui-inline"><i class="layui-edge layui-table-sort-asc"></i><i class="layui-edge layui-table-sort-desc"></i></span>'
- ,'{{# } }}'
- ,'{{# } }}'
- ,'</div>'
- ,'</th>'
- ,(options.fixed ? '{{# }; }}' : '')
- ,'{{# }); }}'
- ,'</tr>'
- ,'{{# }); }}'
- ,'</thead>'
- ,'</table>'].join('');
- }
-
- ,TPL_FILTER = function(options){
- }
-
- ,TPL_BODY = ['<table cellspacing="0" cellpadding="0" border="0" class="layui-table" '
- ,'{{# if(d.data.skin){ }}lay-skin="{{d.data.skin}}"{{# } }} {{# if(d.data.size){ }}lay-size="{{d.data.size}}"{{# } }} {{# if(d.data.even){ }}lay-even{{# } }}>'
- ,'<tbody></tbody>'
- ,'</table>'].join('')
-
- ,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;{{# } }}">'
- ,'{{# if(d.data.toolbar){ }}'
- ,'<div class="layui-table-tool"></div>'
- ,'{{# } }}'
- ,'<div class="layui-table-box">'
- ,'{{# var left, right; }}'
- ,'<div class="layui-table-header">'
- ,TPL_HEADER()
- ,'</div>'
- ,'<div class="layui-table-filter">'
- ,TPL_FILTER()
- ,'</div>'
- ,'<div class="layui-table-body layui-table-main">'
- ,TPL_BODY
- ,'</div>'
- ,'{{# if(left){ }}'
- ,'<div class="layui-table-fixed layui-table-fixed-l">'
- ,'<div class="layui-table-header">'
- ,TPL_HEADER({fixed: true})
- ,'</div>'
- ,'<div class="layui-table-body">'
- ,TPL_BODY
- ,'</div>'
- ,'</div>'
- ,'{{# }; }}'
- ,'{{# if(right){ }}'
- ,'<div class="layui-table-fixed layui-table-fixed-r">'
- ,'<div class="layui-table-header">'
- ,TPL_HEADER({fixed: 'right'})
- ,'<div class="layui-table-mend"></div>'
- ,'</div>'
- ,'<div class="layui-table-body">'
- ,TPL_BODY
- ,'</div>'
- ,'</div>'
- ,'{{# }; }}'
- ,'</div>'
- ,'{{# if(d.data.page){ }}'
- ,'<div class="layui-table-page">'
- ,'<div id="layui-table-page{{d.index}}"></div>'
- ,'</div>'
- ,'{{# } }}'
-
- ,'</div>'].join('')
- ,_WIN = $(window)
- ,_DOC = $(document)
-
- ,Class = function(options){
- var that = this;
- that.index = ++table.index;
- that.config = $.extend({}, that.config, table.config, options);
- that.render();
- table.pushClass(options.id,that);
- };
-
- Class.prototype.config = {
- limit: 10
- ,loading: true
- ,cellMinWidth: 60
- ,text: {
- none: '无数据'
- }
- ,isFilter:false
- ,method:'post'
- };
-
- Class.prototype.render = function(){
- var that = this
- ,options = that.config;
- options.elem = $(options.elem);
- options.where = options.where || {};
- options.id = options.id || options.elem.attr('id');
-
- options.request = $.extend({
- pageName: 'page'
- ,limitName: 'limit'
- }, options.request)
-
- options.response = $.extend({
- statusName: 'code'
- ,statusCode: 0
- ,msgName: 'msg'
- ,dataName: 'data'
- ,countName: 'count'
- }, options.response);
-
- if(typeof options.page === 'object'){
- options.limit = options.page.limit || options.limit;
- options.limits = options.page.limits || options.limits;
- that.page = options.page.curr = options.page.curr || 1;
- delete options.page.elem;
- delete options.page.jump;
- }
- if(!options.elem[0]) return that;
- that.setArea();
-
- var othis = options.elem
- ,hasRender = othis.next('.' + ELEM_VIEW)
-
- ,reElem = that.elem = $(laytpl(TPL_MAIN).render({
- VIEW_CLASS: ELEM_VIEW
- ,data: options
- ,index: that.index
- }));
- options.index = that.index;
-
- hasRender[0] && hasRender.remove();
- othis.after(reElem);
- that.renderTdCss();
-
- that.layHeader = reElem.find(ELEM_HEADER);
- that.layMain = reElem.find(ELEM_MAIN);
- that.layBody = reElem.find(ELEM_BODY);
- that.layFixed = reElem.find(ELEM_FIXED);
- that.layFixLeft = reElem.find(ELEM_FIXL);
- that.layFixRight = reElem.find(ELEM_FIXR);
- that.layTool = reElem.find(ELEM_TOOL);
- that.layPage = reElem.find(ELEM_PAGE);
- that.layFilter=reElem.find(ELEM_FILTER);
- that.layTool.html(
- laytpl($(options.toolbar).html()||'').render(options)
- );
- if(options.height) that.fullSize();
-
- if(options.cols.length > 1){
- var th = that.layFixed.find(ELEM_HEADER).find('th');
- th.height(that.layHeader.height() - 1 - parseFloat(th.css('padding-top')) - parseFloat(th.css('padding-bottom')));
- }
-
- if(options.isFilter){
- that.layFilter.html(
- that.renderFilter()
- );
- }
-
- that.pullData(that.page);
- that.events();
- };
-
- Class.prototype.initOpts = function(item){
- var that = this,
- options = that.config
- ,initWidth = {
- checkbox: 48
- ,space: 15
- ,numbers: 40
- };
-
- if(item.checkbox) item.type = "checkbox";
- if(item.space) item.type = "space";
- if(!item.type) item.type = "normal";
- if(item.type !== "normal"){
- item.unresize = true;
- item.width = item.width || initWidth[item.type];
- }
- if(options.isFilter){
- if(item.isFilter!=false){
- item.isFilter=true;
- }
- }
- };
-
- Class.prototype.setArea = function(){
- var that = this,
- options = that.config
- ,colNums = 0
- ,autoColNums = 0
- ,autoWidth = 0
- ,countWidth = 0
- ,cntrWidth = options.width ||function(){
-
- var getWidth = function(parent){
- var width, isNone;
- parent = parent || options.elem.parent()
- width = parent.width();
- try {
- isNone = parent.css('display') === 'none';
- } catch(e){}
- if(parent[0] && (!width || isNone)) return getWidth(parent.parent());
- return width;
- };
- return getWidth();
- }();
-
- that.eachCols(function(){
- colNums++;
- });
-
- cntrWidth = cntrWidth - function(){
- return (options.skin === 'line' || options.skin === 'nob') ? 2 : colNums + 1;
- }();
-
- layui.each(options.cols, function(i1, item1){
- layui.each(item1, function(i2, item2){
- var width;
- if(!item2){
- item1.splice(i2, 1);
- return;
- }
- that.initOpts(item2);
- width = item2.width || 0;
- if(item2.colspan > 1) return;
- if(/\d+%$/.test(width)){
- item2.width = width = Math.floor((parseFloat(width) / 100) * cntrWidth);
- } else if(item2._is_width_dev||!width){
- item2._is_width_dev=true;
- item2.width = width = 0;
- autoColNums++;
- }
- countWidth = countWidth + width;
- });
- });
- that.autoColNums = autoColNums;
-
- (cntrWidth > countWidth && autoColNums) && (
- autoWidth = (cntrWidth - countWidth) / autoColNums
- );
- layui.each(options.cols, function(i1, item1){
- layui.each(item1, function(i2, item2){
- var minWidth = item2.minWidth || options.cellMinWidth;
- if(item2.colspan > 1) return;
- if(item2.width === 0){
- item2.width = Math.floor(autoWidth >= minWidth ? autoWidth : minWidth);
- }
- });
- });
-
- var th=that.getParentDivHeight(options.id);
- that.fullHeightGap=0;
- if(options.height){
- if(/^full-\d+$/.test(options.height)){
- that.fullHeightGap = options.height.split('-')[1];
- }
- }
- options.height = th - that.fullHeightGap;
- layui.each(options.cols, function(i1, item1){
- layui.each(item1, function(i2, item2){
- })
- });
- };
-
- Class.prototype.getParentDivHeight = function(tableId){
- var th=$("#"+tableId).parent().height();
- return th;
- };
-
- Class.prototype.reload = function(options){
- var that = this;
- if(that.config.data && that.config.data.constructor === Array) delete that.config.data;
- that.config = $.extend({}, that.config, options);
-
- that.render();
- };
-
- Class.prototype.page = 1;
-
- Class.prototype.restNumbers=function(){
- var that = this
- ,options = that.config;
- var trs=that.layBody.find('table tbody tr');
- var i=0;
- trs.each(function (o) {
- $(this).attr("data-index",i);
- $(this).find(".laytable-cell-numbers p").text(i+1);
- $(this).data('index',i);
- i++;
- });
- }
-
- Class.prototype.resetData=function(list) {
- var that = this
- ,options = that.config;
- var datas=[];
- var treeList=[];
- var tableList=[];
- var map={};
- datas.push(tableList);
- datas.push(treeList)
- datas.push(map)
- if(list==null||list.length<=0)return datas;
-
- for (var i = 0; i < list.length; i++) {
- var n = list[i];
- if(options.isTree){
- if(!n.hasOwnProperty("is_open")){
- n.is_open=true;
- }
- if(!n.hasOwnProperty("is_show")){
- n.is_show=true;
- }
- }
- }
- if(options.isTree){
- var field_Id=options[TREE_ID];
- var field_upId=options[TREE_UPID];
-
- var fa = function(upId) {
- var _array = [];
- for (var i = 0; i < list.length; i++) {
- var n = list[i];
- if (n[field_upId] === upId) {
- n.children = fa(n[field_Id]);
- _array.push(n);
- }
- }
- return _array;
- }
- treeList=fa(list[0][field_upId],"");
-
- var fa2=function (l,level,upids) {
- for (var i = 0; i < l.length; i++) {
- var n = l[i];
- n.level=level;
- n.upIds=upids;
- tableList.push(n);
- if (n.children&&n.children.length>0) {
- fa2(n.children,1+level,upids+"_"+n[field_Id]+"_");
- }
- }
- return;
- }
- fa2(treeList,1,"");
- }
- else{
- tableList=list;
- }
-
- tableList.forEach(function (o) {
- map[o[field_Id]]=o;
- });
-
- table.setDataList(that.config.id,tableList);
- table.setDataTreeList(that.config.id,treeList);
- table.setDataMap(that.config.id,map);
- return datas;
- }
-
- Class.prototype.treeFindDataById=function(u_Id) {
- var that = this
- ,options = that.config;
- var e=null;
- var list=table.getDataList(that.key);
- var key=options[TREE_ID];
- list.forEach(function (o) {
- if(o[key]==u_Id){
- e=o;
- return;
- }
- });
- return e;
- }
-
- Class.prototype.treeFindUpData=function(o){
- var uOjb=null;
- var that = this
- ,options = that.config;
-
- var key=options[TREE_UPID];
- var mapData=table.getDataMap(that.config.id);
- uOjb=mapData[o[key]];
- return uOjb;
- }
-
- Class.prototype.treeFindSonData=function (data) {
- var objs=[];
- function f(o) {
- if(o.children.length>0){
- o.children.forEach(function (i) {
- objs.push(i);
- if(i.children.length>0){
- f(i);
- }
- });
- }
- }f(data);
- return objs;
- };
-
- Class.prototype.treeConvertShowName=function (o) {
- var that = this
- ,options = that.config;
- var isTreeNode=(o.children&&o.children.length>0);
- var temhtml='<div style="float: left;height: 28px;line-height: 28px;padding-left: '+
- function () {
- if(isTreeNode){
- return '5px'
- }else{
- return '21px'
- }
- }()
- +'">'
- +function () {
- var nbspHtml="<i>"
- for(var i=1;i<o.level;i++) {
- nbspHtml = nbspHtml + " ";
- }
- nbspHtml=nbspHtml+"</i>";
- return nbspHtml;
- }()
- +function () {
- var temTreeHtml='';
- if(isTreeNode)temTreeHtml='<i class="layui-icon layui-tree-head"></i> ';
- return temTreeHtml;
- }()
- +'</div>';
- return temhtml;
- };
-
- Class.prototype.treeNodeOpen=function (o,is_open) {
- var that = this
- ,options = that.config
- ,tr = that.layBody.find('tr[data-index="'+ o[table.config.indexName] +'"]');
- o.is_open=is_open;
-
- var fa = function(e) {
- if(e.children&&e.children.length>0){
- var temList=e.children;
- for (var i = 0; i < temList.length; i++) {
- var n = temList[i];
- if(o.is_open){
- if(e.is_open&&e.is_show){
- var temo=that.layBody.find('tr[data-index="'+ n[table.config.indexName] +'"]');
- temo.show();
- n.is_show=true;
- }
- }else{
- var temo=that.layBody.find('tr[data-index="'+ n[table.config.indexName] +'"]');
- temo.hide();
- n.is_show=false;
- }
- fa(n);
- }
- }
- }
- fa(o);
-
- var dbClickI=tr.find('.layui-tree-head');
- if(o.is_open){
- dbClickI.html('');
- }else{
- dbClickI.html('');
- }
- };
-
- Class.prototype.pullData = function(curr, loadIndex){
- var that = this
- ,options = that.config
- ,request = options.request
- ,response = options.response
- ,sort = function(){
- if(typeof options.initSort === 'object'){
- that.sort(options.initSort.field, options.initSort.type);
- }
- };
- that.startTime = new Date().getTime();
- if(options.url){
- var params = {};
- params[request.pageName] = curr;
- params[request.limitName] = options.limit;
-
- var list=that.layFilter.find("[name^='filter_']");
- layui.each(list,function (i, o) {
- params[o.name]=$(o).val();
- });
- $.ajax({
- type: options.method || 'get'
- ,url: options.url
- ,data: $.extend(params, options.where)
- ,dataType: 'json'
- ,success: function(res){
- if(res.data.length){
- let arr = [],obj = res.data;
- (function objArr(obj,id) {
- for (let i in obj) {
- obj[i].myid = id+i;
- obj[i].myPid = id;
- if (obj[i].childMenus != null) {
- objArr(obj[i].childMenus,id+i);
- obj[i].childMenus = null;
- arr.push(obj[i]);
- } else {
- arr.push(obj[i]);
- }
- }
- })(obj,'#');
-
- res.data = arr.reverse();
- console.log(res.data);
- res.is = true;
- res.count = res.data.length;
- }else{
- res.data[0] = {name:"无数据",myid:'0',myPid:'0',id:'0'};
- }
- that.resetData(res.data);
- res.data=table.getDataList(options.id);
- if(res[response.statusName] != response.statusCode){
- that.renderForm();
- that.layMain.html('<div class="'+ NONE +'">'+ (res[response.msgName] || '返回的数据状态异常') +'</div>');
- } else {
- that.renderData(res, curr, res[response.countName]), sort();
- options.time = (new Date().getTime() - that.startTime) + ' ms';
- }
- loadIndex && layer.close(loadIndex);
- typeof options.done === 'function' && options.done(res, curr, res[response.countName]);
- }
- ,error: function(e, m){
- that.layMain.html('<div class="'+ NONE +'">数据接口请求异常</div>');
- that.renderForm();
- loadIndex && layer.close(loadIndex);
- }
- });
- } else if(options.data && options.data.constructor === Array){
- var res = {},startLimit = curr*options.limit - options.limit
- res[response.dataName] = options.data.concat().splice(startLimit, options.limit);
- res[response.countName] = options.data.length;
- that.renderData(res, curr, options.data.length), sort();
- typeof options.done === 'function' && options.done(res, curr, res[response.countName]);
- }
- };
-
- Class.prototype.eachCols = function(callback){
- var cols = $.extend(true, [], this.config.cols)
- ,arrs = [], index = 0;
-
- layui.each(cols, function(i1, item1){
- layui.each(item1, function(i2, item2){
-
- if(item2.colspan > 1){
- var childIndex = 0;
- index++
- item2.CHILD_COLS = [];
- layui.each(cols[i1 + 1], function(i22, item22){
- if(item22.PARENT_COL || childIndex == item2.colspan) return;
- item22.PARENT_COL = index;
- item2.CHILD_COLS.push(item22);
- childIndex = childIndex + (item22.colspan > 1 ? item22.colspan : 1);
- });
- }
- if(item2.PARENT_COL) return;
- arrs.push(item2)
- });
- });
-
- var eachArrs = function(obj){
- layui.each(obj || arrs, function(i, item){
- if(item.CHILD_COLS) return eachArrs(item.CHILD_COLS);
- callback(i, item);
- });
- };
- eachArrs();
- };
-
- Class.prototype.renderTreeConvertShowName = function(o){
- var that = this
- ,options = that.config
- ,m=options.elem
- ,hasRender = m.next('.' + ELEM_VIEW);
- var temhtml=that.treeConvertShowName(o);
- var temdiv=that.layBody.find('tr[data-index="'+ o[table.config.indexName] +'"]').find('td[data-field='+options[TREE_SHOW_NAME]+']').find('.layui-table-cell');
- $(temdiv).find('div').remove();
- $(temdiv).prepend(temhtml);
- }
-
- Class.prototype.renderTdCss = function(){
- var that = this
- ,options = that.config
- ,m=options.elem
- ,hasRender = m.next('.' + ELEM_VIEW);
- var id=that.index+'_dltable_td_style';
- hasRender.find("#"+id).remove();
- var styel='<style id="'+id+'">'
- +function () {
- var ret="";
- layui.each(that.config.cols,function (i1, item1) {
- layui.each(item1, function(i2, item2){
- ret+='.laytable-cell-'+that.index+'-'+(item2.field||i2)+'{' +
- 'width:'+(item2.width?item2.width+"px":"0px")
- +'}';
- });
- });
- return ret;
- }()+'</style>';
- hasRender.append(styel);
- }
-
- Class.prototype.renderTrUpids=function (obj) {
- var that = this
- ,options = that.config;
- var tree_upid_key=options[TREE_UPID];
- var upids=' upids="'+obj["upIds"]+'" ';
- var u_id=' u_id="'+obj[tree_upid_key]+'" '
- var ret=options.isTree?u_id:'';
- return ret;
- }
-
- Class.prototype.renderTd=function (param) {
- var that = this
- ,options = that.config;
- var cols=param.cols;
- var obj=param.obj;
- var numbers=param.numbers;
- var i3=param.i3;
- var field = cols.field || i3, content = obj[field]||''
- ,cell = that.getColElem(that.layHeader, field);
- var treeImgHtml='';
- if(options.isTree){
- if(options.treeShowName==cols.field){
- treeImgHtml=that.treeConvertShowName(obj);
- }
- }
-
- var td = ['<td data-field="'+ field +'" '+ function(){
- var attr = [];
- if(cols.edit) attr.push('data-edit="'+ cols.edit +'"');
- if(cols.align) attr.push('align="'+ cols.align +'"');
- if(cols.templet) attr.push('data-content="'+ content +'"');
- if(cols.toolbar) attr.push('data-off="true"');
- if(cols.event) attr.push('lay-event="'+ cols.event +'"');
- if(cols.style) attr.push('style="'+ cols.style +'"');
- if(cols.minWidth) attr.push('data-minwidth="'+ cols.minWidth +'"');
- return attr.join(' ');
- }() +'>'
- ,'<div class="layui-table-cell laytable-cell-'+ function(){
- var str = (options.index + '-' + field);
- return cols.type === 'normal' ? str
- : (str + ' laytable-cell-' + cols.type);
- }() +'">'+treeImgHtml+'<p style="width: auto;height: 100%;">'+ function(){
- var tplData = $.extend(true, {
- LAY_INDEX: numbers
- }, obj);
-
- if(cols.type === 'checkbox'){
- return '<input type="checkbox" name="layTableCheckbox" value="'+tplData[table.config.indexName]+'" lay-skin="primary" '+ function(){
- var checkName = table.config.checkName;
-
- if(cols[checkName]){
- obj[checkName] = cols[checkName];
- return cols[checkName] ? 'checked' : '';
- }
- return tplData[checkName] ? 'checked' : '';
- }() +'>';
- } else if(cols.type === 'numbers'){
- return numbers;
- }else if(cols.type === 'drop'){
- var rowsField=dl.ui.table.drop.findFieldObj(options.cols[0],field);
- if(rowsField&&rowsField['drop']){
- var o=dl.cache.code.get(rowsField.drop);
- return dl.ui.table.drop.findDropLable(rowsField.drop,content);
- }
- }else if(cols.type === 'radio'){
- return '<input type="radio" name="'+TABLE_RADIO_ID+options.id+'" value="'+tplData[table.config.indexName]+'" checked="">';
- }
-
- if(cols.toolbar){
- return laytpl($(cols.toolbar).html()||'').render(tplData);
- }
- return cols.templet ? function(){
- return typeof cols.templet === 'function'
- ? cols.templet(tplData)
- : laytpl($(cols.templet).html() || String(content)).render(tplData)
- }() : content;
- }()
- ,'</p></div></td>'].join('');
- return td;
- }
-
- Class.prototype.renderTr=function (obj,numbers) {
- var that = this
- ,options = that.config;
- var tds= [];
- that.eachCols(function(i3, cols){
- var field = cols.field || i3, content = obj[field]
- ,cell = that.getColElem(that.layHeader, field);
- if(content === undefined || content === null) content = '';
- if(cols.colspan > 1) return;
-
- var td = that.renderTd({
- 'obj':obj,'numbers':numbers,'cols':cols,'i3':i3
- });
- tds.push(td);
-
-
- });
- return tds;
- };
-
- Class.prototype.renderData = function(res, curr, count, sort){
- var that = this
- ,options = that.config
- ,data = res[options.response.dataName] || []
- ,trs = []
- ,trs_fixed = []
- ,trs_fixed_r = []
-
- ,render = function(){
- if(!sort && that.sortKey){
- return that.sort(that.sortKey.field, that.sortKey.sort, true);
- }
- layui.each(data, function(i1, obj){
- var tds = [], tds_fixed = [], tds_fixed_r = []
- ,numbers = i1 + options.limit*(curr - 1) + 1;
- if(obj.length === 0) return;
- if(!sort){
- obj[table.config.indexName] = i1;
- }
- tds=that.renderTr(obj,numbers);
- trs.push('<tr data-index="'+ i1 +'" '+that.renderTrUpids(obj)+'>'+ tds.join('') + '</tr>');
- trs_fixed.push('<tr data-index="'+ i1 +'">'+ tds_fixed.join('') + '</tr>');
- trs_fixed_r.push('<tr data-index="'+ i1 +'">'+ tds_fixed_r.join('') + '</tr>');
- });
-
- that.layBody.scrollTop(0);
- that.layMain.find('.'+ NONE).remove();
- that.layMain.find('tbody').html(trs.join(''));
- that.layFixLeft.find('tbody').html(trs_fixed.join(''));
- that.layFixRight.find('tbody').html(trs_fixed_r.join(''));
- that.renderForm();
- that.syncCheckAll();
- that.haveInit ? that.scrollPatch() : setTimeout(function(){
- that.scrollPatch();
- }, 50);
- that.haveInit = true;
- layer.close(that.tipsIndex);
- };
- that.key = options.id || options.index;
-
- table.setDataList(that.key,data);
-
- that.layPage[data.length === 0 && curr == 1 ? 'addClass' : 'removeClass'](HIDE);
-
- if(sort){
- return render();
- }
- if(data.length === 0){
- that.renderForm();
- that.layFixed.remove();
- that.layMain.find('tbody').html('');
- that.layMain.find('.'+ NONE).remove();
- return that.layMain.append('<div class="'+ NONE +'">'+ options.text.none +'</div>');
- }
- render();
- that.renderPage(count);
-
- table.pushClassIds(options.id,true);
- layui.each(options.cols, function(i1, item1){
- layui.each(item1, function(i2, item2){
- })
- });
- };
-
- Class.prototype.renderPage=function (count) {
- var that = this
- ,options = that.config;
-
- if(options.page){
- options.page = $.extend({
- elem: 'layui-table-page' + options.index
- ,count: count
- ,limit: options.limit
- ,limits: options.limits || [10,15,20,30,40,50,60,70,80,90]
- ,groups: 3
- ,layout: ['prev', 'page', 'next', 'skip', 'count', 'limit']
- ,prev: '<i class="layui-icon"></i>'
- ,next: '<i class="layui-icon"></i>'
- ,jump: function(obj, first){
- if(!first){
-
-
- that.page = obj.curr;
- options.limit = obj.limit;
- that.pullData(obj.curr, that.loading());
- }
- }
- }, options.page);
- options.page.count = count;
- laypage.render(options.page);
- }
- };
-
- Class.prototype.renderFilter = function(){
- var that = this
- ,options = that.config
- ,VIEW_CLASS=ELEM_VIEW
- ,index=that.index;
- var v = [];
- v.push('<form method="post" id="'+options.id+'_filter_form">');
- v.push('<table cellspacing="0" cellpadding="0" border="0" class="layui-table"><thead><tr>');
- layui.each(options.cols,function (i, o) {
- layui.each(o, function(i2, item2){
- var field=item2.field||i2;
- var minW=item2.minWidth?"data-minwidth='"+item2.minWidth+"'":"";
- var rowCols=item2.colspan?'colspan="'+item2.colspan+'"':'';
- var rowspan=item2.rowspan?'rowspan="'+item2.rowspan+'"':'';
- var unresize=item2.unresize?'data-unresize="true"':'';
- v.push('<th data-field="'+field+'"'+minW+rowCols+rowspan +unresize+'>');
- v.push('<div class="layui-table-cell laytable-cell-'+function () {
- var tem="";
- if (item2.colspan > 1) {
- tem='group';
- }else{
- tem=index+"-"+field;
- if(item2.type !== "normal"){
- tem+=" laytable-cell-"+item2.type;
- }
- }
- return tem;
- }()+'">');
- if(!item2.isFilter||!item2.field){
- v.push('');
- }else{
- v.push('<input class="layui-input '+ ELEM_EDIT +'" id="filter_'+item2.field+'" name="filter_'+item2.field+'">');
- }
- v.push('</div></th>');
- });
- });
- v.push('</tr></thead></table>');
- v.push('</form>');
- return v.join('');
- };
-
- Class.prototype.getColElem = function(parent, field){
- var that = this
- ,options = that.config;
- return parent.eq(0).find('.laytable-cell-'+ (options.index + '-' + field) + ':eq(0)');
- };
-
- Class.prototype.renderForm = function(type){
- form.render(type, 'LAY-table-'+ this.index);
- }
-
- Class.prototype.sort = function(th, type, pull, formEvent){
- var that = this
- ,field
- ,res = {}
- ,options = that.config
- ,filter = options.elem.attr('lay-filter')
- ,data = table.getDataList(that.key), thisData;
-
- if(typeof th === 'string'){
- that.layHeader.find('th').each(function(i, item){
- var othis = $(this)
- ,_field = othis.data('field');
- if(_field === th){
- th = othis;
- field = _field;
- return false;
- }
- });
- }
- try {
- var field = field || th.data('field');
-
- if(that.sortKey && !pull){
- if(field === that.sortKey.field && type === that.sortKey.sort){
- return;
- }
- }
- var elemSort = that.layHeader.find('th .laytable-cell-'+ options.index +'-'+ field).find(ELEM_SORT);
- that.layHeader.find('th').find(ELEM_SORT).removeAttr('lay-sort');
- elemSort.attr('lay-sort', type || null);
- that.layFixed.find('th')
- } catch(e){
- return hint.error('Table modules: Did not match to field');
- }
-
- that.sortKey = {
- field: field
- ,sort: type
- };
- if(type === 'asc'){
- thisData = layui.sort(data, field);
- } else if(type === 'desc'){
- thisData = layui.sort(data, field, true);
- } else {
- thisData = layui.sort(data, table.config.indexName);
- delete that.sortKey;
- }
- res[options.response.dataName] = thisData;
- that.renderData(res, that.page, that.count, true);
- if(formEvent){
- layui.event.call(th, MOD_NAME, 'sort('+ filter +')', {
- field: field
- ,type: type
- });
- }
- };
-
- Class.prototype.loading = function(){
- var that = this
- ,options = that.config;
- if(options.loading && options.url){
- return layer.msg('数据请求中', {
- icon: 16
- ,offset: [
- that.elem.offset().top + that.elem.height()/2 - 35 - _WIN.scrollTop() + 'px'
- ,that.elem.offset().left + that.elem.width()/2 - 90 - _WIN.scrollLeft() + 'px'
- ]
- ,time: -1
- ,anim: -1
- ,fixed: false
- });
- }
- };
-
- Class.prototype.setCheckData = function(index, checked){
- var that = this
- ,options = that.config
- ,thisData = table.getDataList(that.key);
- if(!thisData[index]) return;
- if(thisData[index].constructor === Array) return;
- thisData[index][options.checkName] = checked;
- };
-
- Class.prototype.syncCheckAll = function(){
- var that = this
- ,options = that.config
- ,checkAllElem = that.layHeader.find('input[name="layTableCheckbox"]')
- ,syncColsCheck = function(checked){
- that.eachCols(function(i, item){
- if(item.type === 'checkbox'){
- item[options.checkName] = checked;
- }
- });
- return checked;
- };
- if(!checkAllElem[0]) return;
- if(table.checkStatus(that.key).isAll){
- if(!checkAllElem[0].checked){
- checkAllElem.prop('checked', true);
- that.renderForm('checkbox');
- }
- syncColsCheck(true);
- } else {
- if(checkAllElem[0].checked){
- checkAllElem.prop('checked', false);
- that.renderForm('checkbox');
- }
- syncColsCheck(false);
- }
- };
-
- Class.prototype.getCssRule = function(field, callback){
- var that = this
- ,style = that.elem.find('style')[0]
- ,sheet = style.sheet || style.styleSheet || {}
- ,rules = sheet.cssRules || sheet.rules;
- layui.each(rules, function(i, item){
- if(item.selectorText === ('.laytable-cell-'+ that.index +'-'+ field)){
- return callback(item), true;
- }
- });
- };
-
- Class.prototype.resize = function(){
- var that = this;
-
-
-
- that.setArea();
- that.fullSize();
-
- that.resizeWidth();
- that.scrollPatch();
- };
-
- Class.prototype.resizeWidth = function(){
- var that = this;
- that.renderTdCss();
- };
-
- Class.prototype.fullSize = function(){
- var that = this
- ,options = that.config
- ,height = options.height, bodyHeight;
- height = that.getParentDivHeight(options.id) - that.fullHeightGap;
- height = '100%';
- that.elem.css('height', height);
-
-
- var theader=options.isFilter?76:38;
- bodyHeight = parseFloat(height) - theader - 1;
- if(options.toolbar){
- bodyHeight = bodyHeight - that.layTool.outerHeight();
- }
- if(options.page){
- bodyHeight = bodyHeight - that.layPage.outerHeight() - 1;
- }
-
-
- };
-
- Class.prototype.getScrollWidth = function(elem){
- var width = 0;
- if(elem){
- width = elem.offsetWidth - elem.clientWidth;
- } else {
- elem = document.createElement('div');
- elem.style.width = '100px';
- elem.style.height = '100px';
- elem.style.overflowY = 'scroll';
- document.body.appendChild(elem);
- width = elem.offsetWidth - elem.clientWidth;
- document.body.removeChild(elem);
- }
- return width;
- };
-
- Class.prototype.scrollPatch = function(){
- var that = this
- ,layMainTable = that.layMain.children('table')
- ,scollWidth = that.layMain.width() - that.layMain.prop('clientWidth')
- ,scollHeight = that.layMain.height() - that.layMain.prop('clientHeight')
- ,getScrollWidth = that.getScrollWidth(that.layMain[0])
- ,outWidth = layMainTable.outerWidth() - that.layMain.width();
-
- if(that.autoColNums && outWidth < 5 && !that.scrollPatchWStatus){
- var th = that.layHeader.eq(0).find('thead th:last-child')
- ,field = th.data('field');
- that.getCssRule(field, function(item){
- var width = item.style.width || th.outerWidth();
- item.style.width = (parseFloat(width) - getScrollWidth - outWidth) + 'px';
-
- if(that.layMain.height() - that.layMain.prop('clientHeight') > 0){
- item.style.width = parseFloat(item.style.width) - 1 + 'px';
- }
- that.scrollPatchWStatus = true;
- });
- }
- if(scollWidth && scollHeight){
- if(that.elem.find('.layui-table-patch').length<=0){
- var patchElem = $('<th class="layui-table-patch"><div class="layui-table-cell"></div></th>');
- patchElem.find('div').css({
- width: scollWidth
- });
- that.layHeader.eq(0).find('thead tr').append(patchElem);
-
- }
- } else {
- that.layFilter.eq(0).find('.layui-table-patch').remove();
- that.layHeader.eq(0).find('.layui-table-patch').remove();
- }
-
- var mainHeight = that.layMain.height()
- ,fixHeight = mainHeight - scollHeight;
- that.layFixed.find(ELEM_BODY).css('height', layMainTable.height() > fixHeight ? fixHeight : 'auto');
-
- that.layFixRight[outWidth > 0 ? 'removeClass' : 'addClass'](HIDE);
-
- that.layFixRight.css('right', scollWidth - 1);
- };
-
- Class.prototype.events = function(){
- var that = this
- ,options = that.config
- ,_BODY = $('body')
- ,dict = {}
- ,th = that.layHeader.find('th')
- ,resizing
- ,ELEM_CELL = '.layui-table-cell'
- ,filter = options.elem.attr('lay-filter')
- ,layFilter=that.layFilter.find("[name^='filter_']")
- ;
-
- layFilter.on('keyup',function () {
- that.page=1;
- that.pullData(that.page, that.loading());
- });
-
- th.on('mousemove', function(e){
- var othis = $(this)
- ,oLeft = othis.offset().left
- ,pLeft = e.clientX - oLeft;
- if(othis.attr('colspan') > 1 || othis.data('unresize') || dict.resizeStart){
- return;
- }
- dict.allowResize = othis.width() - pLeft <= 10;
- _BODY.css('cursor', (dict.allowResize ? 'col-resize' : ''));
- }).on('mouseleave', function(){
- var othis = $(this);
- if(dict.resizeStart) return;
- _BODY.css('cursor', '');
- }).on('mousedown', function(e){
- var othis = $(this);
- if(dict.allowResize){
- var field = othis.data('field');
- e.preventDefault();
- dict.resizeStart = true;
- dict.offset = [e.clientX, e.clientY];
- that.getCssRule(field, function(item){
- var width = item.style.width || othis.outerWidth();
- dict.rule = item;
- dict.ruleWidth = parseFloat(width);
- dict.minWidth = othis.data('minwidth') || options.cellMinWidth;
- });
- }
- });
-
- _DOC.on('mousemove', function(e){
- if(dict.resizeStart){
- e.preventDefault();
- if(dict.rule){
- var setWidth = dict.ruleWidth + e.clientX - dict.offset[0];
- if(setWidth < dict.minWidth) setWidth = dict.minWidth;
- dict.rule.style.width = setWidth + 'px';
- layer.close(that.tipsIndex);
- }
- resizing = 1
- }
- }).on('mouseup', function(e){
- if(dict.resizeStart){
- dict = {};
- _BODY.css('cursor', '');
- that.scrollPatch();
- }
- if(resizing === 2){
- resizing = null;
- }
- });
-
- th.on('click', function(){
- var othis = $(this)
- ,elemSort = othis.find(ELEM_SORT)
- ,nowType = elemSort.attr('lay-sort')
- ,type;
- if(!elemSort[0] || resizing === 1) return resizing = 2;
- if(nowType === 'asc'){
- type = 'desc';
- } else if(nowType === 'desc'){
- type = null;
- } else {
- type = 'asc';
- }
- that.sort(othis, type, null, true);
- }).find(ELEM_SORT+' .layui-edge ').on('click', function(e){
- var othis = $(this)
- ,index = othis.index()
- ,field = othis.parents('th').eq(0).data('field')
- layui.stope(e);
- if(index === 0){
- that.sort(field, 'asc', null, true);
- } else {
- that.sort(field, 'desc', null, true);
- }
- });
-
- that.elem.on('click', 'i.layui-tree-head', function(){
- var othis = $(this)
- ,index = othis.parents('tr').eq(0).data('index')
- ,tr = that.layBody.find('tr[data-index="'+ index +'"]')
- ,options=that.config
- ,tree_id=options[TREE_ID]
- ,datas=table.getDataList(that.key);
- var o=datas[index];
- that.treeNodeOpen(o,!o.is_open);
- });
-
- that.elem.on('click', 'input[name="layTableCheckbox"]+', function(){
- var checkbox = $(this).prev()
- ,childs = that.layBody.find('input[name="layTableCheckbox"]')
- ,index = checkbox.parents('tr').eq(0).data('index')
- ,checked = checkbox[0].checked
- ,isAll = checkbox.attr('lay-filter') === 'layTableAllChoose';
-
- if(isAll){
- childs.each(function(i, item){
- item.checked = checked;
- that.setCheckData(i, checked);
- });
- that.syncCheckAll();
- that.renderForm('checkbox');
- } else {
- that.setCheckData(index, checked);
- that.syncCheckAll();
- }
- layui.event.call(this, MOD_NAME, 'checkbox('+ filter +')', {
- checked: checked
- ,data: table.getDataList(that.key) ? (table.getDataList(that.key)[index] || {}) : {}
- ,type: isAll ? 'all' : 'one'
- });
- });
-
- that.layBody.on('mouseenter', 'tr', function(){
- var othis = $(this)
- ,index = othis.index();
- that.layBody.find('tr:eq('+ index +')').addClass(ELEM_HOVER)
- })
- that.layBody.on('mouseleave', 'tr', function(){
- var othis = $(this)
- ,index = othis.index();
- that.layBody.find('tr:eq('+ index +')').removeClass(ELEM_HOVER)
- });
- that.layBody.on('change', '.'+ELEM_EDIT, function(){
- var othis = $(this)
- ,value = this.value
- ,field = othis.parent().data('field')
- ,index = othis.parents('tr').eq(0).data('index')
- ,data = table.getDataList(that.key)[index];
- data[field] = value;
- layui.event.call(this, MOD_NAME, 'edit('+ filter +')', {
- value: value
- ,data: data
- ,field: field
- });
- });
- that.layBody.on('blur', '.'+ELEM_EDIT, function(){
- var templet
- ,othis = $(this)
- ,field = othis.parent().data('field')
- ,index = othis.parents('tr').eq(0).data('index')
- ,editType = othis.parent().data('edit')
- ,data = table.getDataList(that.key)[index];
- var options = that.config;
- that.eachCols(function(i, item){
- if(item.field == field && item.templet){
- templet = item.templet;
- }
- });
- var value="";
- if(editType === 'select') {
- var rowsField=dl.ui.table.drop.findFieldObj(options.cols[0],field);
- if(rowsField&&rowsField['drop']){
- var o=dl.cache.code.get(rowsField.drop);
- value=dl.ui.table.drop.findDropLable(rowsField.drop,this.value);
- }
- othis.parent().find(ELEM_CELL+' p').html(
- templet ? laytpl($(templet).html() || value).render(data) : value
- );
- } else {
- othis.parent().find(ELEM_CELL+' p').html(
- templet ? laytpl($(templet).html() || this.value).render(data) : this.value
- );
- }
- othis.parent().data('content', this.value);
- othis.remove();
- });
-
- that.layBody.on('click', 'td div.layui-table-cell p', function(){
- var othis = $(this).parent().parent()
- ,field = othis.data('field')
- ,editType = othis.data('edit')
- ,index = othis.parents('tr').eq(0).data('index')
- ,data = table.getDataList(that.key)[index]
- ,elemCell = othis.children(ELEM_CELL);
- var options = that.config;
- layer.close(that.tipsIndex);
- if(othis.data('off')) return;
-
- if(editType){
- if(editType === 'select') {
- var dropName=othis.data('drop');
- var rowsField=dl.ui.table.drop.findFieldObj(options.cols[0],field);
- var o=dl.cache.code.get(rowsField.drop);
- var html='';
- var scv=o.syscodevaluecache;
- for(var i in scv){
- var isSelected="";
- if(scv[i].scv_value==data[field]){
- isSelected="selected='selected'";
- }
-
- html+='<option '+isSelected+' value="'+scv[i].scv_value+'">'+scv[i].scv_show_name+'</option>'
- }
- var select = $('<select class="'+ ELEM_EDIT +'" lay-ignore>' +
- html+
- '</select>');
- othis.find('.'+ELEM_EDIT)[0] || othis.append(select);
- } else {
- var input = $('<input class="layui-input '+ ELEM_EDIT +'">');
- input[0].value = $.trim($(this).text());
- othis.find('.'+ELEM_EDIT)[0] || othis.append(input);
- input.focus();
- }
- return;
- }
-
- if(elemCell.find('.layui-form-switch,.layui-form-checkbox')[0]) return;
- if(Math.round(elemCell.prop('scrollWidth')) > Math.round(elemCell.outerWidth())){
- that.tipsIndex = layer.tips([
- '<div class="layui-table-tips-main" style="margin-top: -'+ (elemCell.height() + 16) +'px;'+ function(){
- if(options.size === 'sm'){
- return 'padding: 4px 15px; font-size: 12px;';
- }
- if(options.size === 'lg'){
- return 'padding: 14px 15px;';
- }
- return '';
- }() +'">'
- ,elemCell.html()
- ,'</div>'
- ,'<i class="layui-icon layui-table-tips-c">ဆ</i>'
- ].join(''), elemCell[0], {
- tips: [3, '']
- ,time: -1
- ,anim: -1
- ,maxWidth: (device.ios || device.android) ? 300 : 600
- ,isOutAnim: false
- ,skin: 'layui-table-tips'
- ,success: function(layero, index){
- layero.find('.layui-table-tips-c').on('click', function(){
- layer.close(index);
- });
- }
- });
- }
- });
-
- that.layBody.on('click', '*[lay-event]', function(){
- var othis = $(this)
- ,index = othis.parents('tr').eq(0).data('index')
- ,tr = that.layBody.find('tr[data-index="'+ index +'"]')
- ,ELEM_CLICK = 'layui-table-click'
- ,list = table.getDataList(that.key)
- ,data = table.getDataList(that.key)[index];
- layui.event.call(this, MOD_NAME, 'tool('+ filter +')', {
- data: data
- ,event: othis.attr('lay-event')
- ,tr: tr
- ,del: function(){
- table.delRow(options.id,data);
- }
- ,update: function(fields){
- fields = fields || {};
- layui.each(fields, function(key, value){
- if(key in data){
- var templet, td = tr.children('td[data-field="'+ key +'"]');
- data[key] = value;
- that.eachCols(function(i, item2){
- if(item2.field == key && item2.templet){
- templet = item2.templet;
- }
- });
- td.children(ELEM_CELL).html(
- templet ? laytpl($(templet).html() || value).render(data) : value
- );
- td.data('content', value);
- }
- });
- }
- });
- tr.addClass(ELEM_CLICK).siblings('tr').removeClass(ELEM_CLICK);
- });
-
- that.layMain.on('scroll', function(){
- var othis = $(this)
- ,scrollLeft = othis.scrollLeft()
- ,scrollTop = othis.scrollTop();
- that.layHeader.scrollLeft(scrollLeft);
- that.layFixed.find(ELEM_BODY).scrollTop(scrollTop);
- layer.close(that.tipsIndex);
- });
- _WIN.on('resize', function(){
- that.resize();
- });
- };
-
- thisTable.config = {};
-
- table.init();
-
- exports(MOD_NAME, table);
- });
|