uni-table.vue 10 KB

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