yealuo-select.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <template>
  2. <view class="yealuo-select" >
  3. <view class="yealuo-background" @tap="isShow=false" v-show="isShow"></view>
  4. <view class="yealuo-con" :style="inputStyle" @tap='isShow=isShow?false:nowData.length'>
  5. <slot name='left'></slot>
  6. <input :disabled="theDisabled" :placeholder="placeholder" v-model="theValue" @input="theInput" @focus="theFocus" @blur="theBlur" autocomplete="off" />
  7. <slot name='right' v-if="selectIco">
  8. <svg class="icon" v-if="!isShow" style="width: 1.5em; height: 1.5em;vertical-align: middle;fill: currentColor;overflow: hidden;" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="530"><path d="M512 714.666667c-8.533333 0-17.066667-2.133333-23.466667-8.533334l-341.333333-341.333333c-12.8-12.8-12.8-32 0-44.8 12.8-12.8 32-12.8 44.8 0l320 317.866667 317.866667-320c12.8-12.8 32-12.8 44.8 0 12.8 12.8 12.8 32 0 44.8L533.333333 704c-4.266667 8.533333-12.8 10.666667-21.333333 10.666667z" p-id="531"></path></svg>
  9. <svg class="icon" v-else style="width: 1.5em; height: 1.5em;vertical-align: middle;fill: currentColor;overflow: hidden;" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1927"><path d="M904.533333 674.133333l-362.666666-362.666666c-17.066667-17.066667-42.666667-17.066667-59.733334 0l-362.666666 362.666666c-17.066667 17.066667-17.066667 42.666667 0 59.733334 17.066667 17.066667 42.666667 17.066667 59.733333 0L512 401.066667l332.8 332.8c8.533333 8.533333 19.2 12.8 29.866667 12.8s21.333333-4.266667 29.866666-12.8c17.066667-17.066667 17.066667-42.666667 0-59.733334z" p-id="1928"></path></svg>
  10. </slot>
  11. </view>
  12. <view class="yealuo-select" v-show="show" :style="selectStyle">
  13. <view class="data">
  14. <radio-group v-if="checkType=='radio'" @change="selectCheckbox">
  15. <view class="select-item" :class="'item-'+overflow" v-for="(item, index) in nowData" :key="index" >
  16. <label class="item-text" :class="{active: theValue==item.value}">
  17. <radio name="name1" checked v-if="theValue==item.value" :value="item.value+'|'+item.id"></radio>
  18. <radio name="name1" v-else :value="item.value+'|'+item.id"></radio>
  19. {{item.value}}
  20. </label>
  21. </view>
  22. </radio-group>
  23. <checkbox-group v-else-if="checkType=='checkbox'" @change="selectCheckbox">
  24. <view class="select-item" :class="'item-'+overflow" v-for="(item, index) in nowData" :key="index" >
  25. <label class="item-text" :class="{active: theValue.indexOf(item.value)!=-1 }">
  26. <checkbox name="name1" checked v-if="theValue.indexOf(item.value)!=-1 " :value="item.value+'|'+item.id"></checkbox>
  27. <checkbox name="name1" v-else :value="item.value+'|'+item.id"></checkbox>
  28. {{item.value}}
  29. </label>
  30. </view>
  31. </checkbox-group>
  32. <radio-group v-else @change="selectCheckbox">
  33. <view class="select-item" :class="'item-'+overflow" v-for="(item, index) in nowData" :key="index" >
  34. <label class="item-text" :class="{active: theValue==item.value}">
  35. <radio name="name1" style="display: none;" checked v-if="theValue==item.value" :value="item.value+'|'+item.id"></radio>
  36. <radio name="name1" style="display: none;" v-else :value="item.value+'|'+item.id"></radio>
  37. {{item.value}}
  38. </label>
  39. </view>
  40. </radio-group>
  41. </view>
  42. <view class="item-close" @tap="isShow=false">收起</view>
  43. </view>
  44. </view>
  45. </template>
  46. <script>
  47. /**
  48. * v1.0.3
  49. * 最后修改: 2021.02.02
  50. * 创建: 2020.9.30
  51. * Author:yealuo.com
  52. * contact:470797533@qq.com
  53. */
  54. let fontUnit = 'upx'
  55. // #ifdef MP-WEIXIN
  56. fontUnit = 'rpx'
  57. // #endif
  58. export default {
  59. name: 'yealuoInputs',
  60. props: {
  61. placeholder: {
  62. type: String,
  63. default: ''
  64. },
  65. value: {
  66. type: String,
  67. default: ''
  68. },
  69. checkType: {
  70. type: String,
  71. default: ''
  72. },
  73. itemKey: {
  74. type: String,
  75. default: ''
  76. },
  77. width: {
  78. type: String,
  79. default: '600'
  80. },
  81. disabled: {
  82. type: Boolean,
  83. default: false
  84. },
  85. inputStyle: {
  86. type: String,
  87. default: ''
  88. },
  89. selectStyle: {
  90. type: String,
  91. default: ''
  92. },
  93. overflow: {
  94. type: String,
  95. default: 'auto'
  96. },
  97. tags: {
  98. type: String,
  99. default: ''
  100. },
  101. binData:{
  102. type:Array,
  103. default: ''
  104. },
  105. selectIco:{
  106. type: Boolean,
  107. default: false
  108. }
  109. },
  110. data() {
  111. return {
  112. odData:this.binData,
  113. nowData:this.binData,
  114. isShow: false,
  115. theValue: this.value,
  116. theDisabled: this.disabled
  117. }
  118. },
  119. watch: {
  120. value(val){
  121. this.theValue = val;
  122. },
  123. //监听数据变化
  124. nowData:{
  125. handler:function(){
  126. this.nowData=this.binData;
  127. },
  128. deep:true
  129. }
  130. },
  131. computed: {
  132. show(){
  133. return this.isShow && this.nowData.length
  134. }
  135. },
  136. methods: {
  137. //获取焦点
  138. theFocus(e){
  139. this.nowData=this.odData;
  140. },
  141. //失去焦点
  142. theBlur(e){
  143. this.$emit('blur',e)
  144. },
  145. //获取输入值
  146. theInput(e) {
  147. var val=e.detail.value;
  148. let data = [];
  149. var odData=this.odData;
  150. for(var i=0;i<odData.length;i++){
  151. var isHas=false;
  152. if(odData[i].value.indexOf(val)!=-1){
  153. data.push(odData[i])
  154. if(odData[i].value==val){
  155. isHas=true;
  156. var arr=[];
  157. arr.push(odData[i].value+"|"+odData[i].id)
  158. this.$emit('getBackVal',arr);
  159. }
  160. }
  161. if(!isHas){
  162. var arr=[];
  163. arr.push(val)
  164. this.$emit('getBackVal',arr);
  165. }
  166. }
  167. this.nowData=data;
  168. },
  169. //下拉选中
  170. selectCheckbox(e){
  171. var val=e.target.value;
  172. var str=val;
  173. if(typeof(str)!="string"){
  174. str="";
  175. for(var i=0;i<val.length;i++){
  176. var vt=val[i].split("|");
  177. str+=i>0?","+vt[0]:vt[0];
  178. }
  179. }
  180. else{
  181. this.isShow = false;
  182. str=str.split("|")[0];
  183. }
  184. this.$emit('getBackVal',val+"|"+this.tags)
  185. this.theValue = str;
  186. }
  187. },
  188. }
  189. </script>
  190. <style lang="scss" scoped>
  191. .yealuo-select{
  192. max-width: 100%;
  193. position: relative;
  194. .yealuo-background{position: fixed;top:0;left:0;width: 750upx;height: 100%;}
  195. .yealuo-con{display: flex;align-items: center;justify-content: center;
  196. input{flex: 1;margin: 0 6upx;}
  197. }
  198. .yealuo-select {
  199. border: 1px solid #f3f3f4;
  200. position: absolute;
  201. z-index: 999;
  202. background: #fff;
  203. width: 100%;
  204. .data{
  205. max-height: 600upx;
  206. padding: 10upx;
  207. overflow: auto;
  208. .select-item {width: 100%;color:#666;
  209. .item-text{ width:100%; display:block;}
  210. .active{font-weight: bold;}
  211. }
  212. .item-auto{overflow: auto;
  213. .item-text{width: max-content;}
  214. }
  215. .item-hide .item-text{
  216. overflow: hidden;
  217. text-overflow:ellipsis;
  218. white-space: nowrap;
  219. }
  220. }
  221. .item-close {
  222. padding: 20upx;
  223. text-align: center;
  224. font-size: 32upx;
  225. border-top: 1px solid #f3f3f4;
  226. color:#8F8F94;
  227. }
  228. }
  229. }
  230. </style>