Table.js 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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.total=data.total;
  651. this.render(data.rows);
  652. Vue.nextTick(()=>{
  653. this.onRender(this.list);
  654. });
  655. this.summary=data.summary;
  656. this.loading=false;
  657. }).catch(error=>{
  658. this.loading=false;
  659. });
  660. }
  661. },
  662. render:function(list){
  663. for(let j=0;j<list.length;j++){
  664. let row=list[j];
  665. this.formatRow(row);
  666. if(row.childlist){
  667. this.render(row.childlist);
  668. }
  669. }
  670. this.list=list;
  671. },
  672. formatRow:function (row){
  673. row._formatter={};
  674. for(let i=0;i<this.table_.columns.length;i++){
  675. let columns1=this.table_.columns[i];
  676. if(columns1.children){
  677. for(let j=0;j<columns1.children.length;j++){
  678. let columns2=this.table_.columns[i].children[j];
  679. if(columns2.children){
  680. for(let k=0;k<columns2.children.length;k++){
  681. let columns3=columns2.children[k];
  682. if(columns3.field===undefined){
  683. continue;
  684. }
  685. let formatter=this.getFormatter(row,columns3);
  686. row._formatter[columns3.field]=formatter;
  687. }
  688. }else{
  689. if(columns2.field===undefined){
  690. continue;
  691. }
  692. let formatter=this.getFormatter(row,columns2);
  693. row._formatter[columns2.field]=formatter;
  694. }
  695. }
  696. }else{
  697. if(columns1.field===undefined){
  698. continue;
  699. }
  700. let formatter=this.getFormatter(row,columns1);
  701. row._formatter[columns1.field]=formatter;
  702. }
  703. }
  704. },
  705. getFormatter:function (row,columns){
  706. let value=getValue(row,columns.field);
  707. let formatter=copyObj(Yunqi.formatter.text);
  708. if(columns.searchList){
  709. value=(columns.searchList[value]!==undefined)?columns.searchList[value]:value;
  710. }
  711. if(columns.field=='operate'){
  712. return true;
  713. }
  714. if(!columns.formatter){
  715. formatter.value=value;
  716. }
  717. if(columns.formatter && typeof columns.formatter=='function'){
  718. let rx=columns.formatter(value,row);
  719. if(rx===undefined || rx===''){
  720. rx='-';
  721. }
  722. if(typeof rx == 'object'){
  723. formatter=copyObj(rx);
  724. if(formatter._name=='button' && typeof formatter.click=='string'){
  725. let clickstr=formatter.click;
  726. formatter.click=function(row){
  727. Yunqi.app[clickstr](row);
  728. }
  729. }
  730. }else{
  731. formatter.value=rx;
  732. }
  733. }
  734. if(columns.formatter && typeof columns.formatter=='object'){
  735. formatter=copyObj(columns.formatter);
  736. if(formatter._name=='images'){
  737. value=value?value.split(','):[];
  738. }
  739. if(formatter._name=='date' && typeof value=='number'){
  740. value=formatDate(new Date(value*1000));
  741. }
  742. if(formatter._name=='datetime'){
  743. if(typeof value=='number'){
  744. value=formatDateTime(new Date(value*1000)).slice(0,16);
  745. }else if(!value){
  746. value='-';
  747. }
  748. }
  749. if(formatter._name=='select'){
  750. for(let k in columns.searchList){
  751. if(value==columns.searchList[k]){
  752. value=k;
  753. }
  754. }
  755. }
  756. if(formatter._name=='tags'){
  757. if(value instanceof Array){
  758. }else{
  759. value=value?value.split(','):[];
  760. }
  761. if(columns.searchList){
  762. value=value.map(ts=>{
  763. ts=(columns.searchList[ts]!==undefined)?columns.searchList[ts]:ts;
  764. return ts;
  765. });
  766. }
  767. }
  768. if(formatter._name=='switch' && columns.searchList){
  769. let xs=0,activeValue,inactiveValue;
  770. for(let k in columns.searchList){
  771. if(k==='0')k=0;
  772. if(k==='1')k=1;
  773. if(k===0 || k===1){
  774. activeValue=1;
  775. inactiveValue=0;
  776. }else if(k==='normal' || k==='hidden'){
  777. activeValue='normal';
  778. inactiveValue='hidden';
  779. }else{
  780. if(xs===0){
  781. activeValue=k;
  782. }
  783. if(xs===1){
  784. inactiveValue=k;
  785. }
  786. }
  787. if(value==columns.searchList[k]){
  788. value=k;
  789. }
  790. xs++;
  791. }
  792. formatter.activeValue=activeValue;
  793. formatter.inactiveValue=inactiveValue;
  794. }
  795. formatter.value=value;
  796. }
  797. return formatter;
  798. },
  799. rowDrop:function(){
  800. const _this = this;
  801. const tbody = document.querySelector('.el-table__body-wrapper tbody');
  802. class TreeClass{
  803. constructor(index) {
  804. this.index = index;
  805. this.j = 0;
  806. this.arr=[];
  807. }
  808. getItem(list) {
  809. for(let i=0;i<list.length;i++){
  810. if(this.j===this.index){
  811. return list[i];
  812. }
  813. this.j++;
  814. if(list[i].childlist && list[i].childlist.length>0){
  815. let item = this.getItem(list[i].childlist);
  816. if(item){
  817. return item;
  818. }
  819. }
  820. }
  821. return false;
  822. }
  823. getList(list,pid){
  824. for(let i=0;i<list.length;i++){
  825. if(list[i].pid==pid){
  826. this.arr.push(list[i]);
  827. }
  828. if(list[i].childlist && list[i].childlist.length>0){
  829. this.getList(list[i].childlist,pid);
  830. }
  831. }
  832. return this.arr;
  833. }
  834. };
  835. Sortable.create(tbody, {
  836. // 指定父元素下可被拖拽的子元素
  837. draggable: ".el-table__row",
  838. animation: 100,
  839. handle:'.sortableButton',
  840. ghostClass: "sortable-ghost",
  841. chosenClass: "sortable-chosen",
  842. dragClass: "sortable-drag",
  843. onEnd ({ newIndex, oldIndex }) {
  844. let data=[];
  845. if(_this.isTree){
  846. let new_item=(new TreeClass(newIndex)).getItem(_this.list);
  847. let old_item=(new TreeClass(oldIndex)).getItem(_this.list);
  848. if(new_item.weigh===undefined || old_item.weigh===undefined){
  849. Yunqi.message.error(__('没有weigh属性,排序失败'));
  850. return;
  851. }
  852. if(new_item.pid!==old_item.pid){
  853. Yunqi.message.error(__('只支持在同级别表内拖拽'));
  854. return;
  855. }
  856. //找到影响拖拽的其他行,并修改weigh属性
  857. let list=(new TreeClass(oldIndex)).getList(_this.list,old_item.pid);
  858. let weigh=new_item.weigh;
  859. data.push({id:old_item.id,weigh:weigh});
  860. //从上往下拖
  861. if(newIndex>oldIndex){
  862. for(let i=list.length-1;i>=0;i--){
  863. if(list[i].id==old_item.id){
  864. break;
  865. }
  866. if(list[i].weigh<new_item.weigh){
  867. continue;
  868. }
  869. if(list[i].weigh<=old_item.weigh){
  870. data.push({
  871. id:list[i].id,
  872. weigh:list[i].weigh+1
  873. });
  874. }
  875. }
  876. }
  877. //从下往上拖
  878. if(newIndex<oldIndex){
  879. for(let i=0;i<list.length;i++){
  880. if(list[i].id==old_item.id){
  881. break;
  882. }
  883. if(list[i].weigh<=new_item.weigh){
  884. data.push({
  885. id:list[i].id,
  886. weigh:list[i].weigh-1
  887. });
  888. }
  889. }
  890. }
  891. }else{
  892. let new_weigh=_this.list[newIndex].weigh;
  893. let old_weigh=_this.list[oldIndex].weigh;
  894. if(new_weigh===undefined || old_weigh===undefined){
  895. Yunqi.message.error(__('没有weigh属性,排序失败'));
  896. return;
  897. }
  898. data.push({id:_this.list[oldIndex].id,weigh:new_weigh});
  899. let line=Math.abs(oldIndex-newIndex);
  900. let weigh=_this.list[newIndex].weigh;
  901. (newIndex>oldIndex)?weigh++:weigh--;
  902. let i=0;
  903. while(i<line){
  904. data.push({
  905. id:(newIndex>oldIndex)?_this.list[newIndex-i].id:_this.list[newIndex+i].id,
  906. weigh:(newIndex>oldIndex)?weigh++:weigh--
  907. });
  908. i++;
  909. }
  910. }
  911. const elloading=ElementPlus.ElLoading.service({text:'排序中..'});
  912. const promise=data.map(res=>{
  913. return new Promise((resolve, reject)=>{
  914. Yunqi.ajax.post(_this.extend.multi_url,{ids:res.id,field:'weigh',value:res.weigh},false,false).then(res=>{
  915. resolve();
  916. }).catch(err=>{
  917. reject();
  918. });
  919. });
  920. });
  921. Promise.all(promise).then(res=>{
  922. elloading.close();
  923. top.ElementPlus.ElMessage({
  924. message: '排序完成',
  925. type: 'success'
  926. });
  927. _this.reload();
  928. }).catch(err=>{
  929. elloading.close();
  930. });
  931. }
  932. });
  933. },
  934. tabChange:function (e){
  935. this.tabsValue=e;
  936. this.reload();
  937. },
  938. reload:function(){
  939. this.selections=[];
  940. this.dataList();
  941. },
  942. blurSearch:function(){
  943. this.dataList();
  944. },
  945. del:function(){
  946. let ids=[];
  947. this.selections.forEach(res=>{
  948. ids.push(res[this.pk]);
  949. });
  950. Yunqi.api.del(this.extend.del_url,ids,()=>{
  951. this.dataList();
  952. this.selections=[];
  953. });
  954. },
  955. delOne:function(row){
  956. Yunqi.api.del(this.extend.del_url,row[this.pk],()=>{
  957. this.dataList();
  958. this.selections=[];
  959. });
  960. },
  961. selectOne:function(e){
  962. this.selections=e;
  963. },
  964. submit:function(){
  965. this.currentPage=1;
  966. this.dataList();
  967. },
  968. selectAll:function(e){
  969. this.selections=e;
  970. },
  971. handleSizeChange:function(size){
  972. this.pageSize=size;
  973. this.dataList();
  974. },
  975. handleCurrentChange:function(page){
  976. this.currentPage=page;
  977. this.dataList();
  978. },
  979. changeSort:function(e){
  980. if(!e.order){
  981. this.sortData='';
  982. }else{
  983. this.sortData=e;
  984. }
  985. this.dataList();
  986. },
  987. changeVisiable:function(field){
  988. let columns=this.table_.columns;
  989. for(let i=0;i<columns.length;i++){
  990. if(columns[i].children){
  991. let show1=false;
  992. for(let j=0;j<columns[i].children.length;j++){
  993. if(columns[i].children[j].children){
  994. let show2=false;
  995. for (let k=0;k<columns[i].children[j].children.length;k++){
  996. if(columns[i].children[j].children[k].field==field){
  997. columns[i].children[j].children[k].visible=!columns[i].children[j].children[k].visible;
  998. }
  999. show2=show2 || columns[i].children[j].children[k].visible;
  1000. }
  1001. columns[i].children[j].visible=show2;
  1002. }else{
  1003. if(columns[i].children[j].field==field){
  1004. columns[i].children[j].visible=!columns[i].children[j].visible;
  1005. }
  1006. }
  1007. show1=show1 || columns[i].children[j].visible;
  1008. }
  1009. columns[i].visible=show1;
  1010. }else{
  1011. if(columns[i].field==field){
  1012. columns[i].visible=!columns[i].visible;
  1013. }
  1014. }
  1015. }
  1016. },
  1017. changeShow:function(status){
  1018. let ids=[];
  1019. this.selections.forEach(res=>{
  1020. ids.push(res[this.pk]);
  1021. });
  1022. let options={
  1023. ids:ids,
  1024. field:'status',
  1025. value:status
  1026. };
  1027. Yunqi.api.multi(this.extend.multi_url,options,()=>{
  1028. this.dataList();
  1029. this.selections=[];
  1030. });
  1031. },
  1032. changeSwitch:function(row,field){
  1033. let value=row._formatter[field].value;
  1034. setValue(row,field,value);
  1035. let options={
  1036. ids:row[this.pk],
  1037. field:field,
  1038. value:value
  1039. };
  1040. Yunqi.api.multi(this.extend.multi_url,options,()=>{
  1041. this.render(this.list);
  1042. });
  1043. },
  1044. changeSelect:function(row,field){
  1045. let value=row._formatter[field].value;
  1046. setValue(row,field,value);
  1047. let options={
  1048. ids:row[this.pk],
  1049. field:field,
  1050. value:value
  1051. };
  1052. Yunqi.api.multi(this.extend.multi_url,options,()=>{
  1053. this.render(this.list);
  1054. });
  1055. },
  1056. changeExpand:function () {
  1057. this.mainFrameExpand=!this.mainFrameExpand;
  1058. if(this.mainFrameExpand){
  1059. Yunqi.api.expand();
  1060. }else{
  1061. Yunqi.api.compress();
  1062. }
  1063. },
  1064. changeSelectpage:function (r){
  1065. for(let i=0;i<this.table_.columns.length;i++){
  1066. if(this.table_.columns[i].field==r.field){
  1067. this.table_.columns[i].operate.value=r.value;
  1068. return;
  1069. }
  1070. }
  1071. },
  1072. clickRightToolBar:function (btn){
  1073. if(btn=='column'){
  1074. let show=[];
  1075. let columns=this.table_.columns;
  1076. for(let i=0;i<columns.length;i++){
  1077. if(columns[i].children){
  1078. for(let j=0;j<columns[i].children.length;j++){
  1079. if(columns[i].children[j].children){
  1080. for (let k=0;k<columns[i].children[j].children.length;k++){
  1081. show.push(columns[i].children[j].children[k]);
  1082. }
  1083. }else{
  1084. show.push(columns[i].children[j]);
  1085. }
  1086. }
  1087. }else{
  1088. show.push(columns[i]);
  1089. }
  1090. }
  1091. this.rightToolOption={type:'column',list:show};
  1092. }
  1093. if(btn=='font'){
  1094. this.rightToolOption={type:'font',list:{large:'大',default:'中',small:'小'}};
  1095. }
  1096. if(btn=='download'){
  1097. if(!this.extend.download_url){
  1098. Yunqi.message.error('download_url未设置');
  1099. return;
  1100. }
  1101. this.download.show=true;
  1102. }
  1103. },
  1104. clickLink:function (str,triger='copy'){
  1105. if(triger=='redict'){
  1106. window.open(str,'_blank');
  1107. }
  1108. if(triger=='copy'){
  1109. navigator.clipboard.writeText(str);
  1110. Yunqi.message.success('复制成功');
  1111. }
  1112. },
  1113. changeFont:function (key){
  1114. this.pageFont=key;
  1115. this.$refs.rightToolSelect.visible=false
  1116. this.table_.searchFormVisible=!this.table_.searchFormVisible;
  1117. setTimeout(()=>{
  1118. this.table_.searchFormVisible=!this.table_.searchFormVisible;
  1119. },50);
  1120. },
  1121. recyclebin:function (){
  1122. if(this.extend.recyclebin_url==undefined){
  1123. Yunqi.message.error(__('recyclebin_url未设置'));
  1124. return;
  1125. }
  1126. let table={
  1127. width:'80%',
  1128. height:690,
  1129. title:__('回收站'),
  1130. url:this.extend.recyclebin_url+'?action=list',
  1131. icon:'fa fa-recycle',
  1132. close:function (){
  1133. let id=top.Yunqi.app.activeTab.id;
  1134. let tab=top.document.getElementById('addtabs-'+id).contentWindow;
  1135. let doc=tab.document.getElementsByClassName('refresh');
  1136. if(doc.length>0){
  1137. doc[0].click();
  1138. }
  1139. }
  1140. };
  1141. Yunqi.api.open(table);
  1142. },
  1143. add:function () {
  1144. if(this.extend.add_url==undefined){
  1145. Yunqi.message.error(__('add_url未设置'));
  1146. return;
  1147. }
  1148. let form={...this.addForm,title:__('添加'),url:this.extend.add_url,icon:'fa fa-plus'};
  1149. if(form.expand==undefined){
  1150. form.expand=false;
  1151. }
  1152. form.close=function (refresh=false){
  1153. if(!refresh){
  1154. return
  1155. }
  1156. let id=top.Yunqi.app.activeTab.id;
  1157. let tab=top.document.getElementById('addtabs-'+id).contentWindow;
  1158. let doc=tab.document.getElementsByClassName('refresh');
  1159. if(doc.length>0){
  1160. doc[0].click();
  1161. }
  1162. };
  1163. Yunqi.api.open(form);
  1164. },
  1165. edit:function (row) {
  1166. if(this.extend.edit_url==undefined){
  1167. Yunqi.message.error(__('edit_url未设置'));
  1168. return;
  1169. }
  1170. let form={...this.editForm,title:__('编辑'),icon:'fa fa-pencil-square-o'};
  1171. if(form.expand==undefined){
  1172. form.expand=false;
  1173. }
  1174. let ids=[];
  1175. if(row){
  1176. ids.push(row[this.pk]);
  1177. }else{
  1178. this.selections.forEach(res=>{
  1179. ids.push(res[this.pk]);
  1180. });
  1181. }
  1182. let time=0;
  1183. ids.forEach(res=>{
  1184. setTimeout(()=>{
  1185. form.url=(this.extend.edit_url.indexOf('?')!=-1)?this.extend.edit_url+'&ids='+res:this.extend.edit_url+'?ids='+res,
  1186. form.close=function (refresh=false){
  1187. if(!refresh){
  1188. return;
  1189. }
  1190. let id=top.Yunqi.app.activeTab.id;
  1191. let tab=top.document.getElementById('addtabs-'+id).contentWindow;
  1192. let doc=tab.document.getElementsByClassName('refresh');
  1193. if(doc.length>0){
  1194. doc[0].click();
  1195. }
  1196. };
  1197. Yunqi.api.open(form);
  1198. },time);
  1199. time+=100;
  1200. });
  1201. },
  1202. previewImg(imgs){
  1203. Yunqi.api.previewImg(imgs);
  1204. },
  1205. importExcel:function (){
  1206. document.querySelector('.importUpload button').click();
  1207. },
  1208. handleImportSuccess:function (e){
  1209. let file=e.data.url;
  1210. if(!this.extend.import_url){
  1211. Yunqi.message.error('import_url未设置');
  1212. return;
  1213. }
  1214. Yunqi.ajax.post(this.extend.import_url,{file:file}).then(res=>{
  1215. this.importResult.success=res.success;
  1216. this.importResult.fail=res.fail;
  1217. this.importResult.show=true;
  1218. this.reload();
  1219. });
  1220. }
  1221. }
  1222. };