en-select.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <template>
  2. <view class="box">
  3. <view class="input-box">
  4. <view class="input-box-left" :style="{'letter-spacing':labelWidth}">
  5. {{ label }}
  6. </view>
  7. <view class="input-box-right" @click="showPickerObj">
  8. <text :class="{'no-option':!optionName}">{{ optionName ? optionName : placeholder }}</text>
  9. <text class="iconfont">&#xe62b;</text>
  10. </view>
  11. </view>
  12. <uni-data-picker :popup-title="'选择'+label" :localdata="localData" ref="pickerObj" v-show="showPicker"
  13. @change="pickerChange" :border="false" :clear-icon="false" @popupclosed="setPopupClosed">
  14. </uni-data-picker>
  15. </view>
  16. </template>
  17. <script>
  18. export default {
  19. name: 'en-select',
  20. props: {
  21. localData: {
  22. type: Array,
  23. default: [{
  24. text: "一年级",
  25. value: "1-0",
  26. children: [{
  27. text: "1.1班",
  28. value: "1-1"
  29. },
  30. {
  31. text: "1.2班",
  32. value: "1-2"
  33. }]
  34. },
  35. {
  36. text: "二年级",
  37. value: "2-0",
  38. children: [{
  39. text: "2.1班",
  40. value: "2-1"
  41. },
  42. {
  43. text: "2.2班",
  44. value: "2-2"
  45. }]
  46. }]
  47. },
  48. label: {
  49. type: String,
  50. default: '标题'
  51. },
  52. placeholder: {
  53. type: String,
  54. default: '请选择'
  55. },
  56. disabled: {
  57. type: Boolean,
  58. default: false
  59. },
  60. name: {
  61. type: String,
  62. default: 'text'
  63. },
  64. valueType: {
  65. type: String,
  66. default: '1'
  67. },
  68. value: {
  69. default: ''
  70. }
  71. },
  72. data() {
  73. return {
  74. inputValue: [],
  75. optionName: '',
  76. labelWidth: 0,
  77. showPicker:false
  78. }
  79. },
  80. components: {},
  81. mounted() {
  82. this.setValue()
  83. this.setLabelWidth()
  84. },
  85. watch: {
  86. 'value': function () {
  87. this.setValue()
  88. },
  89. 'inputValue': function () {
  90. if(this.valueType==='1'){
  91. this.$emit('input', this.inputValue.join(','))
  92. }else {
  93. this.$emit('input', this.inputValue)
  94. }
  95. }
  96. },
  97. methods: {
  98. setValue(){
  99. let value=this.value
  100. if(typeof value==='string'){
  101. value=value.split(',')
  102. }
  103. if(this.inputValue!==value){
  104. this.inputValue=value
  105. this.optionName = "";
  106. this.localData.forEach((one)=>{
  107. if(one.value===this.inputValue[0]){
  108. this.optionName += one.text + " ";
  109. if(one.children){
  110. one.children.forEach((two)=>{
  111. if(two.value===this.inputValue[1]){
  112. this.optionName += two.text + " ";
  113. if(two.children){
  114. two.children.forEach((three)=>{
  115. if(three.value===this.inputValue[2]){
  116. this.optionName += three.text + " ";
  117. }
  118. })
  119. }
  120. }
  121. })
  122. }
  123. }
  124. })
  125. }
  126. },
  127. setPopupClosed() {
  128. this.showPicker = false;
  129. },
  130. showPickerObj() {
  131. console.log(this.$refs.pickerObj)
  132. this.$refs.pickerObj.show();
  133. this.showPicker = true;
  134. },
  135. pickerChange(data) {
  136. this.optionName = "";
  137. this.inputValue = [];
  138. data.detail.value.forEach((item) => {
  139. this.optionName += item.text + " ";
  140. this.inputValue.push(item.value)
  141. });
  142. this.showPicker = false;
  143. },
  144. setLabelWidth() {
  145. let differenceNum = 4 - this.label.length;
  146. if (differenceNum === 2) {
  147. this.labelWidth = '2em'
  148. } else if (differenceNum === 1) {
  149. this.labelWidth = '0.5em'
  150. }
  151. }
  152. }
  153. }
  154. </script>
  155. <style lang="scss" scoped>
  156. @import url("../../static/css/en-common.css");
  157. .box {
  158. .input-box {
  159. display: flex;
  160. align-items: center;
  161. justify-content: space-between;
  162. .input-box-left {
  163. width: 210rpx;
  164. font-size: 32rpx;
  165. color: #333333;
  166. }
  167. .input-box-right {
  168. width: 100%;
  169. text {
  170. font-size: 32rpx;
  171. color: #333;
  172. }
  173. .iconfont {
  174. float: right;
  175. }
  176. .no-option {
  177. color: #999999;
  178. }
  179. }
  180. }
  181. }
  182. </style>