tn-tr.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <view class="tn-tr-class tn-tr" :class="[trClass]" :style="[trStyle]">
  3. <slot></slot>
  4. </view>
  5. </template>
  6. <script>
  7. import componentsColorMixin from '../../libs/mixin/components_color.js'
  8. export default {
  9. name: 'tn-tr',
  10. options: {
  11. // 在微信小程序中将组件节点渲染为虚拟节点,更加接近Vue组件的表现(不会出现shadow节点下再去创建元素)
  12. virtualHost: true
  13. },
  14. mixins: [componentsColorMixin],
  15. props: {
  16. // 宽度
  17. width: {
  18. type: [String, Number],
  19. default: ''
  20. },
  21. // 边框颜色
  22. borderColor: {
  23. type: String,
  24. default: ''
  25. },
  26. // 边框宽度
  27. borderWidth: {
  28. type: [String, Number],
  29. default: ''
  30. },
  31. // 左边框
  32. borderLeft: {
  33. type: Boolean,
  34. default: false
  35. },
  36. // 上边框
  37. borderTop: {
  38. type: Boolean,
  39. default: false
  40. },
  41. // 换行显示
  42. wrap: {
  43. type: Boolean,
  44. default: false
  45. },
  46. // 固定表格
  47. fixed: {
  48. type: Boolean,
  49. default: false
  50. },
  51. // left偏移值
  52. left: {
  53. type: [String, Number],
  54. default: 0
  55. },
  56. // right偏移值
  57. right: {
  58. type: [String, Number],
  59. default: 0
  60. },
  61. // top偏移值(自定义顶部导航栏时用到)
  62. top: {
  63. type: [String, Number],
  64. default: 0
  65. },
  66. // 外边距
  67. margin: {
  68. type: String,
  69. default: ''
  70. },
  71. // zIndex
  72. zIndex: {
  73. type: Number,
  74. default: 0
  75. },
  76. // 行数索引
  77. index: {
  78. type: [String, Number],
  79. default: 0
  80. },
  81. // 参数
  82. params: {
  83. type: String,
  84. default: ''
  85. }
  86. },
  87. computed: {
  88. borderWidthValue() {
  89. return this.borderWidth || this.parentData.borderWidth || ''
  90. },
  91. borderColorValue() {
  92. return this.borderColor || this.parentData.borderColor || ''
  93. },
  94. trClass() {
  95. let clazz = ''
  96. if (this.backgroundColorClass) {
  97. clazz += ` ${this.backgroundColorClass}`
  98. }
  99. if (this.fontColorClass) {
  100. clazz += ` ${this.fontColorClass}`
  101. }
  102. if (this.wrap) {
  103. clazz += ' tn-tr--wrap'
  104. }
  105. if (this.fixed) {
  106. clazz += ' tn-tr--fixed'
  107. }
  108. return clazz
  109. },
  110. trStyle() {
  111. let style = {}
  112. if (this.width) {
  113. style.width = this.$t.string.getLengthUnitValue(this.width)
  114. }
  115. if (this.backgroundColorStyle) {
  116. style.backgroundColor = this.backgroundColorStyle
  117. }
  118. if (this.fontColorStyle) {
  119. style.color = this.fontColorStyle
  120. }
  121. if (this.fontSizeStyle) {
  122. style.fontSize = this.fontSizeStyle
  123. }
  124. if (this.borderWidth !== '' || this.parentData.borderWidth !== '') {
  125. style.borderWidth = this.borderWidth !== '' ? this.$t.string.getLengthUnitValue(this.borderWidth) : this.$t.string.getLengthUnitValue(this.parentData.borderWidth)
  126. }
  127. if (this.borderColor || this.parentData.borderColor) {
  128. style.borderColor = this.borderColor || this.parentData.borderColor
  129. }
  130. if (this.borderLeft) {
  131. style.borderLeftStyle = 'solid'
  132. }
  133. if (this.borderTop) {
  134. style.borderTopStyle = 'solid'
  135. }
  136. if (this.fixed) {
  137. style.left = this.left ? this.$t.string.getLengthUnitValue(this.left) : 'auto'
  138. style.right = this.right ? this.$t.string.getLengthUnitValue(this.right) : 'auto'
  139. style.top = this.top ? this.$t.string.getLengthUnitValue(this.top) : 'auto'
  140. }
  141. if (this.margin) {
  142. style.margin = this.margin
  143. }
  144. style.zIndex = this.zIndex ? this.zIndex : this.$t.zIndex.tableTr
  145. return style
  146. }
  147. },
  148. data() {
  149. return {
  150. parentData: {
  151. borderColor: null,
  152. borderWidth: null
  153. }
  154. }
  155. },
  156. watch: {
  157. parentData: {
  158. handler() {
  159. // 更新子组件的数据
  160. if (this.children.length) {
  161. this.children.map((child) => {
  162. typeof(child.updateParentData) === 'function' && child.updateParentData()
  163. })
  164. }
  165. },
  166. deep: true
  167. }
  168. },
  169. created() {
  170. this.children = []
  171. this.parent = false
  172. this.updateParentData()
  173. this.parent && this.parent.children.push(this)
  174. },
  175. methods: {
  176. handleClick() {
  177. this.$emit('click', {
  178. index: this.index,
  179. params: this.params
  180. })
  181. },
  182. // 更新父组件信息
  183. updateParentData() {
  184. this.getParentData('tn-table')
  185. }
  186. }
  187. }
  188. </script>
  189. <style lang="scss" scoped>
  190. .tn-tr {
  191. width: 100%;
  192. display: flex;
  193. box-sizing: border-box;
  194. background-color: #FFFFFF;
  195. border-width: 1rpx;
  196. border-style: none none solid none;
  197. border-color: #AAAAAA;
  198. &--wrap {
  199. flex-wrap: wrap;
  200. }
  201. &--fixed {
  202. position: fixed;
  203. }
  204. }
  205. </style>