uni-table.vue 9.8 KB

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