uni-th.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <template>
  2. <!-- #ifdef H5 -->
  3. <th :rowspan="rowspan" :colspan="colspan" class="uni-table-th" :class="{ 'table--border': border }" :style="{ width: width + 'px', 'text-align': align }">
  4. <view class="uni-table-th-row">
  5. <view class="uni-table-th-content" :style="{ 'justify-content': contentAlign }" @click="sort">
  6. <slot></slot>
  7. <view v-if="sortable" class="arrow-box">
  8. <text class="arrow up" :class="{ active: ascending }" @click.stop="ascendingFn"></text>
  9. <text class="arrow down" :class="{ active: descending }" @click.stop="descendingFn"></text>
  10. </view>
  11. </view>
  12. <dropdown v-if="filterType || filterData.length" :filterData="filterData" :filterType="filterType" @change="ondropdown"></dropdown>
  13. </view>
  14. </th>
  15. <!-- #endif -->
  16. <!-- #ifndef H5 -->
  17. <view class="uni-table-th" :class="{ 'table--border': border }" :style="{ width: width + 'px', 'text-align': align }"><slot></slot></view>
  18. <!-- #endif -->
  19. </template>
  20. <script>
  21. import dropdown from './filter-dropdown.vue'
  22. /**
  23. * Th 表头
  24. * @description 表格内的表头单元格组件
  25. * @tutorial https://ext.dcloud.net.cn/plugin?id=3270
  26. * @property {Number} width 单元格宽度
  27. * @property {Boolean} sortable 是否启用排序
  28. * @property {Number} align = [left|center|right] 单元格对齐方式
  29. * @value left 单元格文字左侧对齐
  30. * @value center 单元格文字居中
  31. * @value right 单元格文字右侧对齐
  32. * @property {Array} filterData 筛选数据
  33. * @property {String} filterType [search|select] 筛选类型
  34. * @value search 关键字搜素
  35. * @value select 条件选择
  36. * @event {Function} sort-change 排序触发事件
  37. */
  38. export default {
  39. name: 'uniTh',
  40. options: {
  41. virtualHost: true
  42. },
  43. components: {
  44. dropdown
  45. },
  46. emits:['sort-change','filter-change'],
  47. props: {
  48. width: {
  49. type: [String, Number],
  50. default: ''
  51. },
  52. align: {
  53. type: String,
  54. default: 'left'
  55. },
  56. rowspan: {
  57. type: [Number, String],
  58. default: 1
  59. },
  60. colspan: {
  61. type: [Number, String],
  62. default: 1
  63. },
  64. sortable: {
  65. type: Boolean,
  66. default: false
  67. },
  68. filterType: {
  69. type: String,
  70. default: ""
  71. },
  72. filterData: {
  73. type: Array,
  74. default () {
  75. return []
  76. }
  77. }
  78. },
  79. data() {
  80. return {
  81. border: false,
  82. ascending: false,
  83. descending: false
  84. }
  85. },
  86. computed: {
  87. contentAlign() {
  88. let align = 'left'
  89. switch (this.align) {
  90. case 'left':
  91. align = 'flex-start'
  92. break
  93. case 'center':
  94. align = 'center'
  95. break
  96. case 'right':
  97. align = 'flex-end'
  98. break
  99. }
  100. return align
  101. }
  102. },
  103. created() {
  104. this.root = this.getTable('uniTable')
  105. this.rootTr = this.getTable('uniTr')
  106. this.rootTr.minWidthUpdate(this.width ? this.width : 140)
  107. this.border = this.root.border
  108. this.root.thChildren.push(this)
  109. },
  110. methods: {
  111. sort() {
  112. if (!this.sortable) return
  113. this.clearOther()
  114. if (!this.ascending && !this.descending) {
  115. this.ascending = true
  116. this.$emit('sort-change', { order: 'ascending' })
  117. return
  118. }
  119. if (this.ascending && !this.descending) {
  120. this.ascending = false
  121. this.descending = true
  122. this.$emit('sort-change', { order: 'descending' })
  123. return
  124. }
  125. if (!this.ascending && this.descending) {
  126. this.ascending = false
  127. this.descending = false
  128. this.$emit('sort-change', { order: null })
  129. }
  130. },
  131. ascendingFn() {
  132. this.clearOther()
  133. this.ascending = !this.ascending
  134. this.descending = false
  135. this.$emit('sort-change', { order: this.ascending ? 'ascending' : null })
  136. },
  137. descendingFn() {
  138. this.clearOther()
  139. this.descending = !this.descending
  140. this.ascending = false
  141. this.$emit('sort-change', { order: this.descending ? 'descending' : null })
  142. },
  143. clearOther() {
  144. this.root.thChildren.map(item => {
  145. if (item !== this) {
  146. item.ascending = false
  147. item.descending = false
  148. }
  149. return item
  150. })
  151. },
  152. ondropdown(e) {
  153. this.$emit("filter-change", e)
  154. },
  155. /**
  156. * 获取父元素实例
  157. */
  158. getTable(name) {
  159. let parent = this.$parent
  160. let parentName = parent.$options.name
  161. while (parentName !== name) {
  162. parent = parent.$parent
  163. if (!parent) return false
  164. parentName = parent.$options.name
  165. }
  166. return parent
  167. }
  168. }
  169. }
  170. </script>
  171. <style lang="scss">
  172. $border-color: #ebeef5;
  173. .uni-table-th {
  174. padding: 12px 10px;
  175. /* #ifndef APP-NVUE */
  176. display: table-cell;
  177. box-sizing: border-box;
  178. /* #endif */
  179. font-size: 14px;
  180. font-weight: bold;
  181. color: #909399;
  182. border-bottom: 1px $border-color solid;
  183. }
  184. .uni-table-th-row {
  185. /* #ifndef APP-NVUE */
  186. display: flex;
  187. /* #endif */
  188. flex-direction: row;
  189. }
  190. .table--border {
  191. border-right: 1px $border-color solid;
  192. }
  193. .uni-table-th-content {
  194. display: flex;
  195. align-items: center;
  196. flex: 1;
  197. }
  198. .arrow-box {
  199. }
  200. .arrow {
  201. display: block;
  202. position: relative;
  203. width: 10px;
  204. height: 8px;
  205. // border: 1px red solid;
  206. left: 5px;
  207. overflow: hidden;
  208. cursor: pointer;
  209. }
  210. .down {
  211. top: 3px;
  212. ::after {
  213. content: '';
  214. width: 8px;
  215. height: 8px;
  216. position: absolute;
  217. left: 2px;
  218. top: -5px;
  219. transform: rotate(45deg);
  220. background-color: #ccc;
  221. }
  222. &.active {
  223. ::after {
  224. background-color: #007aff;
  225. }
  226. }
  227. }
  228. .up {
  229. ::after {
  230. content: '';
  231. width: 8px;
  232. height: 8px;
  233. position: absolute;
  234. left: 2px;
  235. top: 5px;
  236. transform: rotate(45deg);
  237. background-color: #ccc;
  238. }
  239. &.active {
  240. ::after {
  241. background-color: #007aff;
  242. }
  243. }
  244. }
  245. </style>