uni-th.vue 6.5 KB

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