uni-table.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. /**
  81. * 清除选中
  82. */
  83. clearSelection(){
  84. this.trChildren.forEach((item, index) => {
  85. item.value = false
  86. })
  87. this.$emit('selection-change', {
  88. detail: {
  89. index: [],
  90. value: []
  91. }
  92. })
  93. },
  94. check(child, check) {
  95. const childDom = this.trChildren.find((item, index) => child === item)
  96. const childDomIndex = this.trChildren.findIndex((item, index) => child === item)
  97. if (childDomIndex === 0) {
  98. if (childDom.value !== check) {
  99. this.backData = []
  100. this.trChildren.map((item, index) => item.value = check)
  101. }
  102. this.trChildren.forEach((item, index) => {
  103. if (index > 0 && item.value) {
  104. this.backData.push(index - 1)
  105. }
  106. })
  107. } else {
  108. if (!check) {
  109. this.trChildren[0].value = false
  110. }
  111. childDom.value = check
  112. if (check) {
  113. this.backData.push(childDomIndex - 1)
  114. } else {
  115. const index = this.backData.findIndex(item => item === (childDomIndex - 1))
  116. this.backData.splice(index, 1)
  117. }
  118. const domCheckAll = this.trChildren.find((item, index) => index > 0 && !item.value)
  119. if (!domCheckAll) {
  120. this.trChildren[0].value = true
  121. }
  122. }
  123. this.$emit('selection-change', {
  124. detail: {
  125. index: this.backData.sort(),
  126. value: []
  127. }
  128. })
  129. }
  130. }
  131. }
  132. </script>
  133. <style lang="scss">
  134. .uni-table-scroll {
  135. width: 100%;
  136. overflow-x: auto;
  137. }
  138. .uni-table {
  139. position: relative;
  140. width: 100%;
  141. display: table;
  142. box-sizing: border-box;
  143. border-radius: 5px;
  144. box-shadow: 0px 0px 3px 1px rgba(0, 0, 0, 0.1);
  145. overflow-x: auto;
  146. background-color: #fff;
  147. ::v-deep .uni-table-tr:nth-child(n+2) {
  148. &:hover {
  149. background-color: #f5f7fa;
  150. }
  151. }
  152. }
  153. .table--border {
  154. border: 1px #ddd solid;
  155. }
  156. .border-none {
  157. border-bottom: none;
  158. }
  159. .table--stripe {
  160. ::v-deep .uni-table-tr:nth-child(2n+3) {
  161. background-color: #fafafa;
  162. }
  163. }
  164. /* 表格加载、无数据样式 */
  165. .uni-table-loading {
  166. position: relative;
  167. display: table-row;
  168. height: 50px;
  169. line-height: 50px;
  170. }
  171. .uni-table-text {
  172. position: absolute;
  173. right: 0;
  174. left: 0;
  175. text-align: center;
  176. font-size: 14px;
  177. color: #999;
  178. }
  179. .uni-table-mask {
  180. position: absolute;
  181. top: 0;
  182. bottom: 0;
  183. left: 0;
  184. right: 0;
  185. margin: auto;
  186. background-color: rgba(255, 255, 255, 0.8);
  187. z-index: 99;
  188. display: flex;
  189. justify-content: center;
  190. align-items: center;
  191. transition: all 0.5s;
  192. }
  193. .uni-table--loader {
  194. width: 30px;
  195. height: 30px;
  196. border: 2px solid #aaa;
  197. // border-bottom-color: transparent;
  198. border-radius: 50%;
  199. animation: 2s uni-table--loader linear infinite;
  200. position: relative;
  201. }
  202. @keyframes uni-table--loader {
  203. 0% {
  204. transform: rotate(360deg);
  205. }
  206. 10% {
  207. border-left-color: transparent;
  208. }
  209. 20% {
  210. border-bottom-color: transparent;
  211. }
  212. 30% {
  213. border-right-color: transparent;
  214. }
  215. 40% {
  216. border-top-color: transparent;
  217. }
  218. 50% {
  219. transform: rotate(0deg);
  220. }
  221. 60% {
  222. border-top-color: transparent;
  223. }
  224. 70% {
  225. border-left-color: transparent;
  226. }
  227. 80% {
  228. border-bottom-color: transparent;
  229. }
  230. 90% {
  231. border-right-color: transparent;
  232. }
  233. 100% {
  234. transform: rotate(-360deg);
  235. }
  236. }
  237. </style>