zb-table.vue 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. <template>
  2. <!-- #ifdef H5 || APP-PLUS -->
  3. <view :class="['zb-table','zb-table-fixed-header',!border&&(bodyTableLeft>50||headerTableLeft>50)&&'scroll-left-fixed']">
  4. <view class="zb-table-content" style="flex: 1">
  5. <view class="zb-table-scroll" style="height: 100%;">
  6. <template v-if="showHeader">
  7. <view class="zb-table-header top-header-uni" :style="{paddingRight:`${scrollbarSize}px`}"
  8. >
  9. <scroll-view class="zb-table-headers"
  10. @touchmove.stop="getMove"
  11. @scroll="handleTableScrollLeft"
  12. scroll-x="true"
  13. scroll-y="false"
  14. id="tableHeaders"
  15. scroll-anchoring="true"
  16. :scroll-left="headerTableLeft"
  17. style="
  18. height: 100%">
  19. <view class="zb-table-fixed" >
  20. <view class="zb-table-thead" style="position: relative;" >
  21. <view class="item-tr">
  22. <view
  23. @click.stop="sortAction(item,index)"
  24. class="item-th"
  25. :style="[{
  26. width:`${item.width?item.width:'100'}px`,
  27. flex:index===transColumns.length-1?1:'none',
  28. minWidth:`${item.width?item.width:'100'}px`,
  29. borderRight:`${border?'1px solid #e8e8e8':''}`,
  30. borderRight:`${(scrollbarSize&&index===transColumns.length-1)?'':border?'1px solid #e8e8e8':''}`,
  31. borderTop:`${border?'1px solid #e8e8e8':''}`,
  32. textAlign:item.align||'left'
  33. },getHeaderCellStyle(item,index)]"
  34. v-for="(item,index) in transColumns" :key="index">
  35. <template v-if="item.type==='selection'">
  36. <view class="checkbox-item">
  37. <tableCheckbox
  38. :indeterminate="indeterminate" :checked="checkedAll" @checkboxSelected="checkboxSelectedAll"></tableCheckbox>
  39. </view>
  40. </template>
  41. <template v-else>
  42. {{ item.label }}
  43. <view class="sorter-table" v-if="item.sorter">
  44. <view :class="['sorter-table-icon',item.sorterMode==='_asc'&&`sorting${item.sorterMode||''}`]"></view>
  45. <view :class="['sorter-table-icon',item.sorterMode==='_desc'&&`sorting${item.sorterMode||''}`]"></view>
  46. </view>
  47. </template>
  48. </view>
  49. </view>
  50. </view>
  51. </view>
  52. </scroll-view>
  53. </view>
  54. </template>
  55. <template v-if="!data.length">
  56. <view class="no-data">暂无数据~~</view>
  57. </template>
  58. <scroll-view
  59. class="zb-table-body" ref="tableBody" scroll-x="true" scroll-y="true" id="tableBody"
  60. :lower-threshold="40"
  61. :upper-threshold="10"
  62. @touchmove.stop="getMove"
  63. @scrolltolower="scrolltolower"
  64. @scrolltoupper="(e)=>debounce(scrollToLeft)(e)"
  65. @scroll="handleBodyScroll" :scroll-left="bodyTableLeft" :scroll-top="bodyScrollTop"
  66. :style=" `height: calc(100% - ${showSummary?80:40}px)`" >
  67. <view class="zb-table-fixed">
  68. <view class="zb-table-tbody">
  69. <view :class="['item-tr',highlight&&isHighlight(item,index)?'current-row':'']"
  70. @click.stop="rowClick(item,index)"
  71. v-for="(item,index) in transData" :key="item.key" >
  72. <view
  73. :style="[{
  74. width:`${ite.width?ite.width:'100'}px`,
  75. flex:i===transColumns.length-1?1:'none',
  76. minWidth:`${ite.width?ite.width:'100'}px`,
  77. borderRight:`${border?'1px solid #e8e8e8':''}`,
  78. textAlign:ite.align||'left',
  79. },cellStyle&&getCellStyle(item,ite,index,i)]"
  80. :class="['item-td',stripe?(index % 2) != 0?'odd':'even':'']"
  81. v-for="(ite,i) in transColumns" :key="i">
  82. <template v-if="ite.type==='operation'">
  83. <view style="display: flex;align-items: center;height: 100%">
  84. <view
  85. v-for="ren,ind in permission(item,ite.renders,index)"
  86. :key="ind"
  87. @click.stop="$emit(ren.func,item,index)"
  88. :style="{
  89. display:'flex',
  90. alignItems: 'center',
  91. marginRight:ite.renders.length>1?'8px':'0'
  92. }">
  93. <template v-if="ren.type==='custom'">
  94. <view :class="ren.class||''" style="cursor: pointer">
  95. {{ren.name}}
  96. </view>
  97. </template>
  98. <template v-else>
  99. <button
  100. :class="ren.class||''"
  101. :type="ren.type||'primary'" :size="ren.size||'mini'">{{ren.name}}</button>
  102. </template>
  103. </view>
  104. </view>
  105. </template>
  106. <template v-else-if="ite.type==='selection'">
  107. <view class="checkbox-item">
  108. <tableCheckbox @checkboxSelected="(e)=>checkboxSelected(e,item)" :cellData="item" :checked="item.checked"/>
  109. </view>
  110. </template>
  111. <template v-else-if="ite.type==='index'">
  112. {{index+1}}
  113. </template>
  114. <template v-else-if="ite.type==='img'">
  115. <view class="checkbox-item">
  116. <image
  117. @click.stop="previewImage(item,item[ite.name],index)"
  118. v-if="item[ite.name]"
  119. :show-menu-by-longpress="false"
  120. :src="item[ite.name]" style="width: 40px;height:30px; " mode="aspectFit"></image>
  121. <text v-else>{{ite.emptyString}}</text>
  122. </view>
  123. </template>
  124. <template v-else>
  125. <!-- {{ ite.filters?itemFilter(item,ite):(item[ite.name]==null||item[ite.name]==='')?ite.emptyString:item[ite.name] }}-->
  126. {{ ite.filters?itemFilter(item,ite):formatterAction(item,ite,index,i) }}
  127. </template>
  128. </view>
  129. </view>
  130. </view>
  131. </view>
  132. </scroll-view>
  133. <table-h5-summary
  134. :scrollbarSize="scrollbarSize"
  135. :data="data"
  136. :handleFooterTableScrollLeft="handleFooterTableScrollLeft"
  137. :headerFooterTableLeft="headerFooterTableLeft"
  138. v-if="showSummary"
  139. :showSummary="showSummary"
  140. :transColumns="transColumns"
  141. :border="border"
  142. :summary-method="summaryMethod"
  143. :sumText="sumText"
  144. :fixedLeftColumns="fixedLeftColumns"/>
  145. </view>
  146. <view class="zb-table-fixed-left"
  147. v-if="isFixedLeft"
  148. :style=" {height: `calc(100% - ${scrollbarSize}px)`}"
  149. >
  150. <template v-if="showHeader">
  151. <view class="zb-table-header" style="display: flex">
  152. <view class="item-tr"
  153. style=""
  154. @click.stop="rowClick(item,index)"
  155. v-for="(item,index) in fixedLeftColumns" :key="index">
  156. <view
  157. :style="{
  158. width:`${item.width?item.width:'100'}px`,
  159. borderRight:`${border?'1px solid #e8e8e8':''}`,
  160. borderTop:`${border?'1px solid #e8e8e8':''}`,
  161. textAlign:item.align||'left'
  162. }"
  163. @click.stop="sortAction(item,index)"
  164. class="item-th"
  165. >
  166. <template v-if="item.type==='selection'">
  167. <view class="checkbox-item">
  168. <tableCheckbox
  169. :indeterminate="indeterminate" :checked="checkedAll" @checkboxSelected="checkboxSelectedAll"></tableCheckbox>
  170. </view>
  171. </template>
  172. <template v-else>
  173. {{ item.label }}
  174. <view class="sorter-table" v-if="item.sorter">
  175. <view :class="['sorter-table-icon',item.sorterMode==='_asc'&&`sorting${item.sorterMode||''}`]"></view>
  176. <view :class="['sorter-table-icon',item.sorterMode==='_desc'&&`sorting${item.sorterMode||''}`]"></view>
  177. </view>
  178. </template>
  179. </view>
  180. </view>
  181. </view>
  182. </template>
  183. <scroll-view
  184. scroll-y="true"
  185. id="leftTableFixed"
  186. :upper-threshold="15"
  187. @scrolltoupper="(e)=>scrollToFixedLeft(e)"
  188. @scroll="leftFixedScrollAction"
  189. :scroll-top="leftFiexScrollTop"
  190. class="zb-table-body-inner"
  191. :style=" `height: calc(100% - ${showSummary?80:40}px)`">
  192. <view class="zb-table-fixed">
  193. <view class="zb-table-tbody">
  194. <view
  195. :class="['item-tr',stripe?(i % 2) != 0?'odd':'even':'',highlight&&isHighlight(ite,i)?'current-row':'']"
  196. v-for="(ite,i) in transData"
  197. @click.stop="rowClick(ite,i)"
  198. :key="ite.key"
  199. style="">
  200. <view class='item-td'
  201. :style="[{
  202. width:`${item.width?item.width:'100'}px`,
  203. borderRight:`${border?'1px solid #e8e8e8':''}`,
  204. textAlign:item.align||'left'
  205. },cellStyle&&getCellStyle(ite,item,i,index)]"
  206. :key="index"
  207. v-for="(item,index) in fixedLeftColumns">
  208. <template v-if="item.type==='selection'">
  209. <view class="checkbox-item">
  210. <tableCheckbox @checkboxSelected="(e)=>checkboxSelected(e,ite)" :cellData="ite" :checked="ite.checked"/>
  211. </view>
  212. </template>
  213. <template v-else-if="item.type==='index'">
  214. {{i+1}}
  215. </template>
  216. <template v-else>
  217. {{ite[item.name]||item.emptyString}}
  218. </template>
  219. </view>
  220. </view>
  221. </view>
  222. </view>
  223. </scroll-view>
  224. <table-side-summary
  225. :scrollbarSize="scrollbarSize"
  226. v-if="showSummary&&!(scrollbarSize>0)"
  227. :data="data"
  228. :showSummary="showSummary"
  229. :transColumns="transColumns"
  230. :border="border"
  231. :summary-method="summaryMethod"
  232. :sumText="sumText"
  233. :fixedLeftColumns="fixedLeftColumns"/>
  234. </view>
  235. </view>
  236. <zb-load-more v-if="isLoadMore&&!completeLoading"/>
  237. </view>
  238. <!-- #endif -->
  239. <!-- #ifndef H5 || APP-PLUS -->
  240. <view class="zb-table-applet">
  241. <view class="zb-table-content">
  242. <scroll-view
  243. <!-- #ifdef MP-ALIPAY -->
  244. @scroll="scrollAlipay"
  245. <!-- #endif -->
  246. @scrolltolower="scrolltolower"
  247. <!-- #ifdef MP-ALIPAY -->
  248. style=" height: 100%;overflow-x:scroll"
  249. <!-- #endif -->
  250. <!-- #ifndef MP-ALIPAY -->
  251. style=" height: 100%"
  252. <!-- #endif -->
  253. scroll-y="true"
  254. scroll-x="true">
  255. <view class="zb-table-scroll" >
  256. <template v-if="showHeader">
  257. <view class="zb-table-header top-header-uni" style="">
  258. <view class="zb-table-fixed" >
  259. <view class="zb-table-thead" style="position: relative;" >
  260. <view class="item-tr">
  261. <view
  262. @click.stop="sortAction(item,index)"
  263. :class="['item-th',index <fixedLeftColumns.length&&'zb-stick-side']"
  264. :style="{
  265. left:`${item.left}px`,
  266. width:`${item.width?item.width:'100'}px`,
  267. flex:index===transColumns.length-1?1:'none',
  268. minWidth:`${item.width?item.width:'100'}px`,
  269. borderRight:`${border?'1px solid #e8e8e8':''}`,
  270. borderTop:`${border?'1px solid #e8e8e8':''}`,
  271. textAlign:item.align||'left'
  272. }"
  273. v-for="(item,index) in transColumns" :key="index">
  274. <template v-if="item.type==='selection'">
  275. <view class="checkbox-item">
  276. <tableCheckbox
  277. :indeterminate="indeterminate" :checked="checkedAll" @checkboxSelected="checkboxSelectedAll"></tableCheckbox>
  278. </view>
  279. </template>
  280. <template v-else>
  281. {{ item.label||'' }}
  282. <view class="sorter-table" v-if="item.sorter">
  283. <view :class="['sorter-table-icon',item.sorterMode==='_asc'&&`sorting${item.sorterMode||''}`]"></view>
  284. <view :class="['sorter-table-icon',item.sorterMode==='_desc'&&`sorting${item.sorterMode||''}`]"></view>
  285. </view>
  286. </template>
  287. </view>
  288. </view>
  289. </view>
  290. </view>
  291. </view>
  292. </template>
  293. <template v-if="!data.length">
  294. <view class="no-data">暂无数据~~</view>
  295. </template>
  296. <view class="zb-table-fixed">
  297. <view class="zb-table-tbody">
  298. <view :class="['item-tr',highlight&&isHighlight(item,index)?'current-row':'']"
  299. @click.stop="rowClick(item,index)"
  300. v-for="(item,index) in transData" :key="item.key" >
  301. <view
  302. :style="[{
  303. left:`${ite.left}px`,
  304. width:`${ite.width?ite.width:'100'}px`,
  305. flex:i===transColumns.length-1?1:'none',
  306. minWidth:`${ite.width?ite.width:'100'}px`,
  307. borderRight:`${border?'1px solid #e8e8e8':''}`,
  308. textAlign:ite.align||'left',
  309. },getCellStyle(item,ite,index,i)]"
  310. :class="['item-td', i <fixedLeftColumns.length&&'zb-stick-side',stripe?(index % 2) != 0?'odd':'even':'']"
  311. v-for="(ite,i) in transColumns" :key="i">
  312. <template v-if="ite.type==='operation'">
  313. <view style="display: flex;align-items: center;height: 100%">
  314. <view
  315. v-for="ren,ind in permission(item,ite.renders,index)"
  316. :key="ind"
  317. @click.stop="$emit(ren.func,item,index)"
  318. :style="{
  319. display:'flex',
  320. alignItems: 'center',
  321. marginRight:ite.renders.length>1?'8px':'0'
  322. }">
  323. <template v-if="ren.type==='custom'">
  324. <view :class="ren.class||''" style="cursor: pointer">
  325. {{ren.name}}
  326. </view>
  327. </template>
  328. <template v-else>
  329. <button
  330. :class="ren.class||''"
  331. :type="ren.type||'primary'" :size="ren.size||'mini'">{{ren.name}}</button>
  332. </template>
  333. </view>
  334. </view>
  335. </template>
  336. <template v-else-if="ite.type==='selection'">
  337. <view class="checkbox-item">
  338. <tableCheckbox @checkboxSelected="(e)=>checkboxSelected(e,item)" :cellData="item" :checked="item.checked"/>
  339. </view>
  340. </template>
  341. <template v-else-if="ite.type==='img'">
  342. <image
  343. @click.stop="previewImage(item,item[ite.name],index)"
  344. v-if="item[ite.name]"
  345. :show-menu-by-longpress="false"
  346. :src="item[ite.name]" style="width: 40px;height:30px; " mode="aspectFit"></image>
  347. <text v-else>{{ite.emptyString}}</text>
  348. </template>
  349. <template v-else-if="ite.type==='index'">
  350. {{index+1}}
  351. </template>
  352. <template v-else>
  353. <!-- {{ ite.filters?itemFilter(item,ite):(item[ite.name]==null||item[ite.name]==='')?ite.emptyString:item[ite.name] }}-->
  354. {{ ite.filters?itemFilter(item,ite):formatterAction(item,ite,index,i) }}
  355. </template>
  356. </view>
  357. </view>
  358. </view>
  359. </view>
  360. <table-summary
  361. v-if="showSummary"
  362. :data="data"
  363. :showSummary="showSummary"
  364. :fixedLeftColumns="fixedLeftColumns"
  365. :transColumns="transColumns"
  366. :border="border"
  367. :summary-method="summaryMethod"
  368. :sumText="sumText"
  369. />
  370. </view>
  371. </scroll-view>
  372. </view>
  373. <zb-load-more v-if="isLoadMore&&!completeLoading"/>
  374. </view>
  375. <!-- #endif -->
  376. </template>
  377. <script>
  378. import TableCheckbox from './components/table-checkbox.vue'
  379. import TableSummary from "./components/table-summary.vue";
  380. import TableSideSummary from "./components/table-side-summary.vue";
  381. import TableH5Summary from './components/table-h5-summary'
  382. import ZbLoadMore from './components/zb-load-more'
  383. import {getScrollbarSize} from "./js/util";
  384. export default {
  385. components:{
  386. TableCheckbox,
  387. TableSummary,
  388. TableSideSummary,
  389. TableH5Summary,
  390. ZbLoadMore
  391. },
  392. props:{
  393. highlight:{
  394. type:Boolean,
  395. default:false
  396. },
  397. itemDate:{
  398. type:Object,
  399. default:()=>{}
  400. },
  401. columns:{
  402. type:Array,
  403. default:()=>[]
  404. },
  405. showSummary:{
  406. type:Boolean,
  407. default:false
  408. },
  409. isShowLoadMore:{
  410. type:Boolean,
  411. default:false
  412. },
  413. data:{
  414. type:[Object,Array],
  415. default:()=>[]
  416. },
  417. sumText:{
  418. type:String,
  419. default:'合计'
  420. },
  421. showHeader:{
  422. type:Boolean,
  423. default:true
  424. },
  425. border:{
  426. type:Boolean,
  427. default:false
  428. },
  429. stripe:{
  430. type:Boolean,
  431. default:true
  432. },
  433. fit:{
  434. type:Boolean,
  435. default:false
  436. },
  437. rowKey:[String, Function],
  438. summaryMethod:Function,
  439. pullUpLoading:Function,
  440. formatter:Function,
  441. cellStyle:Function,
  442. cellHeaderStyle:Function,
  443. permissionBtn:Function,
  444. },
  445. computed:{
  446. loadMoreHeight(){
  447. return this.isLoadMore?40:0
  448. },
  449. fixedLeftColumns(){
  450. let arr = []
  451. for(let i=0;i<this.columns.length;i++){
  452. let item = this.columns[i]
  453. if(item.fixed){
  454. arr.push(item)
  455. }else {
  456. break
  457. }
  458. }
  459. return arr
  460. },
  461. itemfilters(){
  462. return(item,ite)=>{
  463. if(item[ite.name]==null){
  464. return ite.emptyString
  465. }
  466. return item[ite.name]
  467. }
  468. },
  469. scrollbarSize(){
  470. // #ifdef H5
  471. return getScrollbarSize()
  472. // #endif
  473. // #ifndef H5
  474. return 0
  475. // #endif
  476. },
  477. isFixedLeft(){
  478. if(!this.columns.length){
  479. return false
  480. }
  481. if(!this.data.length){
  482. return false
  483. }
  484. let [firstArr] = this.columns
  485. return !!firstArr.fixed;
  486. },
  487. transColumns(){
  488. if(this.fit){
  489. this.columns.forEach(column=>{
  490. if(column.type==="operation"&&column.renders){
  491. let str = ''
  492. column.renders.map((item)=>{
  493. str+=item.name
  494. })
  495. column.width = this.getTextWidth(str)+column.renders.length*40
  496. }else if(column.type==="img"){
  497. }else if(column.type==="selection"){
  498. }else{
  499. let arr = [this.getTextWidth(column.label)]
  500. this.data.forEach(data=>{
  501. let str = (data[column.name]+'')
  502. if(str==='undefined'){
  503. arr.push(30)
  504. }else{
  505. let width = this.getTextWidth(str)
  506. arr.push(width)
  507. }
  508. })
  509. column.width = Math.max(...arr)+20
  510. }
  511. })
  512. }
  513. let number = 0
  514. this.columns.forEach((item,index)=>{
  515. if(item.type==="operation"&&item.renders&&!item.width){
  516. let str = ''
  517. item.renders.map((item)=>{
  518. str+=item.name
  519. })
  520. item.width = this.getTextWidth(str)+item.renders.length*40
  521. }
  522. if(item.fixed){
  523. if(index===0){
  524. item.left = 0
  525. number+=item.width
  526. }else {
  527. item.left = number
  528. number+=item.width
  529. }
  530. }
  531. item.emptyString = item.emptyString||'--'
  532. })
  533. return this.columns
  534. },
  535. transData(){
  536. let flag = this.columns.some(item=>item.type==='selection')
  537. this.data.forEach((item,index)=>{
  538. if(flag){
  539. if(item.checked){
  540. if(!this.selectArr.length){
  541. this.selectArr.push(item)
  542. }
  543. }
  544. }
  545. if(this.rowKey){
  546. if(typeof this.rowKey==='function'){
  547. item.key = Object.freeze(this.rowKey(item))||Date.now()
  548. }else {
  549. item.key = Object.freeze(item[this.rowKey])||Date.now()
  550. }
  551. }else {
  552. item.key = index
  553. }
  554. })
  555. if(flag&&this.data.length){
  556. let le = this.data.filter(item=>item.checked).length
  557. if(le){
  558. if(le===this.data.length){
  559. this.checkedAll = true
  560. }else {
  561. this.indeterminate = true
  562. }
  563. }
  564. }
  565. return this.data
  566. },
  567. isHighlight(){
  568. return (item,index)=>{
  569. if(this.rowKey){
  570. return item.key === this.currentRow['key']
  571. }else{
  572. return index === this.currentRowIndex
  573. }
  574. }
  575. },
  576. getHeaderCellStyle() {
  577. return (column, columnIndex,childIndex)=>{
  578. const cellStyle = this.cellHeaderStyle;
  579. if(typeof cellStyle==='function'){
  580. return cellStyle({ column, columnIndex})
  581. }
  582. return {}
  583. }
  584. },
  585. getCellStyle() {
  586. return (row, column, rowIndex, columnIndex)=>{
  587. const cellStyle = this.cellStyle;
  588. if(typeof cellStyle==='function'){
  589. return cellStyle({row, column, rowIndex, columnIndex})
  590. }
  591. return {}
  592. }
  593. },
  594. },
  595. data() {
  596. return {
  597. button:[],
  598. alipayScrollTop:0,
  599. alipayScrollOldTop:0,
  600. alipayFlag:false,
  601. bodyTableLeft:0,
  602. headerTableLeft:0,
  603. lastScrollLeft:0,
  604. isLoadMore:false,
  605. headerFooterTableLeft:0,
  606. leftFiexScrollTop:0,
  607. bodyScrollTop:0,
  608. currentDriver:null,
  609. currentDriver1:null,
  610. bodyTime:null,
  611. currentRowIndex:null,
  612. currentRow: {},
  613. bodyTime1:null,
  614. headerTime:null,
  615. debounceTime:null,
  616. operation:{},
  617. completedFlag:false,
  618. selectArr:[],
  619. indeterminate:false,
  620. checkedAll:false,
  621. completeLoading:false,
  622. aliTime:null,
  623. }
  624. },
  625. created(){
  626. },
  627. mounted(){
  628. },
  629. methods: {
  630. getMove(){
  631. },
  632. formatterAction(row,column,rowIndex,columnIndex){
  633. if(column.formatter&&typeof this.formatter==='function'){
  634. return this.formatter(row,column,rowIndex,columnIndex)
  635. }
  636. return (row[column.name]==null||row[column.name]==='')?column.emptyString:row[column.name]
  637. },
  638. permission(item,renders,index){
  639. if(this.permissionBtn&&typeof this.permissionBtn==='function'){
  640. return this.permissionBtn(item,renders,index)
  641. }
  642. return renders
  643. },
  644. pullUpCompleteLoading(type){
  645. this.isLoadMore = false
  646. if(type==='ok'){
  647. this.completeLoading = true
  648. }
  649. },
  650. scrollAlipay(e){
  651. if(!this.alipayScrollOldTop){
  652. this.alipayScrollOldTop = e.detail.scrollTop
  653. }
  654. this.aliTime&&clearTimeout(this.aliTime)
  655. this.aliTime = setTimeout(()=>{
  656. if(this.alipayFlag&&e.detail.scrollTop>this.alipayScrollOldTop){
  657. this.pullLoad()
  658. }
  659. this.alipayFlag = false
  660. this.alipayScrollOldTop = null
  661. },500)
  662. },
  663. pullLoad(){
  664. if(this.isShowLoadMore){
  665. this.isLoadMore = true
  666. this.$emit('pullUpLoading')
  667. let that = this
  668. this.pullUpLoading&&this.pullUpLoading.call(this.$parent.$parent, (type)=>{
  669. that.isLoadMore = false
  670. if(type==='ok'){
  671. that.completeLoading=true
  672. }
  673. })
  674. }
  675. },
  676. scrolltolower(e){
  677. this.alipayFlag = true
  678. if(e.detail.direction==='bottom'){
  679. this.pullLoad()
  680. }
  681. // this.pullUpLoading.call(this.$parent)
  682. },
  683. previewImage(item,url,current){
  684. uni.previewImage({
  685. current,
  686. urls:[url]
  687. })
  688. },
  689. resetHighlight(){
  690. this.currentRowIndex = null
  691. this.currentRow = {}
  692. },
  693. rowClick(row,index){
  694. if(this.highlight){
  695. this.currentRowIndex = index
  696. this.currentRow = row
  697. this.$emit('currentChange',row,index)
  698. }
  699. this.$emit('rowClick',row,index)
  700. },
  701. checkboxSelectedAll(e){
  702. this.indeterminate = false
  703. if(e.checked){
  704. this.selectArr = []
  705. this.checkedAll = true
  706. this.data.forEach(item=>{
  707. // this.$set(item,'checked',true)
  708. item.checked = true
  709. this.selectArr.push(item)
  710. })
  711. }else{
  712. this.checkedAll = false
  713. this.data.forEach(item=>{
  714. this.$set(item,'checked',false)
  715. })
  716. this.selectArr = []
  717. }
  718. // #ifndef H5 || APP-PLUS
  719. this.$forceUpdate()
  720. // #endif
  721. this.$emit('toggleAllSelection',e.checked,this.selectArr)
  722. },
  723. checkboxSelected(e,item){
  724. // #ifdef H5 || APP-PLUS
  725. this.$set(item,'checked',e.checked)
  726. // #endif
  727. // #ifndef H5 || APP-PLUS
  728. this.data.forEach(item=>{
  729. if(item.key===e.data.key){
  730. item.checked = e.checked
  731. }
  732. })
  733. // #endif
  734. item.checked = e.checked
  735. e.data.checked = e.checked
  736. if(e.checked){
  737. this.selectArr.push(e.data)
  738. }else{
  739. this.selectArr = this.selectArr.filter(item=>item.key!==e.data.key)
  740. }
  741. if(this.selectArr.length===this.transData.length){
  742. this.indeterminate = false
  743. this.checkedAll = true
  744. }else{
  745. this.indeterminate = true
  746. this.checkedAll = false
  747. }
  748. if(!this.selectArr.length){
  749. this.checkedAll = false
  750. this.indeterminate = false
  751. }
  752. // #ifndef H5 || APP-PLUS
  753. this.$forceUpdate()
  754. // #endif
  755. this.$emit('toggleRowSelection',e.checked,this.selectArr)
  756. },
  757. itemFilter(item,ite){
  758. if(ite.filters&&ite.name){
  759. let key = item[ite.name]
  760. return ite.filters[key]||''
  761. }
  762. return item[ite.name]||ite.emptyString
  763. },
  764. // 默认字体为微软雅黑 Microsoft YaHei,字体大小为 14px
  765. getTextWidth(str) {
  766. if(str.length<3){
  767. return 40
  768. }
  769. let regx = /^[0-9]+.?[0-9]*$/
  770. let flexWidth = 0
  771. for (const char of str) {
  772. if ((char >= 'A' && char <= 'Z') || (char >= 'a' && char <= 'z')) {
  773. // 如果是英文字符,为字符分配8个单位宽度
  774. flexWidth += 10
  775. } else if (char >= '\u4e00' && char <= '\u9fa5') {
  776. // 如果是中文字符,为字符分配15个单位宽度
  777. flexWidth += 15
  778. } else if(regx.test(char)){
  779. flexWidth += 9
  780. }else {
  781. // 其他种类字符,为字符分配8个单位宽度
  782. flexWidth += 7
  783. }
  784. }
  785. return flexWidth
  786. },
  787. width(item){
  788. return `${item.width?item.width:'100'}px`
  789. },
  790. showStripe(index){
  791. if(this.currentDriver)return
  792. if(this.stripe){
  793. return (index % 2) != 0?'odd':'even'
  794. }else{
  795. return ''
  796. }
  797. },
  798. //验证字符串是否是数字
  799. checkNumber(theObj) {
  800. var reg = /^[0-9]+.?[0-9]*$/;
  801. if (reg.test(theObj)) {
  802. return true;
  803. }
  804. return false;
  805. },
  806. isDate(data){
  807. if(isNaN(data)&&!isNaN(Date.parse(data))){
  808. return true
  809. }
  810. return false
  811. },
  812. sortAction(item,index){
  813. if(!item.sorter){return false}
  814. this.$set(item,'sorterMode',item.sorterMode==='_asc'?'_desc':'_asc')
  815. if(item.sorter==='custom'){
  816. this.$emit('sort-change',item,item.sorterMode.replace('_',''),index)
  817. }else {
  818. this.sortData(item)
  819. }
  820. // #ifndef H5 || APP-PLUS
  821. this.$forceUpdate()
  822. // #endif
  823. },
  824. sortData(item){
  825. let key = item.name
  826. if(item.sorterMode==='_asc'){
  827. this.data.sort((a,b)=>{
  828. if(this.checkNumber(a[key])){
  829. return a[key]-b[key]
  830. }
  831. if(this.isDate(a[key])){
  832. let a1 = new Date(a[key]).getTime()
  833. let b1 = new Date(b[key]).getTime()
  834. return a1-b1
  835. }
  836. })
  837. }else {
  838. this.data.sort((a,b)=>{
  839. if(this.checkNumber(a[key])){
  840. return b[key]-a[key]
  841. }
  842. if(this.isDate(a[key])){
  843. let a1 = new Date(a[key]).getTime()
  844. let b1 = new Date(b[key]).getTime()
  845. return b1-a1
  846. }
  847. })
  848. }
  849. },
  850. throttle(method,delay=60){
  851. let time = null
  852. return (...args)=>{
  853. if(!time){
  854. time = setTimeout(()=>{
  855. method(...args)
  856. time = null;
  857. },delay)
  858. }
  859. }
  860. },
  861. debounce(method,delay=1000){
  862. return (...args)=>{
  863. this.debounceTime&&clearTimeout(this.debounceTime)
  864. this.debounceTime = setTimeout(()=>{
  865. method(...args)
  866. },delay)
  867. }
  868. },
  869. handleBodyScroll(e){
  870. if(this.currentDriver&&this.currentDriver!==e.currentTarget.id)return
  871. this.currentDriver = e.currentTarget.id
  872. this.headerTableLeft = e.detail.scrollLeft
  873. this.headerFooterTableLeft = e.detail.scrollLeft
  874. this.leftFiexScrollTop = e.detail.scrollTop
  875. this.bodyTime&&clearTimeout(this.bodyTime)
  876. this.bodyTime = setTimeout(()=>{
  877. this.currentDriver=null
  878. },200)
  879. },
  880. leftFixedScrollAction(e){
  881. if(this.currentDriver&&this.currentDriver!==e.currentTarget.id)return
  882. this.currentDriver = e.currentTarget.id
  883. this.bodyScrollTop = e.detail.scrollTop
  884. this.bodyTime&&clearTimeout(this.bodyTime)
  885. this.bodyTime = setTimeout(()=>{
  886. this.currentDriver=null
  887. },200)
  888. },
  889. scrollToLeft(e){
  890. if(this.currentDriver1&&this.currentDriver1!==e.currentTarget.id)return
  891. this.currentDriver1 = e.currentTarget.id
  892. if(e.detail.direction==='left'&&this.headerTableLeft<10){
  893. this.headerTableLeft = 0
  894. }else if(e.detail.direction==='top'&&this.leftFiexScrollTop<10){
  895. this.leftFiexScrollTop = 0
  896. }
  897. this.bodyTime&&clearTimeout(this.bodyTime)
  898. this.bodyTime = setTimeout(()=>{
  899. this.currentDriver1=null
  900. },200)
  901. },
  902. scrollToFixedLeft(e){
  903. if(this.currentDriver1&&this.currentDriver1!==e.currentTarget.id)return
  904. this.currentDriver1 = e.currentTarget.id
  905. if(e.detail.direction==='top'&&this.bodyScrollTop<10){
  906. this.bodyScrollTop = 0
  907. }
  908. this.bodyTime&&clearTimeout(this.bodyTime)
  909. this.bodyTime = setTimeout(()=>{
  910. this.currentDriver1=null
  911. },200)
  912. },
  913. handleTableScrollLeft(e,type){
  914. if(this.currentDriver&&this.currentDriver!==e.currentTarget.id)return
  915. this.currentDriver = e.currentTarget.id
  916. this.bodyTableLeft = e.detail.scrollLeft
  917. this.headerFooterTableLeft = e.detail.scrollLeft
  918. this.bodyTime&&clearTimeout(this.bodyTime)
  919. this.bodyTime = setTimeout(()=>{
  920. this.currentDriver=null
  921. },200)
  922. },
  923. handleFooterTableScrollLeft(e){
  924. if(this.currentDriver&&this.currentDriver!==e.currentTarget.id)return
  925. this.currentDriver = e.currentTarget.id
  926. this.bodyTableLeft = e.detail.scrollLeft
  927. this.headerTableLeft = e.detail.scrollLeft
  928. this.bodyTime&&clearTimeout(this.bodyTime)
  929. this.bodyTime = setTimeout(()=>{
  930. this.currentDriver=null
  931. },200)
  932. }
  933. }
  934. }
  935. </script>
  936. <style lang="scss">
  937. .zb-table-fixed-left{
  938. /*去除左边滚动条 */
  939. scroll-view ::-webkit-scrollbar {
  940. display: none !important;
  941. width: 0 !important;
  942. height: 0 !important;
  943. -webkit-appearance: none;
  944. background: transparent;
  945. }
  946. }
  947. .zb-table-header{
  948. ///*去除头部滚动条 */
  949. scroll-view ::-webkit-scrollbar {
  950. display: none !important;
  951. width: 0 !important;
  952. height: 0 !important;
  953. -webkit-appearance: none;
  954. background: transparent;
  955. }
  956. }
  957. </style>
  958. <style lang="scss" scoped>
  959. .sorter-table{
  960. position: absolute;
  961. right: 6px;
  962. top:50%;
  963. transform:translateY(-50%);
  964. .sorter-table-icon{
  965. width: 0;
  966. height: 0;
  967. color: #dcdcdc;
  968. border-right: 4px solid transparent;
  969. border-left: 4px solid transparent;
  970. }
  971. .sorter-table-icon:first-child{
  972. border-bottom: 5px solid currentColor;
  973. }
  974. .sorter-table-icon:last-child{
  975. margin-top: 1.5px;
  976. border-top: 5px solid currentColor;
  977. }
  978. .sorting_desc{
  979. color: #2979ff;
  980. }
  981. .sorting_asc{
  982. color: #2979ff;
  983. }
  984. }
  985. .checkbox-item{
  986. display: flex;align-items: center;justify-content: center;width: 100%;height: 100%
  987. }
  988. .no-data{
  989. width: 100%;
  990. height: 80rpx;
  991. display: flex;
  992. justify-content: center;
  993. align-items: center;
  994. border-bottom: 1px solid #e8e8e8;
  995. }
  996. .item-th{
  997. position: relative;
  998. flex-shrink: 0;
  999. width: 100px;
  1000. overflow-wrap: break-word;
  1001. border-bottom: 1px solid #e8e8e8;
  1002. transition: background 0.3s;
  1003. padding-right: 4px;
  1004. word-break:keep-all; /* 不换行 */
  1005. white-space:nowrap; /* 不换行 */
  1006. overflow:hidden; /* 内容超出宽度时隐藏超出部分的内容 */
  1007. text-overflow:ellipsis; /* 当对象内文本溢出时显示省略标记(...) ;需与overflow:hidden;一起使用。*/
  1008. overflow-wrap: break-word;
  1009. }
  1010. .zb-table{
  1011. height: 100%;
  1012. overflow: hidden;
  1013. width: 100%;
  1014. display: flex;
  1015. flex-direction: column;
  1016. font-size: 12px;
  1017. position: relative;
  1018. .zb-table-content{
  1019. //height: 100%;
  1020. //flex: 1;
  1021. position: relative;
  1022. overflow: hidden;
  1023. }
  1024. .zb-table-fixed{
  1025. min-width: 100%;
  1026. }
  1027. .zb-table-body{
  1028. position: relative;
  1029. background: #fff;
  1030. transition: opacity 0.3s;
  1031. }
  1032. .item-tr{
  1033. display: flex;
  1034. //height: 41px;
  1035. }
  1036. .item-td{
  1037. flex-shrink: 0;
  1038. width: 100px;
  1039. padding-left: 4px;
  1040. height: 40px;
  1041. line-height: 40px;
  1042. padding-right: 4px;
  1043. box-sizing: border-box;
  1044. word-break:keep-all; /* 不换行 */
  1045. white-space:nowrap; /* 不换行 */
  1046. overflow:hidden; /* 内容超出宽度时隐藏超出部分的内容 */
  1047. text-overflow:ellipsis; /* 当对象内文本溢出时显示省略标记(...) ;需与overflow:hidden;一起使用。*/
  1048. overflow-wrap: break-word;
  1049. border-bottom: 1px solid #e8e8e8;
  1050. //transition: background 0.3s;
  1051. }
  1052. .zb-table-fixed-left .zb-table-header{
  1053. overflow-y: hidden;
  1054. }
  1055. .zb-table-header {
  1056. overflow: hidden;
  1057. background: #fafafa;
  1058. .item-th{
  1059. padding-left: 4px;
  1060. line-height: 39px;
  1061. height: 40px;
  1062. //display: flex;
  1063. //align-items: center;
  1064. box-sizing: border-box;
  1065. }
  1066. }
  1067. .zb-table-fixed-left .zb-table-fixed{
  1068. background: #fff;
  1069. }
  1070. .zb-table-fixed-right .zb-table-fixed{
  1071. background: #fff;
  1072. }
  1073. .zb-table-body-inner{
  1074. height: 100%;
  1075. // overflow: scroll;
  1076. }
  1077. .zb-table-fixed-left{
  1078. position: absolute;
  1079. top: 0;
  1080. z-index: 1;
  1081. overflow: hidden;
  1082. border-radius: 0;
  1083. height: 100%;
  1084. //transition: box-shadow 0.3s ease;
  1085. }
  1086. .odd{
  1087. background-color:rgba(249,249,249,0.6);
  1088. //height: 100%;
  1089. width: 100%;
  1090. }
  1091. .even{
  1092. background-color:white ;
  1093. //height: 100%;
  1094. width: 100%;
  1095. }
  1096. }
  1097. .scroll-left-fixed{
  1098. .zb-table-fixed-left {
  1099. left: 0;
  1100. box-shadow: 6px 0 6px -4px #ccc;
  1101. }
  1102. }
  1103. .zb-table-applet{
  1104. height: 100%;
  1105. //overflow: hidden;
  1106. width: 100%;
  1107. position: relative;
  1108. display: flex;
  1109. flex-direction: column;
  1110. font-size: 12px;
  1111. .zb-table-content{
  1112. //height: 100%;
  1113. flex: 1;
  1114. overflow: hidden;
  1115. position: relative;
  1116. }
  1117. .zb-table-fixed{
  1118. min-width: 100%;
  1119. width: fit-content;
  1120. }
  1121. .zb-table-body{
  1122. position: relative;
  1123. background: #fff;
  1124. transition: opacity 0.3s;
  1125. }
  1126. .item-tr{
  1127. display: flex;
  1128. //height: 41px;
  1129. }
  1130. .item-td{
  1131. flex-shrink: 0;
  1132. width: 100px;
  1133. padding-left: 4px;
  1134. height: 40px;
  1135. line-height: 40px;
  1136. padding-right:4px;
  1137. box-sizing: border-box;
  1138. word-break:keep-all; /* 不换行 */
  1139. white-space:nowrap; /* 不换行 */
  1140. overflow:hidden; /* 内容超出宽度时隐藏超出部分的内容 */
  1141. text-overflow:ellipsis; /* 当对象内文本溢出时显示省略标记(...) ;需与overflow:hidden;一起使用。*/
  1142. overflow-wrap: break-word;
  1143. border-bottom: 1px solid #e8e8e8;
  1144. //transition: background 0.3s;
  1145. }
  1146. .zb-table-header {
  1147. //overflow: hidden;
  1148. position: sticky;
  1149. top: 0;
  1150. z-index: 2;
  1151. //width: fit-content;
  1152. .item-th{
  1153. padding-left: 4px;
  1154. line-height: 39px;
  1155. height: 40px;
  1156. box-sizing: border-box;
  1157. background: #fafafa;
  1158. }
  1159. .zb-stick-side{
  1160. position: sticky;
  1161. top: 0;
  1162. left: 0;
  1163. z-index: 2;
  1164. //border-right: solid 1rpx #dbdbdb;
  1165. box-sizing: border-box;
  1166. background: #fafafa;
  1167. //box-shadow: 6px 0 6px -4px #ccc;
  1168. }
  1169. }
  1170. .zb-table-fixed-left .zb-table-fixed{
  1171. background: #fff;
  1172. }
  1173. .zb-table-fixed-right .zb-table-fixed{
  1174. background: #fff;
  1175. }
  1176. .zb-table-fixed-header .zb-table-body-inner{
  1177. height: 100%;
  1178. // overflow: scroll;
  1179. }
  1180. .zb-table-fixed-left{
  1181. position: absolute;
  1182. top: 0;
  1183. z-index: 1;
  1184. overflow: hidden;
  1185. border-radius: 0;
  1186. height: 100%;
  1187. //transition: box-shadow 0.3s ease;
  1188. }
  1189. .scroll-left-fixed{
  1190. .zb-table-fixed-left {
  1191. left: 0;
  1192. box-shadow: 6px 0 6px -4px #ccc;
  1193. }
  1194. }
  1195. .odd{
  1196. background-color:rgba(249,249,249,0.6);
  1197. //height: 100%;
  1198. width: 100%;
  1199. }
  1200. .even{
  1201. background-color:white ;
  1202. //height: 100%;
  1203. width: 100%;
  1204. }
  1205. .zb-table-tbody {
  1206. .zb-stick-side{
  1207. position: sticky;
  1208. left: 0;
  1209. z-index: 1;
  1210. box-sizing: border-box;
  1211. background:white;
  1212. //box-shadow: 6px 0 6px -2px #ccc;
  1213. }
  1214. .odd{
  1215. background:#f9f9f9;
  1216. //height: 100%;
  1217. width: 100%;
  1218. }
  1219. .even{
  1220. background:white ;
  1221. //height: 100%;
  1222. width: 100%;
  1223. }
  1224. }
  1225. .current-row{
  1226. .item-td{
  1227. background-color: #ecf5ff;
  1228. }
  1229. }
  1230. }
  1231. .current-row{
  1232. .item-td{
  1233. background-color: #ecf5ff;
  1234. }
  1235. }
  1236. .zb-table-header{
  1237. height: 40px;
  1238. }
  1239. </style>