import {formatDate, formatDateTime, copyObj, inArray, formatTime} from '../util.js'; import selectpage from '../components/SelectPage.js'; import tableTemp from './template/TableTemp.js'; const setValue=function(obj, path, value){ const keys = path.split('.'); if (keys.length === 1) { obj[path] = value; return; } const currentKey = keys[0]; if (!obj.hasOwnProperty(currentKey)) { obj[currentKey] = {}; } const nextObj = obj[currentKey]; const nextPath = keys.slice(1).join('.'); setValue(nextObj, nextPath, value); } const getValue=function(row,field){ let fieldarr=field.split('.'); for(let i=0;i'){ operate.filter='<>'; operate.type='text'; column.operate=operate; } if(column.operate=='null' || column.operate=='NULL'){ operate.filter='IS NULL'; operate.form='hidden'; column.operate=operate; } if(column.operate=='not null' || column.operate=='NOT NULL'){ operate.filter='IS NOT NULL'; operate.form='hidden'; column.operate=operate; } if(column.operate=='like' || column.operate=='LIKE'){ operate.filter='LIKE'; operate.type='text'; column.operate=operate; } if(column.operate=='not like' || column.operate=='NOT LIKE'){ operate.filter='NOT LIKE'; operate.type='text'; column.operate=operate; } if(column.operate=='select' || column.operate=='SELECT'){ operate.filter='='; operate.form='select'; column.operate=operate; } if(column.operate=='selects' || column.operate=='SELECTS'){ operate.filter='in'; operate.form='select'; operate.multiple=true; operate.value=[]; column.operate=operate; } if(column.operate=='checkbox' || column.operate=='CHECKBOX'){ operate.filter='in'; operate.form='checkbox'; operate.value=[]; column.operate=operate; } if(column.operate=='radio' || column.operate=='RADIO'){ operate.filter='='; operate.form='radio'; column.operate=operate; } if(column.operate=='find_in_set' || column.operate=='FIND_IN_SET'){ operate.filter='FIND_IN_SET'; operate.form='select'; column.operate=operate; } if(column.operate=='between' || column.operate=='BETWEEN'){ operate.filter='between'; operate.form='between'; operate.value=[]; column.operate=operate; } if(column.operate=='not between' || column.operate=='NOT BETWEEN'){ operate.filter='not between'; operate.form='between'; operate.value=[]; column.operate=operate; } if(column.operate=='date' || column.operate=='DATE'){ operate.filter='='; operate.form='date-picker'; operate.type='date'; column.operate=operate; } if(column.operate=='datetime' || column.operate=='DATETIME'){ operate.filter='='; operate.form='date-picker'; operate.type='datetime'; column.operate=operate; } if(column.operate=='daterange' || column.operate=='DATERANGE'){ operate.filter='between time'; operate.form='date-picker'; operate.type='daterange'; column.operate=operate; } if(column.operate=='time' || column.operate=='TIME'){ operate.form='time-picker'; operate.filter='='; operate.type='time'; column.operate=operate; } if(column.operate=='timerange' || column.operate=='TIMERANGE'){ operate.form='time-picker'; operate.type='timerange'; operate.filter='between'; column.operate=operate; } if(column.operate=='area' || column.operate=='AREA'){ operate.form='cascader'; operate.url='ajax/area'; operate.level=3; column.operate=operate; } if(column.operate=='category' || column.operate=='CATEGORY'){ operate.form='cascader'; operate.url='ajax/category'; operate.level=2; column.operate=operate; } /**简写end**/ if(typeof column.operate=='object'){ for(let k in column.operate){ if(typeof column.operate[k]=='string'){ column.operate[k]=column.operate[k].toLowerCase(); } } if(column.operate.form=='selectpage'){ operate.filter='='; } if(column.operate.form=='cascader'){ column.operate.props=Object.assign({expandTrigger:'hover',multiple:false,children:'childlist',value:'id',label:'name',lazy:false},column.operate.props); if(column.operate.url && column.operate.level){ column.operate.props.lazy=true; column.operate.props.expandTrigger='click'; let url=column.operate.url; column.operate.props.lazyLoad=function(node,resolve){ let pid=0; let level=column.operate.level; if(node.level){ pid=node.value; } Yunqi.ajax.get(url,{pid:pid}).then(res=>{ if(res instanceof Array){ res.map(t=>{ if(node.level>=level-1){ t.leaf=true; } return t; }); resolve(res); }else{ resolve([]); } }); }; delete column.operate.options; } operate.filter='='; } if(column.operate.form=='date-picker'){ if(column.operate.type=='date'){ operate.format='YYYY-MM-DD'; operate.filter='='; } if(column.operate.type=='year'){ operate.format='YYYY年'; operate.filter='='; } if(column.operate.type=='month'){ operate.format='YYYY年MM月'; operate.filter='='; } if(column.operate.type=='dates'){ operate.format='YYYY-MM-DD'; operate.filter='='; } if(column.operate.type=='datetime'){ operate.format='YYYY-MM-DD HH:mm:ss'; operate.filter='='; } if(column.operate.type=='daterange') { operate.format = 'YYYY-MM-DD'; operate.filter='between time'; operate.shortcuts = [ { text: '今天', value: function () { const start = new Date(); return [start, start]; } }, { text: '昨天', value: function () { const start = new Date(); start.setTime(start.getTime() - 3600 * 1000 * 24 * 1); return [start, start]; } }, { text: '最近7天', value: function () { const end = new Date(); const start = new Date(); start.setTime(start.getTime() - 3600 * 1000 * 24 * 6); return [start, end]; } }, { text: '最近30天', value: function () { const end = new Date(); const start = new Date(); start.setTime(start.getTime() - 3600 * 1000 * 24 * 29); return [start, end]; } }, { text: '本月', value: function () { const end = new Date(); const start = new Date(formatDate(end).slice(0, 7) + '-01'); return [start, end]; } }, { text: '上月', value: function () { const currentDate = new Date(); let lastMonth = currentDate.getMonth() - 1; let year = currentDate.getFullYear(); if (lastMonth < 0) { year--; lastMonth = 11; } let firstDay = new Date(year, lastMonth, 1); let lastDay = new Date(year, lastMonth + 1, 0); return [firstDay, lastDay]; } }, { text: '今年', value: function () { let currentDate = new Date(); let year = currentDate.getFullYear(); let start = new Date(year, 0, 1); let end = new Date(year, 11, 31); return [start, end]; } }, { text: '去年', value: function () { let currentDate = new Date(); let year = currentDate.getFullYear() - 1; let start = new Date(year, 0, 1); let end = new Date(year, 11, 31); return [start, end]; } }, ]; } } //如果用户自定义了属性,则优先使用 for(let key in operate){ column.operate[key]=(column.operate[key]!==undefined)?column.operate[key]:operate[key]; } } //通过visible隐藏数据 if(column.visible!='none'){ column.visible=(column.visible===false)?false:true; } if(column.field=='operate' || column.checkbox || column.treeExpand){ if(column.field=='operate' && !column.direction){ column.direction='row'; } if(column.field=='operate' && !column.align){ column.align='center'; } if(column.field=='operate' && column.action){ for(let kss in column.action){ if(typeof column.action[kss]=='object' && typeof column.action[kss].method=='string'){ column.action[kss].method=Yunqi.app[column.action[kss].method]; } if(typeof column.action[kss]=='object' && !column.action[kss].visible){ column.action[kss].visible=function(){return true;}; } } } return column; } //默认格式 column.formatter=(column.field && (column.formatter===undefined || column.formatter===false))?Yunqi.formatter.text:column.formatter; //初始化tabs if(this.tabs && column.field==this.tabs){ this.tabList=column.searchList || {}; } return column; }, dataList:function(){ this.loading=true; //设置排序规则 let sort=this.sortName; let order=this.order; if(this.sortData && this.sortData.order.startsWith('desc')){ order='desc'; sort=this.sortData.prop; } if(this.sortData && this.sortData.order.startsWith('asc')){ order='asc'; sort=this.sortData.prop; } //设置filter let filter=[]; if(this.tabs && this.tabsValue){ filter.push({ field:this.tabs, op:'=', value:this.tabsValue }); } for(let i=0;i{ res=formatDate(res); return res; }); break; case 'year': value=this.table_.columns[i].operate.value.getFullYear(); break; case 'month': value=formatDate(this.table_.columns[i].operate.value).slice(0,7); break; case 'daterange': let begin1=formatDate(this.table_.columns[i].operate.value[0])+' 00:00:00'; let end1=formatDate(this.table_.columns[i].operate.value[1])+' 23:59:59'; value=[begin1,end1]; break; } } if(this.table_.columns[i].operate.form=='time-picker'){ let timeValue=this.table_.columns[i].operate.value; if(timeValue instanceof Array){ value=[formatTime(timeValue[0]),formatTime(timeValue[1])]; }else{ value=formatTime(timeValue); } } filter.push({ field:this.table_.columns[i].operate.name?this.table_.columns[i].operate.name:this.table_.columns[i].field, op:this.table_.columns[i].operate.filter?this.table_.columns[i].operate.filter:false, value:value }); } } let json={ sort:sort, order:order, filter:[] }; //下载 if(this.download.show){ let field=[]; let searchList={}; json.listAction=this.extend.index_url; this.table_.columns.forEach(res=>{ if(inArray(this.download.field,res.field)){ field.push({ field:res.field, title:res.title, }); if(res.searchList){ searchList[res.field]=res.searchList; } } }); json.field=field; json.searchList=searchList; json.isTree=this.isTree; if(this.download.page){ json.page=this.currentPage; json.limit=this.pageSize; }else{ json.page=1; json.limit=100000; } if(this.download.filter){ json.filter=filter; } Yunqi.ajax.json(this.extend.download_url,json,true,false).then(data=>{ let url=Yunqi.config.baseUrl+this.extend.download_url; if(url.indexOf('?')!=-1){ url+='&file='+data; }else{ url+='?file='+data; } location.href=url; this.loading=false; this.download.show=false; }).catch(error=>{ this.loading=false; }); }else{ json.searchValue=this.searchValue; json.search=this.search; json.filter=filter; json.page=this.currentPage; json.limit=this.pageSize; Yunqi.ajax.json(this.extend.index_url,json).then(data=>{ //this.allData = data this.$emit('data-loaded', data) this.total=data.total; this.render(data.rows); Vue.nextTick(()=>{ this.onRender(this.list); }); this.summary=data.summary; this.loading=false; }).catch(error=>{ this.loading=false; }); } }, render:function(list){ for(let j=0;j{ ts=(columns.searchList[ts]!==undefined)?columns.searchList[ts]:ts; return ts; }); } } if(formatter._name=='switch' && columns.searchList){ let xs=0,activeValue,inactiveValue; for(let k in columns.searchList){ if(k==='0')k=0; if(k==='1')k=1; if(k===0 || k===1){ activeValue=1; inactiveValue=0; }else if(k==='normal' || k==='hidden'){ activeValue='normal'; inactiveValue='hidden'; }else{ if(xs===0){ activeValue=k; } if(xs===1){ inactiveValue=k; } } if(value==columns.searchList[k]){ value=k; } xs++; } formatter.activeValue=activeValue; formatter.inactiveValue=inactiveValue; } formatter.value=value; } return formatter; }, rowDrop:function(){ const _this = this; const tbody = document.querySelector('.el-table__body-wrapper tbody'); class TreeClass{ constructor(index) { this.index = index; this.j = 0; this.arr=[]; } getItem(list) { for(let i=0;i0){ let item = this.getItem(list[i].childlist); if(item){ return item; } } } return false; } getList(list,pid){ for(let i=0;i0){ this.getList(list[i].childlist,pid); } } return this.arr; } }; Sortable.create(tbody, { // 指定父元素下可被拖拽的子元素 draggable: ".el-table__row", animation: 100, handle:'.sortableButton', ghostClass: "sortable-ghost", chosenClass: "sortable-chosen", dragClass: "sortable-drag", onEnd ({ newIndex, oldIndex }) { let data=[]; if(_this.isTree){ let new_item=(new TreeClass(newIndex)).getItem(_this.list); let old_item=(new TreeClass(oldIndex)).getItem(_this.list); if(new_item.weigh===undefined || old_item.weigh===undefined){ Yunqi.message.error(__('没有weigh属性,排序失败')); return; } if(new_item.pid!==old_item.pid){ Yunqi.message.error(__('只支持在同级别表内拖拽')); return; } //找到影响拖拽的其他行,并修改weigh属性 let list=(new TreeClass(oldIndex)).getList(_this.list,old_item.pid); let weigh=new_item.weigh; data.push({id:old_item.id,weigh:weigh}); //从上往下拖 if(newIndex>oldIndex){ for(let i=list.length-1;i>=0;i--){ if(list[i].id==old_item.id){ break; } if(list[i].weigholdIndex)?weigh++:weigh--; let i=0; while(ioldIndex)?_this.list[newIndex-i].id:_this.list[newIndex+i].id, weigh:(newIndex>oldIndex)?weigh++:weigh-- }); i++; } } const elloading=ElementPlus.ElLoading.service({text:'排序中..'}); const promise=data.map(res=>{ return new Promise((resolve, reject)=>{ Yunqi.ajax.post(_this.extend.multi_url,{ids:res.id,field:'weigh',value:res.weigh},false,false).then(res=>{ resolve(); }).catch(err=>{ reject(); }); }); }); Promise.all(promise).then(res=>{ elloading.close(); top.ElementPlus.ElMessage({ message: '排序完成', type: 'success' }); _this.reload(); }).catch(err=>{ elloading.close(); }); } }); }, tabChange:function (e){ this.tabsValue=e; this.reload(); }, reload:function(){ this.selections=[]; this.dataList(); }, blurSearch:function(){ this.dataList(); }, del:function(){ let ids=[]; this.selections.forEach(res=>{ ids.push(res[this.pk]); }); Yunqi.api.del(this.extend.del_url,ids,()=>{ this.dataList(); this.selections=[]; }); }, delOne:function(row){ Yunqi.api.del(this.extend.del_url,row[this.pk],()=>{ this.dataList(); this.selections=[]; }); }, selectOne:function(e){ this.selections=e; }, submit:function(){ this.currentPage=1; this.dataList(); }, selectAll:function(e){ this.selections=e; }, handleSizeChange:function(size){ this.pageSize=size; this.dataList(); }, handleCurrentChange:function(page){ this.currentPage=page; this.dataList(); }, changeSort:function(e){ if(!e.order){ this.sortData=''; }else{ this.sortData=e; } this.dataList(); }, changeVisiable:function(field){ let columns=this.table_.columns; for(let i=0;i{ ids.push(res[this.pk]); }); let options={ ids:ids, field:'status', value:status }; Yunqi.api.multi(this.extend.multi_url,options,()=>{ this.dataList(); this.selections=[]; }); }, changeSwitch:function(row,field){ let value=row._formatter[field].value; setValue(row,field,value); let options={ ids:row[this.pk], field:field, value:value }; Yunqi.api.multi(this.extend.multi_url,options,()=>{ this.render(this.list); }); }, changeSelect:function(row,field){ let value=row._formatter[field].value; setValue(row,field,value); let options={ ids:row[this.pk], field:field, value:value }; Yunqi.api.multi(this.extend.multi_url,options,()=>{ this.render(this.list); }); }, changeExpand:function () { this.mainFrameExpand=!this.mainFrameExpand; if(this.mainFrameExpand){ Yunqi.api.expand(); }else{ Yunqi.api.compress(); } }, changeSelectpage:function (r){ for(let i=0;i{ this.table_.searchFormVisible=!this.table_.searchFormVisible; },50); }, recyclebin:function (){ if(this.extend.recyclebin_url==undefined){ Yunqi.message.error(__('recyclebin_url未设置')); return; } let table={ width:'80%', height:690, title:__('回收站'), url:this.extend.recyclebin_url+'?action=list', icon:'fa fa-recycle', close:function (){ let id=top.Yunqi.app.activeTab.id; let tab=top.document.getElementById('addtabs-'+id).contentWindow; let doc=tab.document.getElementsByClassName('refresh'); if(doc.length>0){ doc[0].click(); } } }; Yunqi.api.open(table); }, add:function () { if(this.extend.add_url==undefined){ Yunqi.message.error(__('add_url未设置')); return; } let form={...this.addForm,title:__('添加'),url:this.extend.add_url,icon:'fa fa-plus'}; if(form.expand==undefined){ form.expand=false; } form.close=function (refresh=false){ if(!refresh){ return } let id=top.Yunqi.app.activeTab.id; let tab=top.document.getElementById('addtabs-'+id).contentWindow; let doc=tab.document.getElementsByClassName('refresh'); if(doc.length>0){ doc[0].click(); } }; Yunqi.api.open(form); }, edit:function (row) { if(this.extend.edit_url==undefined){ Yunqi.message.error(__('edit_url未设置')); return; } let form={...this.editForm,title:__('编辑'),icon:'fa fa-pencil-square-o'}; if(form.expand==undefined){ form.expand=false; } let ids=[]; if(row){ ids.push(row[this.pk]); }else{ this.selections.forEach(res=>{ ids.push(res[this.pk]); }); } let time=0; ids.forEach(res=>{ setTimeout(()=>{ form.url=(this.extend.edit_url.indexOf('?')!=-1)?this.extend.edit_url+'&ids='+res:this.extend.edit_url+'?ids='+res, form.close=function (refresh=false){ if(!refresh){ return; } let id=top.Yunqi.app.activeTab.id; let tab=top.document.getElementById('addtabs-'+id).contentWindow; let doc=tab.document.getElementsByClassName('refresh'); if(doc.length>0){ doc[0].click(); } }; Yunqi.api.open(form); },time); time+=100; }); }, previewImg(imgs){ Yunqi.api.previewImg(imgs); }, importExcel:function (){ document.querySelector('.importUpload button').click(); }, handleImportSuccess:function (e){ let file=e.data.url; if(!this.extend.import_url){ Yunqi.message.error('import_url未设置'); return; } Yunqi.ajax.post(this.extend.import_url,{file:file}).then(res=>{ this.importResult.success=res.success; this.importResult.fail=res.fail; this.importResult.show=true; this.reload(); }); } } };