zb-tables.vue 40 KB

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