uni-table.vue 10 KB

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