Table.js 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. import {formatDate, formatDateTime, copyObj, inArray, formatTime} from '../util.js';
  2. import selectpage from '../components/SelectPage.js';
  3. import tableTemp from './template/TableTemp.js';
  4. const setValue=function(obj, path, value){
  5. const keys = path.split('.');
  6. if (keys.length === 1) {
  7. obj[path] = value;
  8. return;
  9. }
  10. const currentKey = keys[0];
  11. if (!obj.hasOwnProperty(currentKey)) {
  12. obj[currentKey] = {};
  13. }
  14. const nextObj = obj[currentKey];
  15. const nextPath = keys.slice(1).join('.');
  16. setValue(nextObj, nextPath, value);
  17. }
  18. const getValue=function(row,field){
  19. let fieldarr=field.split('.');
  20. for(let i=0;i<fieldarr.length;i++){
  21. row=row[fieldarr[i]];
  22. if(row==undefined){
  23. return row;
  24. }
  25. }
  26. return row;
  27. }
  28. export default {
  29. name: "Table",
  30. components:{'SelectPage':selectpage},
  31. data: function () {
  32. return {
  33. menutype:Yunqi.app.window.menutype,
  34. loading:true,
  35. sortData:'',
  36. expandRowKeys:[],
  37. /*需要reset的数据begin*/
  38. list:[],
  39. total:0,
  40. summary:'',
  41. currentPage:1,
  42. pageSize:10,
  43. selections:[],
  44. searchValue:'',
  45. /*需要reset的数据end*/
  46. table_:{
  47. columns:'',
  48. toolbar:[],
  49. searchFormVisible:true,
  50. },
  51. treeExpandAll_:false,
  52. mainFrameExpand:false,
  53. tabList:{},
  54. tabsValue:'',
  55. rightToolOption:{type:'font',list:{large:'大',default:'中',small:'小'}},
  56. pageFont:'default',
  57. download:{
  58. show:false,
  59. field:[],
  60. filter:1,
  61. page:0,
  62. },
  63. importResult:{
  64. show:false,
  65. success:0,
  66. fail:[]
  67. },
  68. allData:{}
  69. }
  70. },
  71. template:tableTemp,
  72. props:{
  73. columns:{
  74. type: Array,
  75. required: true
  76. },
  77. extend:{
  78. type:Object,
  79. default:{
  80. index_url: '',
  81. add_url: '',
  82. edit_url: '',
  83. del_url: '',
  84. multi_url: '',
  85. download_url:'',
  86. import_url:'',
  87. recyclebin_url:''
  88. }
  89. },
  90. searchFormVisible:{
  91. type:Boolean,
  92. default:true
  93. },
  94. commonSearch:{
  95. type:Boolean,
  96. default:true
  97. },
  98. search:{
  99. type:String,
  100. default:''
  101. },
  102. style:{
  103. type:Object,
  104. default: {width:'100%'}
  105. },
  106. pagination:{
  107. type:Boolean,
  108. default:true
  109. },
  110. toolbar:{
  111. type:String,
  112. default:'refresh,add,edit,del'
  113. },
  114. pk:{
  115. type:String,
  116. default:'id'
  117. },
  118. sortName:{
  119. type:String,
  120. default:'id'
  121. },
  122. order:{
  123. type:String,
  124. default:'desc'
  125. },
  126. tabs:{
  127. type:String,
  128. default:''
  129. },
  130. isTree:{
  131. type:Boolean,
  132. default:false
  133. },
  134. treeExpandAll:{
  135. type:Boolean,
  136. default:false
  137. },
  138. showSummary:{
  139. type:Boolean,
  140. default:false
  141. },
  142. multiHeader:{
  143. type:Boolean,
  144. default:false
  145. },
  146. auth:{
  147. type:Object,
  148. default:{
  149. edit: true,
  150. add: true,
  151. multi:true,
  152. del:true,
  153. import:true,
  154. download:true,
  155. recyclebin:true
  156. }
  157. },
  158. addForm:{
  159. type:Object,
  160. default:{
  161. icon:'fa fa-plus-square-o',
  162. title:__('添加'),
  163. expand:false,
  164. width:800,
  165. height:550
  166. }
  167. },
  168. editForm:{
  169. type:Object,
  170. default:{
  171. icon:'fa fa-pencil-square-o',
  172. title:__('编辑'),
  173. expand:false,
  174. width:800,
  175. height:550
  176. }
  177. },
  178. onRender:{
  179. type:Function,
  180. default:function(){}
  181. },
  182. totalArr:{
  183. type:Array,
  184. default:[]
  185. },
  186. isShowtotal:{
  187. type:Boolean,
  188. default:false
  189. },
  190. },
  191. mounted:function(){
  192. this.table_.searchFormVisible=this.searchFormVisible;
  193. this.table_.toolbar=this.toolbar.split(',');
  194. if(!this.pagination){
  195. this.pageSize=10000;
  196. }
  197. this.reset();
  198. this.rowDrop();
  199. },
  200. methods:{
  201. reset:function(){
  202. let columns=copyObj(this.columns);
  203. for(let i=0;i<columns.length;i++){
  204. if(this.isTree && i===1){
  205. columns[i].type='';
  206. }
  207. if(columns[i].children){
  208. columns[i].visible=true;
  209. for(let j=0;j<columns[i].children.length;j++){
  210. if(columns[i].children[j].children){
  211. columns[i].children[j].visible=true;
  212. for (let k=0;k<columns[i].children[j].children.length;k++){
  213. columns[i].children[j].children[k]=this.formatColumn(columns[i].children[j].children[k]);
  214. }
  215. }else{
  216. columns[i].children[j]=this.formatColumn(columns[i].children[j]);
  217. }
  218. }
  219. }else{
  220. columns[i]=this.formatColumn(columns[i]);
  221. }
  222. }
  223. this.table_.columns=columns;
  224. this.searchValue='';
  225. this.currentPage=1;
  226. this.list=[];
  227. this.total=0;
  228. this.selections=[];
  229. this.dataList();
  230. },
  231. formatColumn:function (column){
  232. let operate={form:'input',value:'',size:'default',placeholder:column.title};
  233. /**简写begin**/
  234. if((column.field && column.operate==undefined) || column.operate=='='){
  235. operate.filter='=';
  236. operate.type='text';
  237. column.operate=operate;
  238. }
  239. if(column.operate=='!=' || column.operate=='<>'){
  240. operate.filter='<>';
  241. operate.type='text';
  242. column.operate=operate;
  243. }
  244. if(column.operate=='null' || column.operate=='NULL'){
  245. operate.filter='IS NULL';
  246. operate.form='hidden';
  247. column.operate=operate;
  248. }
  249. if(column.operate=='not null' || column.operate=='NOT NULL'){
  250. operate.filter='IS NOT NULL';
  251. operate.form='hidden';
  252. column.operate=operate;
  253. }
  254. if(column.operate=='like' || column.operate=='LIKE'){
  255. operate.filter='LIKE';
  256. operate.type='text';
  257. column.operate=operate;
  258. }
  259. if(column.operate=='not like' || column.operate=='NOT LIKE'){
  260. operate.filter='NOT LIKE';
  261. operate.type='text';
  262. column.operate=operate;
  263. }
  264. if(column.operate=='select' || column.operate=='SELECT'){
  265. operate.filter='=';
  266. operate.form='select';
  267. column.operate=operate;
  268. }
  269. if(column.operate=='selects' || column.operate=='SELECTS'){
  270. operate.filter='in';
  271. operate.form='select';
  272. operate.multiple=true;
  273. operate.value=[];
  274. column.operate=operate;
  275. }
  276. if(column.operate=='checkbox' || column.operate=='CHECKBOX'){
  277. operate.filter='in';
  278. operate.form='checkbox';
  279. operate.value=[];
  280. column.operate=operate;
  281. }
  282. if(column.operate=='radio' || column.operate=='RADIO'){
  283. operate.filter='=';
  284. operate.form='radio';
  285. column.operate=operate;
  286. }
  287. if(column.operate=='find_in_set' || column.operate=='FIND_IN_SET'){
  288. operate.filter='FIND_IN_SET';
  289. operate.form='select';
  290. column.operate=operate;
  291. }
  292. if(column.operate=='between' || column.operate=='BETWEEN'){
  293. operate.filter='between';
  294. operate.form='between';
  295. operate.value=[];
  296. column.operate=operate;
  297. }
  298. if(column.operate=='not between' || column.operate=='NOT BETWEEN'){
  299. operate.filter='not between';
  300. operate.form='between';
  301. operate.value=[];
  302. column.operate=operate;
  303. }
  304. if(column.operate=='date' || column.operate=='DATE'){
  305. operate.filter='=';
  306. operate.form='date-picker';
  307. operate.type='date';
  308. column.operate=operate;
  309. }
  310. if(column.operate=='datetime' || column.operate=='DATETIME'){
  311. operate.filter='=';
  312. operate.form='date-picker';
  313. operate.type='datetime';
  314. column.operate=operate;
  315. }
  316. if(column.operate=='daterange' || column.operate=='DATERANGE'){
  317. operate.filter='between time';
  318. operate.form='date-picker';
  319. operate.type='daterange';
  320. column.operate=operate;
  321. }
  322. if(column.operate=='time' || column.operate=='TIME'){
  323. operate.form='time-picker';
  324. operate.filter='=';
  325. operate.type='time';
  326. column.operate=operate;
  327. }
  328. if(column.operate=='timerange' || column.operate=='TIMERANGE'){
  329. operate.form='time-picker';
  330. operate.type='timerange';
  331. operate.filter='between';
  332. column.operate=operate;
  333. }
  334. if(column.operate=='area' || column.operate=='AREA'){
  335. operate.form='cascader';
  336. operate.url='ajax/area';
  337. operate.level=3;
  338. column.operate=operate;
  339. }
  340. if(column.operate=='category' || column.operate=='CATEGORY'){
  341. operate.form='cascader';
  342. operate.url='ajax/category';
  343. operate.level=2;
  344. column.operate=operate;
  345. }
  346. /**简写end**/
  347. if(typeof column.operate=='object'){
  348. for(let k in column.operate){
  349. if(typeof column.operate[k]=='string'){
  350. column.operate[k]=column.operate[k].toLowerCase();
  351. }
  352. }
  353. if(column.operate.form=='selectpage'){
  354. operate.filter='=';
  355. }
  356. if(column.operate.form=='cascader'){
  357. column.operate.props=Object.assign({expandTrigger:'hover',multiple:false,children:'childlist',value:'id',label:'name',lazy:false},column.operate.props);
  358. if(column.operate.url && column.operate.level){
  359. column.operate.props.lazy=true;
  360. column.operate.props.expandTrigger='click';
  361. let url=column.operate.url;
  362. column.operate.props.lazyLoad=function(node,resolve){
  363. let pid=0;
  364. let level=column.operate.level;
  365. if(node.level){
  366. pid=node.value;
  367. }
  368. Yunqi.ajax.get(url,{pid:pid}).then(res=>{
  369. if(res instanceof Array){
  370. res.map(t=>{
  371. if(node.level>=level-1){
  372. t.leaf=true;
  373. }
  374. return t;
  375. });
  376. resolve(res);
  377. }else{
  378. resolve([]);
  379. }
  380. });
  381. };
  382. delete column.operate.options;
  383. }
  384. operate.filter='=';
  385. }
  386. if(column.operate.form=='date-picker'){
  387. if(column.operate.type=='date'){
  388. operate.format='YYYY-MM-DD';
  389. operate.filter='=';
  390. }
  391. if(column.operate.type=='year'){
  392. operate.format='YYYY年';
  393. operate.filter='=';
  394. }
  395. if(column.operate.type=='month'){
  396. operate.format='YYYY年MM月';
  397. operate.filter='=';
  398. }
  399. if(column.operate.type=='dates'){
  400. operate.format='YYYY-MM-DD';
  401. operate.filter='=';
  402. }
  403. if(column.operate.type=='datetime'){
  404. operate.format='YYYY-MM-DD HH:mm:ss';
  405. operate.filter='=';
  406. }
  407. if(column.operate.type=='daterange') {
  408. operate.format = 'YYYY-MM-DD';
  409. operate.filter='between time';
  410. operate.shortcuts = [
  411. {
  412. text: '今天', value: function () {
  413. const start = new Date();
  414. return [start, start];
  415. }
  416. },
  417. {
  418. text: '昨天', value: function () {
  419. const start = new Date();
  420. start.setTime(start.getTime() - 3600 * 1000 * 24 * 1);
  421. return [start, start];
  422. }
  423. },
  424. {
  425. text: '最近7天', value: function () {
  426. const end = new Date();
  427. const start = new Date();
  428. start.setTime(start.getTime() - 3600 * 1000 * 24 * 6);
  429. return [start, end];
  430. }
  431. },
  432. {
  433. text: '最近30天', value: function () {
  434. const end = new Date();
  435. const start = new Date();
  436. start.setTime(start.getTime() - 3600 * 1000 * 24 * 29);
  437. return [start, end];
  438. }
  439. },
  440. {
  441. text: '本月', value: function () {
  442. const end = new Date();
  443. const start = new Date(formatDate(end).slice(0, 7) + '-01');
  444. return [start, end];
  445. }
  446. },
  447. {
  448. text: '上月', value: function () {
  449. const currentDate = new Date();
  450. let lastMonth = currentDate.getMonth() - 1;
  451. let year = currentDate.getFullYear();
  452. if (lastMonth < 0) {
  453. year--;
  454. lastMonth = 11;
  455. }
  456. let firstDay = new Date(year, lastMonth, 1);
  457. let lastDay = new Date(year, lastMonth + 1, 0);
  458. return [firstDay, lastDay];
  459. }
  460. },
  461. {
  462. text: '今年', value: function () {
  463. let currentDate = new Date();
  464. let year = currentDate.getFullYear();
  465. let start = new Date(year, 0, 1);
  466. let end = new Date(year, 11, 31);
  467. return [start, end];
  468. }
  469. },
  470. {
  471. text: '去年', value: function () {
  472. let currentDate = new Date();
  473. let year = currentDate.getFullYear() - 1;
  474. let start = new Date(year, 0, 1);
  475. let end = new Date(year, 11, 31);
  476. return [start, end];
  477. }
  478. },
  479. ];
  480. }
  481. }
  482. //如果用户自定义了属性,则优先使用
  483. for(let key in operate){
  484. column.operate[key]=(column.operate[key]!==undefined)?column.operate[key]:operate[key];
  485. }
  486. }
  487. //通过visible隐藏数据
  488. if(column.visible!='none'){
  489. column.visible=(column.visible===false)?false:true;
  490. }
  491. if(column.field=='operate' || column.checkbox || column.treeExpand){
  492. if(column.field=='operate' && !column.direction){
  493. column.direction='row';
  494. }
  495. if(column.field=='operate' && !column.align){
  496. column.align='center';
  497. }
  498. if(column.field=='operate' && column.action){
  499. for(let kss in column.action){
  500. if(typeof column.action[kss]=='object' && typeof column.action[kss].method=='string'){
  501. column.action[kss].method=Yunqi.app[column.action[kss].method];
  502. }
  503. if(typeof column.action[kss]=='object' && !column.action[kss].visible){
  504. column.action[kss].visible=function(){return true;};
  505. }
  506. }
  507. }
  508. return column;
  509. }
  510. //默认格式
  511. column.formatter=(column.field && (column.formatter===undefined || column.formatter===false))?Yunqi.formatter.text:column.formatter;
  512. //初始化tabs
  513. if(this.tabs && column.field==this.tabs){
  514. this.tabList=column.searchList || {};
  515. }
  516. return column;
  517. },
  518. dataList:function(){
  519. this.loading=true;
  520. //设置排序规则
  521. let sort=this.sortName;
  522. let order=this.order;
  523. if(this.sortData && this.sortData.order.startsWith('desc')){
  524. order='desc';
  525. sort=this.sortData.prop;
  526. }
  527. if(this.sortData && this.sortData.order.startsWith('asc')){
  528. order='asc';
  529. sort=this.sortData.prop;
  530. }
  531. //设置filter
  532. let filter=[];
  533. if(this.tabs && this.tabsValue){
  534. filter.push({
  535. field:this.tabs,
  536. op:'=',
  537. value:this.tabsValue
  538. });
  539. }
  540. for(let i=0;i<this.table_.columns.length;i++){
  541. if(this.table_.columns[i].operate){
  542. if(this.table_.columns[i].operate.value instanceof Array && this.table_.columns[i].operate.value.length===0){
  543. continue;
  544. }
  545. if(this.table_.columns[i].operate.value==='' || this.table_.columns[i].operate.value===null || this.table_.columns[i].operate.value===undefined){
  546. continue;
  547. }
  548. let value=this.table_.columns[i].operate.value;
  549. if(value instanceof Function){
  550. value=value();
  551. }
  552. if(this.table_.columns[i].operate.form=='cascader'){
  553. value=value[value.length-1];
  554. }
  555. //格式化日期时间
  556. if(this.table_.columns[i].operate.form=='date-picker'){
  557. switch (this.table_.columns[i].operate.type){
  558. case 'date':
  559. value=formatDate(this.table_.columns[i].operate.value);
  560. break;
  561. case 'dates':
  562. value=value.map(res=>{
  563. res=formatDate(res);
  564. return res;
  565. });
  566. break;
  567. case 'year':
  568. value=this.table_.columns[i].operate.value.getFullYear();
  569. break;
  570. case 'month':
  571. value=formatDate(this.table_.columns[i].operate.value).slice(0,7);
  572. break;
  573. case 'daterange':
  574. let begin1=formatDate(this.table_.columns[i].operate.value[0])+' 00:00:00';
  575. let end1=formatDate(this.table_.columns[i].operate.value[1])+' 23:59:59';
  576. value=[begin1,end1];
  577. break;
  578. }
  579. }
  580. if(this.table_.columns[i].operate.form=='time-picker'){
  581. let timeValue=this.table_.columns[i].operate.value;
  582. if(timeValue instanceof Array){
  583. value=[formatTime(timeValue[0]),formatTime(timeValue[1])];
  584. }else{
  585. value=formatTime(timeValue);
  586. }
  587. }
  588. filter.push({
  589. field:this.table_.columns[i].operate.name?this.table_.columns[i].operate.name:this.table_.columns[i].field,
  590. op:this.table_.columns[i].operate.filter?this.table_.columns[i].operate.filter:false,
  591. value:value
  592. });
  593. }
  594. }
  595. let json={
  596. sort:sort,
  597. order:order,
  598. filter:[]
  599. };
  600. //下载
  601. if(this.download.show){
  602. let field=[];
  603. let searchList={};
  604. json.listAction=this.extend.index_url;
  605. this.table_.columns.forEach(res=>{
  606. if(inArray(this.download.field,res.field)){
  607. field.push({
  608. field:res.field,
  609. title:res.title,
  610. });
  611. if(res.searchList){
  612. searchList[res.field]=res.searchList;
  613. }
  614. }
  615. });
  616. json.field=field;
  617. json.searchList=searchList;
  618. json.isTree=this.isTree;
  619. if(this.download.page){
  620. json.page=this.currentPage;
  621. json.limit=this.pageSize;
  622. }else{
  623. json.page=1;
  624. json.limit=100000;
  625. }
  626. if(this.download.filter){
  627. json.filter=filter;
  628. }
  629. Yunqi.ajax.json(this.extend.download_url,json,true,false).then(data=>{
  630. let url=Yunqi.config.baseUrl+this.extend.download_url;
  631. if(url.indexOf('?')!=-1){
  632. url+='&file='+data;
  633. }else{
  634. url+='?file='+data;
  635. }
  636. location.href=url;
  637. this.loading=false;
  638. this.download.show=false;
  639. }).catch(error=>{
  640. this.loading=false;
  641. });
  642. }else{
  643. json.searchValue=this.searchValue;
  644. json.search=this.search;
  645. json.filter=filter;
  646. json.page=this.currentPage;
  647. json.limit=this.pageSize;
  648. Yunqi.ajax.json(this.extend.index_url,json).then(data=>{
  649. //this.allData = data
  650. this.$emit('data-loaded', data)
  651. this.total=data.total;
  652. this.render(data.rows);
  653. Vue.nextTick(()=>{
  654. this.onRender(this.list);
  655. });
  656. this.summary=data.summary;
  657. this.loading=false;
  658. }).catch(error=>{
  659. this.loading=false;
  660. });
  661. }
  662. },
  663. render:function(list){
  664. for(let j=0;j<list.length;j++){
  665. let row=list[j];
  666. this.formatRow(row);
  667. if(row.childlist){
  668. this.render(row.childlist);
  669. }
  670. }
  671. this.list=list;
  672. },
  673. formatRow:function (row){
  674. row._formatter={};
  675. for(let i=0;i<this.table_.columns.length;i++){
  676. let columns1=this.table_.columns[i];
  677. if(columns1.children){
  678. for(let j=0;j<columns1.children.length;j++){
  679. let columns2=this.table_.columns[i].children[j];
  680. if(columns2.children){
  681. for(let k=0;k<columns2.children.length;k++){
  682. let columns3=columns2.children[k];
  683. if(columns3.field===undefined){
  684. continue;
  685. }
  686. let formatter=this.getFormatter(row,columns3);
  687. row._formatter[columns3.field]=formatter;
  688. }
  689. }else{
  690. if(columns2.field===undefined){
  691. continue;
  692. }
  693. let formatter=this.getFormatter(row,columns2);
  694. row._formatter[columns2.field]=formatter;
  695. }
  696. }
  697. }else{
  698. if(columns1.field===undefined){
  699. continue;
  700. }
  701. let formatter=this.getFormatter(row,columns1);
  702. row._formatter[columns1.field]=formatter;
  703. }
  704. }
  705. },
  706. getFormatter:function (row,columns){
  707. let value=getValue(row,columns.field);
  708. let formatter=copyObj(Yunqi.formatter.text);
  709. if(columns.searchList){
  710. value=(columns.searchList[value]!==undefined)?columns.searchList[value]:value;
  711. }
  712. if(columns.field=='operate'){
  713. return true;
  714. }
  715. if(!columns.formatter){
  716. formatter.value=value;
  717. }
  718. if(columns.formatter && typeof columns.formatter=='function'){
  719. let rx=columns.formatter(value,row);
  720. if(rx===undefined || rx===''){
  721. rx='-';
  722. }
  723. if(typeof rx == 'object'){
  724. formatter=copyObj(rx);
  725. if(formatter._name=='button' && typeof formatter.click=='string'){
  726. let clickstr=formatter.click;
  727. formatter.click=function(row){
  728. Yunqi.app[clickstr](row);
  729. }
  730. }
  731. }else{
  732. formatter.value=rx;
  733. }
  734. }
  735. if(columns.formatter && typeof columns.formatter=='object'){
  736. formatter=copyObj(columns.formatter);
  737. if(formatter._name=='images'){
  738. value=value?value.split(','):[];
  739. }
  740. if(formatter._name=='date' && typeof value=='number'){
  741. value=formatDate(new Date(value*1000));
  742. }
  743. if(formatter._name=='datetime'){
  744. if(typeof value=='number'){
  745. value=formatDateTime(new Date(value*1000)).slice(0,16);
  746. }else if(!value){
  747. value='-';
  748. }
  749. }
  750. if(formatter._name=='select'){
  751. for(let k in columns.searchList){
  752. if(value==columns.searchList[k]){
  753. value=k;
  754. }
  755. }
  756. }
  757. if(formatter._name=='tags'){
  758. if(value instanceof Array){
  759. }else{
  760. value=value?value.split(','):[];
  761. }
  762. if(columns.searchList){
  763. value=value.map(ts=>{
  764. ts=(columns.searchList[ts]!==undefined)?columns.searchList[ts]:ts;
  765. return ts;
  766. });
  767. }
  768. }
  769. if(formatter._name=='switch' && columns.searchList){
  770. let xs=0,activeValue,inactiveValue;
  771. for(let k in columns.searchList){
  772. if(k==='0')k=0;
  773. if(k==='1')k=1;
  774. if(k===0 || k===1){
  775. activeValue=1;
  776. inactiveValue=0;
  777. }else if(k==='normal' || k==='hidden'){
  778. activeValue='normal';
  779. inactiveValue='hidden';
  780. }else{
  781. if(xs===0){
  782. activeValue=k;
  783. }
  784. if(xs===1){
  785. inactiveValue=k;
  786. }
  787. }
  788. if(value==columns.searchList[k]){
  789. value=k;
  790. }
  791. xs++;
  792. }
  793. formatter.activeValue=activeValue;
  794. formatter.inactiveValue=inactiveValue;
  795. }
  796. formatter.value=value;
  797. }
  798. return formatter;
  799. },
  800. rowDrop:function(){
  801. const _this = this;
  802. const tbody = document.querySelector('.el-table__body-wrapper tbody');
  803. class TreeClass{
  804. constructor(index) {
  805. this.index = index;
  806. this.j = 0;
  807. this.arr=[];
  808. }
  809. getItem(list) {
  810. for(let i=0;i<list.length;i++){
  811. if(this.j===this.index){
  812. return list[i];
  813. }
  814. this.j++;
  815. if(list[i].childlist && list[i].childlist.length>0){
  816. let item = this.getItem(list[i].childlist);
  817. if(item){
  818. return item;
  819. }
  820. }
  821. }
  822. return false;
  823. }
  824. getList(list,pid){
  825. for(let i=0;i<list.length;i++){
  826. if(list[i].pid==pid){
  827. this.arr.push(list[i]);
  828. }
  829. if(list[i].childlist && list[i].childlist.length>0){
  830. this.getList(list[i].childlist,pid);
  831. }
  832. }
  833. return this.arr;
  834. }
  835. };
  836. Sortable.create(tbody, {
  837. // 指定父元素下可被拖拽的子元素
  838. draggable: ".el-table__row",
  839. animation: 100,
  840. handle:'.sortableButton',
  841. ghostClass: "sortable-ghost",
  842. chosenClass: "sortable-chosen",
  843. dragClass: "sortable-drag",
  844. onEnd ({ newIndex, oldIndex }) {
  845. let data=[];
  846. if(_this.isTree){
  847. let new_item=(new TreeClass(newIndex)).getItem(_this.list);
  848. let old_item=(new TreeClass(oldIndex)).getItem(_this.list);
  849. if(new_item.weigh===undefined || old_item.weigh===undefined){
  850. Yunqi.message.error(__('没有weigh属性,排序失败'));
  851. return;
  852. }
  853. if(new_item.pid!==old_item.pid){
  854. Yunqi.message.error(__('只支持在同级别表内拖拽'));
  855. return;
  856. }
  857. //找到影响拖拽的其他行,并修改weigh属性
  858. let list=(new TreeClass(oldIndex)).getList(_this.list,old_item.pid);
  859. let weigh=new_item.weigh;
  860. data.push({id:old_item.id,weigh:weigh});
  861. //从上往下拖
  862. if(newIndex>oldIndex){
  863. for(let i=list.length-1;i>=0;i--){
  864. if(list[i].id==old_item.id){
  865. break;
  866. }
  867. if(list[i].weigh<new_item.weigh){
  868. continue;
  869. }
  870. if(list[i].weigh<=old_item.weigh){
  871. data.push({
  872. id:list[i].id,
  873. weigh:list[i].weigh+1
  874. });
  875. }
  876. }
  877. }
  878. //从下往上拖
  879. if(newIndex<oldIndex){
  880. for(let i=0;i<list.length;i++){
  881. if(list[i].id==old_item.id){
  882. break;
  883. }
  884. if(list[i].weigh<=new_item.weigh){
  885. data.push({
  886. id:list[i].id,
  887. weigh:list[i].weigh-1
  888. });
  889. }
  890. }
  891. }
  892. }else{
  893. let new_weigh=_this.list[newIndex].weigh;
  894. let old_weigh=_this.list[oldIndex].weigh;
  895. if(new_weigh===undefined || old_weigh===undefined){
  896. Yunqi.message.error(__('没有weigh属性,排序失败'));
  897. return;
  898. }
  899. data.push({id:_this.list[oldIndex].id,weigh:new_weigh});
  900. let line=Math.abs(oldIndex-newIndex);
  901. let weigh=_this.list[newIndex].weigh;
  902. (newIndex>oldIndex)?weigh++:weigh--;
  903. let i=0;
  904. while(i<line){
  905. data.push({
  906. id:(newIndex>oldIndex)?_this.list[newIndex-i].id:_this.list[newIndex+i].id,
  907. weigh:(newIndex>oldIndex)?weigh++:weigh--
  908. });
  909. i++;
  910. }
  911. }
  912. const elloading=ElementPlus.ElLoading.service({text:'排序中..'});
  913. const promise=data.map(res=>{
  914. return new Promise((resolve, reject)=>{
  915. Yunqi.ajax.post(_this.extend.multi_url,{ids:res.id,field:'weigh',value:res.weigh},false,false).then(res=>{
  916. resolve();
  917. }).catch(err=>{
  918. reject();
  919. });
  920. });
  921. });
  922. Promise.all(promise).then(res=>{
  923. elloading.close();
  924. top.ElementPlus.ElMessage({
  925. message: '排序完成',
  926. type: 'success'
  927. });
  928. _this.reload();
  929. }).catch(err=>{
  930. elloading.close();
  931. });
  932. }
  933. });
  934. },
  935. tabChange:function (e){
  936. this.tabsValue=e;
  937. this.reload();
  938. },
  939. reload:function(){
  940. this.selections=[];
  941. this.dataList();
  942. },
  943. blurSearch:function(){
  944. this.dataList();
  945. },
  946. del:function(){
  947. let ids=[];
  948. this.selections.forEach(res=>{
  949. ids.push(res[this.pk]);
  950. });
  951. Yunqi.api.del(this.extend.del_url,ids,()=>{
  952. this.dataList();
  953. this.selections=[];
  954. });
  955. },
  956. delOne:function(row){
  957. Yunqi.api.del(this.extend.del_url,row[this.pk],()=>{
  958. this.dataList();
  959. this.selections=[];
  960. });
  961. },
  962. selectOne:function(e){
  963. this.selections=e;
  964. },
  965. submit:function(){
  966. this.currentPage=1;
  967. this.dataList();
  968. },
  969. selectAll:function(e){
  970. this.selections=e;
  971. },
  972. handleSizeChange:function(size){
  973. this.pageSize=size;
  974. this.dataList();
  975. },
  976. handleCurrentChange:function(page){
  977. this.currentPage=page;
  978. this.dataList();
  979. },
  980. changeSort:function(e){
  981. if(!e.order){
  982. this.sortData='';
  983. }else{
  984. this.sortData=e;
  985. }
  986. this.dataList();
  987. },
  988. changeVisiable:function(field){
  989. let columns=this.table_.columns;
  990. for(let i=0;i<columns.length;i++){
  991. if(columns[i].children){
  992. let show1=false;
  993. for(let j=0;j<columns[i].children.length;j++){
  994. if(columns[i].children[j].children){
  995. let show2=false;
  996. for (let k=0;k<columns[i].children[j].children.length;k++){
  997. if(columns[i].children[j].children[k].field==field){
  998. columns[i].children[j].children[k].visible=!columns[i].children[j].children[k].visible;
  999. }
  1000. show2=show2 || columns[i].children[j].children[k].visible;
  1001. }
  1002. columns[i].children[j].visible=show2;
  1003. }else{
  1004. if(columns[i].children[j].field==field){
  1005. columns[i].children[j].visible=!columns[i].children[j].visible;
  1006. }
  1007. }
  1008. show1=show1 || columns[i].children[j].visible;
  1009. }
  1010. columns[i].visible=show1;
  1011. }else{
  1012. if(columns[i].field==field){
  1013. columns[i].visible=!columns[i].visible;
  1014. }
  1015. }
  1016. }
  1017. },
  1018. changeShow:function(status){
  1019. let ids=[];
  1020. this.selections.forEach(res=>{
  1021. ids.push(res[this.pk]);
  1022. });
  1023. let options={
  1024. ids:ids,
  1025. field:'status',
  1026. value:status
  1027. };
  1028. Yunqi.api.multi(this.extend.multi_url,options,()=>{
  1029. this.dataList();
  1030. this.selections=[];
  1031. });
  1032. },
  1033. changeSwitch:function(row,field){
  1034. let value=row._formatter[field].value;
  1035. setValue(row,field,value);
  1036. let options={
  1037. ids:row[this.pk],
  1038. field:field,
  1039. value:value
  1040. };
  1041. Yunqi.api.multi(this.extend.multi_url,options,()=>{
  1042. this.render(this.list);
  1043. });
  1044. },
  1045. changeSelect:function(row,field){
  1046. let value=row._formatter[field].value;
  1047. setValue(row,field,value);
  1048. let options={
  1049. ids:row[this.pk],
  1050. field:field,
  1051. value:value
  1052. };
  1053. Yunqi.api.multi(this.extend.multi_url,options,()=>{
  1054. this.render(this.list);
  1055. });
  1056. },
  1057. changeExpand:function () {
  1058. this.mainFrameExpand=!this.mainFrameExpand;
  1059. if(this.mainFrameExpand){
  1060. Yunqi.api.expand();
  1061. }else{
  1062. Yunqi.api.compress();
  1063. }
  1064. },
  1065. changeSelectpage:function (r){
  1066. for(let i=0;i<this.table_.columns.length;i++){
  1067. if(this.table_.columns[i].field==r.field){
  1068. this.table_.columns[i].operate.value=r.value;
  1069. return;
  1070. }
  1071. }
  1072. },
  1073. clickRightToolBar:function (btn){
  1074. if(btn=='column'){
  1075. let show=[];
  1076. let columns=this.table_.columns;
  1077. for(let i=0;i<columns.length;i++){
  1078. if(columns[i].children){
  1079. for(let j=0;j<columns[i].children.length;j++){
  1080. if(columns[i].children[j].children){
  1081. for (let k=0;k<columns[i].children[j].children.length;k++){
  1082. show.push(columns[i].children[j].children[k]);
  1083. }
  1084. }else{
  1085. show.push(columns[i].children[j]);
  1086. }
  1087. }
  1088. }else{
  1089. show.push(columns[i]);
  1090. }
  1091. }
  1092. this.rightToolOption={type:'column',list:show};
  1093. }
  1094. if(btn=='font'){
  1095. this.rightToolOption={type:'font',list:{large:'大',default:'中',small:'小'}};
  1096. }
  1097. if(btn=='download'){
  1098. if(!this.extend.download_url){
  1099. Yunqi.message.error('download_url未设置');
  1100. return;
  1101. }
  1102. this.download.show=true;
  1103. }
  1104. },
  1105. clickLink:function (str,triger='copy'){
  1106. if(triger=='redict'){
  1107. window.open(str,'_blank');
  1108. }
  1109. if(triger=='copy'){
  1110. navigator.clipboard.writeText(str);
  1111. Yunqi.message.success('复制成功');
  1112. }
  1113. },
  1114. changeFont:function (key){
  1115. this.pageFont=key;
  1116. this.$refs.rightToolSelect.visible=false
  1117. this.table_.searchFormVisible=!this.table_.searchFormVisible;
  1118. setTimeout(()=>{
  1119. this.table_.searchFormVisible=!this.table_.searchFormVisible;
  1120. },50);
  1121. },
  1122. recyclebin:function (){
  1123. if(this.extend.recyclebin_url==undefined){
  1124. Yunqi.message.error(__('recyclebin_url未设置'));
  1125. return;
  1126. }
  1127. let table={
  1128. width:'80%',
  1129. height:690,
  1130. title:__('回收站'),
  1131. url:this.extend.recyclebin_url+'?action=list',
  1132. icon:'fa fa-recycle',
  1133. close:function (){
  1134. let id=top.Yunqi.app.activeTab.id;
  1135. let tab=top.document.getElementById('addtabs-'+id).contentWindow;
  1136. let doc=tab.document.getElementsByClassName('refresh');
  1137. if(doc.length>0){
  1138. doc[0].click();
  1139. }
  1140. }
  1141. };
  1142. Yunqi.api.open(table);
  1143. },
  1144. add:function () {
  1145. if(this.extend.add_url==undefined){
  1146. Yunqi.message.error(__('add_url未设置'));
  1147. return;
  1148. }
  1149. let form={...this.addForm,title:__('添加'),url:this.extend.add_url,icon:'fa fa-plus'};
  1150. if(form.expand==undefined){
  1151. form.expand=false;
  1152. }
  1153. form.close=function (refresh=false){
  1154. if(!refresh){
  1155. return
  1156. }
  1157. let id=top.Yunqi.app.activeTab.id;
  1158. let tab=top.document.getElementById('addtabs-'+id).contentWindow;
  1159. let doc=tab.document.getElementsByClassName('refresh');
  1160. if(doc.length>0){
  1161. doc[0].click();
  1162. }
  1163. };
  1164. Yunqi.api.open(form);
  1165. },
  1166. edit:function (row) {
  1167. if(this.extend.edit_url==undefined){
  1168. Yunqi.message.error(__('edit_url未设置'));
  1169. return;
  1170. }
  1171. let form={...this.editForm,title:__('编辑'),icon:'fa fa-pencil-square-o'};
  1172. if(form.expand==undefined){
  1173. form.expand=false;
  1174. }
  1175. let ids=[];
  1176. if(row){
  1177. ids.push(row[this.pk]);
  1178. }else{
  1179. this.selections.forEach(res=>{
  1180. ids.push(res[this.pk]);
  1181. });
  1182. }
  1183. let time=0;
  1184. ids.forEach(res=>{
  1185. setTimeout(()=>{
  1186. form.url=(this.extend.edit_url.indexOf('?')!=-1)?this.extend.edit_url+'&ids='+res:this.extend.edit_url+'?ids='+res,
  1187. form.close=function (refresh=false){
  1188. if(!refresh){
  1189. return;
  1190. }
  1191. let id=top.Yunqi.app.activeTab.id;
  1192. let tab=top.document.getElementById('addtabs-'+id).contentWindow;
  1193. let doc=tab.document.getElementsByClassName('refresh');
  1194. if(doc.length>0){
  1195. doc[0].click();
  1196. }
  1197. };
  1198. Yunqi.api.open(form);
  1199. },time);
  1200. time+=100;
  1201. });
  1202. },
  1203. previewImg(imgs){
  1204. Yunqi.api.previewImg(imgs);
  1205. },
  1206. importExcel:function (){
  1207. document.querySelector('.importUpload button').click();
  1208. },
  1209. handleImportSuccess:function (e){
  1210. let file=e.data.url;
  1211. if(!this.extend.import_url){
  1212. Yunqi.message.error('import_url未设置');
  1213. return;
  1214. }
  1215. Yunqi.ajax.post(this.extend.import_url,{file:file}).then(res=>{
  1216. this.importResult.success=res.success;
  1217. this.importResult.fail=res.fail;
  1218. this.importResult.show=true;
  1219. this.reload();
  1220. });
  1221. }
  1222. }
  1223. };