zb-tables.vue 39 KB

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