uni-table.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <template>
  2. <view class="uni-table-scroll" :class="{'table--border':border,'border-none':!noData}">
  3. <view class="uni-table" :style="{'min-width':minWidth+'px'}" :class="{ 'table--stripe':stripe}">
  4. <slot></slot>
  5. <view v-if="noData" class="uni-table-loading">
  6. <view class="uni-table-text">{{emptyText}}</view>
  7. </view>
  8. <view v-show="loading" class="uni-table-mask">
  9. <div class="uni-table--loader"></div>
  10. </view>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. /**
  16. * Table 表格
  17. * @description 用于展示多条结构类似的数据
  18. * @tutorial https://ext.dcloud.net.cn/plugin?id=
  19. * @property {Boolean} border 是否带有纵向边框
  20. * @property {Boolean} stripe 是否显示斑马线
  21. * @property {Boolean} type 是否开启多选
  22. * @property {String} emptyText 空数据时显示的文本内容
  23. * @property {Boolean} loading 显示加载中
  24. * @event {Function} selection-change 开启多选时,当选择项发生变化时会触发该事件
  25. */
  26. export default {
  27. name: 'uniTable',
  28. options: {
  29. virtualHost: true
  30. },
  31. props: {
  32. // 是否有竖线
  33. border: {
  34. type: Boolean,
  35. default: false
  36. },
  37. // 是否显示斑马线
  38. stripe: {
  39. type: Boolean,
  40. default: false
  41. },
  42. // 多选
  43. type: {
  44. type: String,
  45. default: ''
  46. },
  47. // 没有更多数据
  48. emptyText: {
  49. type: String,
  50. default: '没有更多数据'
  51. },
  52. loading: {
  53. type: Boolean,
  54. default: false
  55. },
  56. },
  57. data() {
  58. return {
  59. noData: true,
  60. minWidth:0
  61. };
  62. },
  63. watch: {
  64. loading(val) {
  65. }
  66. },
  67. created() {
  68. // 定义tr的实例数组
  69. this.trChildren = []
  70. this.backData = []
  71. },
  72. methods: {
  73. isNodata() {
  74. if (this.trChildren.length > 1) {
  75. this.noData = false
  76. } else {
  77. this.noData = true
  78. }
  79. },
  80. check(child, check) {
  81. const childDom = this.trChildren.find((item, index) => child === item)
  82. const childDomIndex = this.trChildren.findIndex((item, index) => child === item)
  83. if (childDomIndex === 0) {
  84. if (childDom.value !== check) {
  85. this.backData = []
  86. this.trChildren.map((item, index) => item.value = check)
  87. }
  88. this.trChildren.forEach((item, index) => {
  89. if (index > 0 && item.value) {
  90. this.backData.push(index - 1)
  91. }
  92. })
  93. } else {
  94. if (!check) {
  95. this.trChildren[0].value = false
  96. }
  97. childDom.value = check
  98. if (check) {
  99. this.backData.push(childDomIndex - 1)
  100. } else {
  101. const index = this.backData.findIndex(item => item === (childDomIndex - 1))
  102. this.backData.splice(index, 1)
  103. }
  104. const domCheckAll = this.trChildren.find((item, index) => index > 0 && !item.value)
  105. if (!domCheckAll) {
  106. this.trChildren[0].value = true
  107. }
  108. }
  109. this.$emit('selection-change', {
  110. detail: {
  111. index: this.backData.sort(),
  112. value: []
  113. }
  114. })
  115. }
  116. }
  117. }
  118. </script>
  119. <style lang="scss">
  120. .uni-table-scroll {
  121. width: 100%;
  122. overflow-x: auto;
  123. }
  124. .uni-table {
  125. position: relative;
  126. width: 100%;
  127. display: table;
  128. box-sizing: border-box;
  129. border-radius: 5px;
  130. box-shadow: 0px 0px 3px 1px rgba(0, 0, 0, 0.1);
  131. overflow-x: auto;
  132. background-color: #fff;
  133. /deep/ .uni-table-tr:nth-child(n+2) {
  134. &:hover {
  135. background-color: #f5f7fa;
  136. }
  137. }
  138. }
  139. .table--border {
  140. border: 1px #ddd solid;
  141. }
  142. .border-none {
  143. border-bottom: none;
  144. }
  145. .table--stripe {
  146. /deep/ .uni-table-tr:nth-child(2n+3) {
  147. background-color: #fafafa;
  148. }
  149. }
  150. /* 表格加载、无数据样式 */
  151. .uni-table-loading {
  152. position: relative;
  153. display: table-row;
  154. height: 50px;
  155. line-height: 50px;
  156. }
  157. .uni-table-text {
  158. position: absolute;
  159. right: 0;
  160. left: 0;
  161. text-align: center;
  162. font-size: 14px;
  163. color: #999;
  164. }
  165. .uni-table-mask {
  166. position: absolute;
  167. top: 0;
  168. bottom: 0;
  169. left: 0;
  170. right: 0;
  171. margin: auto;
  172. background-color: rgba(255, 255, 255, 0.8);
  173. z-index: 99;
  174. display: flex;
  175. justify-content: center;
  176. align-items: center;
  177. transition: all 0.5s;
  178. }
  179. .uni-table--loader {
  180. width: 30px;
  181. height: 30px;
  182. border: 2px solid #aaa;
  183. // border-bottom-color: transparent;
  184. border-radius: 50%;
  185. animation: 2s uni-table--loader linear infinite;
  186. position: relative;
  187. }
  188. @keyframes uni-table--loader {
  189. 0% {
  190. transform: rotate(360deg);
  191. }
  192. 10% {
  193. border-left-color: transparent;
  194. }
  195. 20% {
  196. border-bottom-color: transparent;
  197. }
  198. 30% {
  199. border-right-color: transparent;
  200. }
  201. 40% {
  202. border-top-color: transparent;
  203. }
  204. 50% {
  205. transform: rotate(0deg);
  206. }
  207. 60% {
  208. border-top-color: transparent;
  209. }
  210. 70% {
  211. border-left-color: transparent;
  212. }
  213. 80% {
  214. border-bottom-color: transparent;
  215. }
  216. 90% {
  217. border-right-color: transparent;
  218. }
  219. 100% {
  220. transform: rotate(-360deg);
  221. }
  222. }
  223. </style>