uni-table.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. :style="{ 'min-width': minWidth + 'px' }">
  6. <slot></slot>
  7. <view v-if="noData" class="uni-table-loading">
  8. <view class="uni-table-text" :class="{ 'empty-border': border }">{{ emptyText }}</view>
  9. </view>
  10. <view v-if="loading" class="uni-table-mask" :class="{ 'empty-border': border }">
  11. <div class="uni-table--loader"></div>
  12. </view>
  13. </table>
  14. <!-- #endif -->
  15. <!-- #ifndef H5 -->
  16. <view class="uni-table" :style="{ 'min-width': minWidth + 'px' }" :class="{ 'table--stripe': stripe }">
  17. <slot></slot>
  18. <view v-if="noData" class="uni-table-loading">
  19. <view class="uni-table-text" :class="{ 'empty-border': border }">{{ emptyText }}</view>
  20. </view>
  21. <view v-if="loading" class="uni-table-mask" :class="{ 'empty-border': border }">
  22. <div class="uni-table--loader"></div>
  23. </view>
  24. </view>
  25. <!-- #endif -->
  26. </view>
  27. </template>
  28. <script>
  29. /**
  30. * Table 表格
  31. * @description 用于展示多条结构类似的数据
  32. * @tutorial https://ext.dcloud.net.cn/plugin?id=3270
  33. * @property {Boolean} border 是否带有纵向边框
  34. * @property {Boolean} stripe 是否显示斑马线
  35. * @property {Boolean} type 是否开启多选
  36. * @property {String} emptyText 空数据时显示的文本内容
  37. * @property {Boolean} loading 显示加载中
  38. * @event {Function} selection-change 开启多选时,当选择项发生变化时会触发该事件
  39. */
  40. export default {
  41. name: 'uniTable',
  42. options: {
  43. virtualHost: true
  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. height: 100%;
  298. /* #ifndef APP-NVUE */
  299. overflow-x: auto;
  300. /* #endif */
  301. }
  302. .uni-table {
  303. position: relative;
  304. width: 100%;
  305. border-radius: 5px;
  306. // box-shadow: 0px 0px 3px 1px rgba(0, 0, 0, 0.1);
  307. background-color: #fff;
  308. /* #ifndef APP-NVUE */
  309. box-sizing: border-box;
  310. display: table;
  311. overflow-x: auto;
  312. ::v-deep .uni-table-tr:nth-child(n + 2) {
  313. &:hover {
  314. background-color: #f5f7fa;
  315. }
  316. }
  317. ::v-deep .uni-table-thead {
  318. .uni-table-tr {
  319. // background-color: #f5f7fa;
  320. &:hover {
  321. background-color: #fafafa;
  322. }
  323. }
  324. }
  325. /* #endif */
  326. }
  327. .table--border {
  328. border: 1px $border-color solid;
  329. border-right: none;
  330. }
  331. .border-none {
  332. /* #ifndef APP-NVUE */
  333. border-bottom: none;
  334. /* #endif */
  335. }
  336. .table--stripe {
  337. /* #ifndef APP-NVUE */
  338. ::v-deep .uni-table-tr:nth-child(2n + 3) {
  339. background-color: #fafafa;
  340. }
  341. /* #endif */
  342. }
  343. /* 表格加载、无数据样式 */
  344. .uni-table-loading {
  345. position: relative;
  346. /* #ifndef APP-NVUE */
  347. display: table-row;
  348. /* #endif */
  349. height: 50px;
  350. line-height: 50px;
  351. overflow: hidden;
  352. box-sizing: border-box;
  353. }
  354. .empty-border {
  355. border-right: 1px $border-color solid;
  356. }
  357. .uni-table-text {
  358. position: absolute;
  359. right: 0;
  360. left: 0;
  361. text-align: center;
  362. font-size: 14px;
  363. color: #999;
  364. }
  365. .uni-table-mask {
  366. position: absolute;
  367. top: 0;
  368. bottom: 0;
  369. left: 0;
  370. right: 0;
  371. background-color: rgba(255, 255, 255, 0.8);
  372. z-index: 99;
  373. /* #ifndef APP-NVUE */
  374. display: flex;
  375. margin: auto;
  376. transition: all 0.5s;
  377. /* #endif */
  378. justify-content: center;
  379. align-items: center;
  380. }
  381. .uni-table--loader {
  382. width: 30px;
  383. height: 30px;
  384. border: 2px solid #aaa;
  385. // border-bottom-color: transparent;
  386. border-radius: 50%;
  387. /* #ifndef APP-NVUE */
  388. animation: 2s uni-table--loader linear infinite;
  389. /* #endif */
  390. position: relative;
  391. }
  392. @keyframes uni-table--loader {
  393. 0% {
  394. transform: rotate(360deg);
  395. }
  396. 10% {
  397. border-left-color: transparent;
  398. }
  399. 20% {
  400. border-bottom-color: transparent;
  401. }
  402. 30% {
  403. border-right-color: transparent;
  404. }
  405. 40% {
  406. border-top-color: transparent;
  407. }
  408. 50% {
  409. transform: rotate(0deg);
  410. }
  411. 60% {
  412. border-top-color: transparent;
  413. }
  414. 70% {
  415. border-left-color: transparent;
  416. }
  417. 80% {
  418. border-bottom-color: transparent;
  419. }
  420. 90% {
  421. border-right-color: transparent;
  422. }
  423. 100% {
  424. transform: rotate(-360deg);
  425. }
  426. }
  427. </style>