uni-table.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <template>
  2. <view class="uni-table-scroll" :class="{ 'table--border': border, 'border-none': !noData }">
  3. <!-- #ifdef H5 -->
  4. <table class="uni-table" border="0" cellpadding="0" cellspacing="0" :class="{ 'table--stripe': stripe }" :style="{ 'min-width': minWidth + 'px' }">
  5. <slot></slot>
  6. <tr v-if="noData" class="uni-table-loading">
  7. <td class="uni-table-text" :class="{ 'empty-border': border }">{{ emptyText }}</td>
  8. </tr>
  9. <view v-if="loading" class="uni-table-mask" :class="{ 'empty-border': border }"><div class="uni-table--loader"></div></view>
  10. </table>
  11. <!-- #endif -->
  12. <!-- #ifndef H5 -->
  13. <view class="uni-table" :style="{ 'min-width': minWidth + 'px' }" :class="{ 'table--stripe': stripe }">
  14. <slot></slot>
  15. <view v-if="noData" class="uni-table-loading">
  16. <view class="uni-table-text" :class="{ 'empty-border': border }">{{ emptyText }}</view>
  17. </view>
  18. <view v-if="loading" class="uni-table-mask" :class="{ 'empty-border': border }"><div class="uni-table--loader"></div></view>
  19. </view>
  20. <!-- #endif -->
  21. </view>
  22. </template>
  23. <script>
  24. /**
  25. * Table 表格
  26. * @description 用于展示多条结构类似的数据
  27. * @tutorial https://ext.dcloud.net.cn/plugin?id=3270
  28. * @property {Boolean} border 是否带有纵向边框
  29. * @property {Boolean} stripe 是否显示斑马线
  30. * @property {Boolean} type 是否开启多选
  31. * @property {String} emptyText 空数据时显示的文本内容
  32. * @property {Boolean} loading 显示加载中
  33. * @event {Function} selection-change 开启多选时,当选择项发生变化时会触发该事件
  34. */
  35. export default {
  36. name: 'uniTable',
  37. options: {
  38. // #ifdef MP-TOUTIAO
  39. virtualHost: false,
  40. // #endif
  41. // #ifndef MP-TOUTIAO
  42. virtualHost: true
  43. // #endif
  44. },
  45. emits:['selection-change'],
  46. props: {
  47. data: {
  48. type: Array,
  49. default() {
  50. return []
  51. }
  52. },
  53. // 是否有竖线
  54. border: {
  55. type: Boolean,
  56. default: false
  57. },
  58. // 是否显示斑马线
  59. stripe: {
  60. type: Boolean,
  61. default: false
  62. },
  63. // 多选
  64. type: {
  65. type: String,
  66. default: ''
  67. },
  68. // 没有更多数据
  69. emptyText: {
  70. type: String,
  71. default: '没有更多数据'
  72. },
  73. loading: {
  74. type: Boolean,
  75. default: false
  76. },
  77. rowKey: {
  78. type: String,
  79. default: ''
  80. }
  81. },
  82. data() {
  83. return {
  84. noData: true,
  85. minWidth: 0,
  86. multiTableHeads: []
  87. }
  88. },
  89. watch: {
  90. loading(val) {},
  91. data(newVal) {
  92. let theadChildren = this.theadChildren
  93. let rowspan = 1
  94. if (this.theadChildren) {
  95. rowspan = this.theadChildren.rowspan
  96. }
  97. // this.trChildren.length - rowspan
  98. this.noData = false
  99. // this.noData = newVal.length === 0
  100. }
  101. },
  102. created() {
  103. // 定义tr的实例数组
  104. this.trChildren = []
  105. this.thChildren = []
  106. this.theadChildren = null
  107. this.backData = []
  108. this.backIndexData = []
  109. },
  110. methods: {
  111. isNodata() {
  112. let theadChildren = this.theadChildren
  113. let rowspan = 1
  114. if (this.theadChildren) {
  115. rowspan = this.theadChildren.rowspan
  116. }
  117. this.noData = this.trChildren.length - rowspan <= 0
  118. },
  119. /**
  120. * 选中所有
  121. */
  122. selectionAll() {
  123. let startIndex = 1
  124. let theadChildren = this.theadChildren
  125. if (!this.theadChildren) {
  126. theadChildren = this.trChildren[0]
  127. } else {
  128. startIndex = theadChildren.rowspan - 1
  129. }
  130. let isHaveData = this.data && this.data.length > 0
  131. theadChildren.checked = true
  132. theadChildren.indeterminate = false
  133. this.trChildren.forEach((item, index) => {
  134. if (!item.disabled) {
  135. item.checked = true
  136. if (isHaveData && item.keyValue) {
  137. const row = this.data.find(v => v[this.rowKey] === item.keyValue)
  138. if (!this.backData.find(v => v[this.rowKey] === row[this.rowKey])) {
  139. this.backData.push(row)
  140. }
  141. }
  142. if (index > (startIndex - 1) && this.backIndexData.indexOf(index - startIndex) === -1) {
  143. this.backIndexData.push(index - startIndex)
  144. }
  145. }
  146. })
  147. // this.backData = JSON.parse(JSON.stringify(this.data))
  148. this.$emit('selection-change', {
  149. detail: {
  150. value: this.backData,
  151. index: this.backIndexData
  152. }
  153. })
  154. },
  155. /**
  156. * 用于多选表格,切换某一行的选中状态,如果使用了第二个参数,则是设置这一行选中与否(selected 为 true 则选中)
  157. */
  158. toggleRowSelection(row, selected) {
  159. // if (!this.theadChildren) return
  160. row = [].concat(row)
  161. this.trChildren.forEach((item, index) => {
  162. // if (item.keyValue) {
  163. const select = row.findIndex(v => {
  164. //
  165. if (typeof v === 'number') {
  166. return v === index - 1
  167. } else {
  168. return v[this.rowKey] === item.keyValue
  169. }
  170. })
  171. let ischeck = item.checked
  172. if (select !== -1) {
  173. if (typeof selected === 'boolean') {
  174. item.checked = selected
  175. } else {
  176. item.checked = !item.checked
  177. }
  178. if (ischeck !== item.checked) {
  179. this.check(item.rowData||item, item.checked, item.rowData?item.keyValue:null, true)
  180. }
  181. }
  182. // }
  183. })
  184. this.$emit('selection-change', {
  185. detail: {
  186. value: this.backData,
  187. index:this.backIndexData
  188. }
  189. })
  190. },
  191. /**
  192. * 用于多选表格,清空用户的选择
  193. */
  194. clearSelection() {
  195. let theadChildren = this.theadChildren
  196. if (!this.theadChildren) {
  197. theadChildren = this.trChildren[0]
  198. }
  199. // if (!this.theadChildren) return
  200. theadChildren.checked = false
  201. theadChildren.indeterminate = false
  202. this.trChildren.forEach(item => {
  203. // if (item.keyValue) {
  204. item.checked = false
  205. // }
  206. })
  207. this.backData = []
  208. this.backIndexData = []
  209. this.$emit('selection-change', {
  210. detail: {
  211. value: [],
  212. index: []
  213. }
  214. })
  215. },
  216. /**
  217. * 用于多选表格,切换所有行的选中状态
  218. */
  219. toggleAllSelection() {
  220. let list = []
  221. let startIndex = 1
  222. let theadChildren = this.theadChildren
  223. if (!this.theadChildren) {
  224. theadChildren = this.trChildren[0]
  225. } else {
  226. startIndex = theadChildren.rowspan - 1
  227. }
  228. this.trChildren.forEach((item, index) => {
  229. if (!item.disabled) {
  230. if (index > (startIndex - 1) ) {
  231. list.push(index-startIndex)
  232. }
  233. }
  234. })
  235. this.toggleRowSelection(list)
  236. },
  237. /**
  238. * 选中\取消选中
  239. * @param {Object} child
  240. * @param {Object} check
  241. * @param {Object} rowValue
  242. */
  243. check(child, check, keyValue, emit) {
  244. let theadChildren = this.theadChildren
  245. if (!this.theadChildren) {
  246. theadChildren = this.trChildren[0]
  247. }
  248. let childDomIndex = this.trChildren.findIndex((item, index) => child === item)
  249. if(childDomIndex < 0){
  250. childDomIndex = this.data.findIndex(v=>v[this.rowKey] === keyValue) + 1
  251. }
  252. const dataLen = this.trChildren.filter(v => !v.disabled && v.keyValue).length
  253. if (childDomIndex === 0) {
  254. check ? this.selectionAll() : this.clearSelection()
  255. return
  256. }
  257. if (check) {
  258. if (keyValue) {
  259. this.backData.push(child)
  260. }
  261. this.backIndexData.push(childDomIndex - 1)
  262. } else {
  263. const index = this.backData.findIndex(v => v[this.rowKey] === keyValue)
  264. const idx = this.backIndexData.findIndex(item => item === childDomIndex - 1)
  265. if (keyValue) {
  266. this.backData.splice(index, 1)
  267. }
  268. this.backIndexData.splice(idx, 1)
  269. }
  270. const domCheckAll = this.trChildren.find((item, index) => index > 0 && !item.checked && !item.disabled)
  271. if (!domCheckAll) {
  272. theadChildren.indeterminate = false
  273. theadChildren.checked = true
  274. } else {
  275. theadChildren.indeterminate = true
  276. theadChildren.checked = false
  277. }
  278. if (this.backIndexData.length === 0) {
  279. theadChildren.indeterminate = false
  280. }
  281. if (!emit) {
  282. this.$emit('selection-change', {
  283. detail: {
  284. value: this.backData,
  285. index: this.backIndexData
  286. }
  287. })
  288. }
  289. }
  290. }
  291. }
  292. </script>
  293. <style lang="scss">
  294. $border-color: #ebeef5;
  295. .uni-table-scroll {
  296. width: 100%;
  297. /* #ifndef APP-NVUE */
  298. overflow-x: auto;
  299. /* #endif */
  300. }
  301. .uni-table {
  302. position: relative;
  303. width: 100%;
  304. border-radius: 5px;
  305. // box-shadow: 0px 0px 3px 1px rgba(0, 0, 0, 0.1);
  306. background-color: #fff;
  307. /* #ifndef APP-NVUE */
  308. box-sizing: border-box;
  309. display: table;
  310. overflow-x: auto;
  311. ::v-deep .uni-table-tr:nth-child(n + 2) {
  312. &:hover {
  313. background-color: #f5f7fa;
  314. }
  315. }
  316. ::v-deep .uni-table-thead {
  317. .uni-table-tr {
  318. // background-color: #f5f7fa;
  319. &:hover {
  320. background-color:#fafafa;
  321. }
  322. }
  323. }
  324. /* #endif */
  325. }
  326. .table--border {
  327. border: 1px $border-color solid;
  328. border-right: none;
  329. }
  330. .border-none {
  331. /* #ifndef APP-NVUE */
  332. border-bottom: none;
  333. /* #endif */
  334. }
  335. .table--stripe {
  336. /* #ifndef APP-NVUE */
  337. ::v-deep .uni-table-tr:nth-child(2n + 3) {
  338. background-color: #fafafa;
  339. }
  340. /* #endif */
  341. }
  342. /* 表格加载、无数据样式 */
  343. .uni-table-loading {
  344. position: relative;
  345. /* #ifndef APP-NVUE */
  346. display: table-row;
  347. /* #endif */
  348. height: 50px;
  349. line-height: 50px;
  350. overflow: hidden;
  351. box-sizing: border-box;
  352. }
  353. .empty-border {
  354. border-right: 1px $border-color solid;
  355. }
  356. .uni-table-text {
  357. position: absolute;
  358. right: 0;
  359. left: 0;
  360. text-align: center;
  361. font-size: 14px;
  362. color: #999;
  363. }
  364. .uni-table-mask {
  365. position: absolute;
  366. top: 0;
  367. bottom: 0;
  368. left: 0;
  369. right: 0;
  370. background-color: rgba(255, 255, 255, 0.8);
  371. z-index: 99;
  372. /* #ifndef APP-NVUE */
  373. display: flex;
  374. margin: auto;
  375. transition: all 0.5s;
  376. /* #endif */
  377. justify-content: center;
  378. align-items: center;
  379. }
  380. .uni-table--loader {
  381. width: 30px;
  382. height: 30px;
  383. border: 2px solid #aaa;
  384. // border-bottom-color: transparent;
  385. border-radius: 50%;
  386. /* #ifndef APP-NVUE */
  387. animation: 2s uni-table--loader linear infinite;
  388. /* #endif */
  389. position: relative;
  390. }
  391. @keyframes uni-table--loader {
  392. 0% {
  393. transform: rotate(360deg);
  394. }
  395. 10% {
  396. border-left-color: transparent;
  397. }
  398. 20% {
  399. border-bottom-color: transparent;
  400. }
  401. 30% {
  402. border-right-color: transparent;
  403. }
  404. 40% {
  405. border-top-color: transparent;
  406. }
  407. 50% {
  408. transform: rotate(0deg);
  409. }
  410. 60% {
  411. border-top-color: transparent;
  412. }
  413. 70% {
  414. border-left-color: transparent;
  415. }
  416. 80% {
  417. border-bottom-color: transparent;
  418. }
  419. 90% {
  420. border-right-color: transparent;
  421. }
  422. 100% {
  423. transform: rotate(-360deg);
  424. }
  425. }
  426. </style>