cus-selects-fan.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <template>
  2. <view class="select_wrap" :style="{'width': size +'px'}">
  3. <view :class="['select_input',isClick?'select_input_select':'']" ref="select-input">
  4. <view class="input_info" @click.stop="openSelect" v-if="!readonly">
  5. <input placeholder-style="font-size: 14px;color: #a0a9b0;" :focus="isClick" @input="selectData" :value="selLabel" type="text" :readonly="readonly" :disabled="readonly" autocomplete="off" :placeholder="placeholder" class="text_tips">
  6. </view>
  7. <view class="input_info" @click.stop="openSelect" v-else>
  8. <view :placeholder="placeholder" class="text_tips">{{selLabel}} <text v-if="!selLabel">{{placeholder}}</text> </view>
  9. </view>
  10. <view class="icon_arrow" @click="clearItem">
  11. <view v-if="(!value&&!clearable) || (value&&!clearable) || (!value&&clearable) && !filterable" :class="['arrow',show?'arrow_down':'arrow_up']" ></view>
  12. <view class="arrow-clear" v-if="value&&clearable">x</view>
  13. </view>
  14. </view>
  15. <view class="select_modal_con" v-if="show"
  16. @touchmove.stop.prevent="() => {}">
  17. <!-- #ifdef H5 -->
  18. <scroll-view scroll-y="true" class="select_modal select_modal_scroll">
  19. <view class="select_content" ref="select_content">
  20. <view v-for="(item, index) in showData" :key="index" class="select_content_li" :class="{'selected': value == item[valueType.value]}" @click="change(item)">{{item[valueType.label]}}</view>
  21. <view class="select_content_li" v-if="!showData.length">{{noDataText}}</view>
  22. </view>
  23. </scroll-view>
  24. <!-- #endif -->
  25. <!-- #ifndef H5 -->
  26. <view class="select_modal">
  27. <view class="select_content" ref="select_content">
  28. <view v-for="(item, index) in showData" :key="index" class="select_content_li" :class="{'selected': value == item[valueType.value]}" @click="change(item)">{{item[valueType.label]}}</view>
  29. <view class="select_content_li" v-if="!showData.length">{{noDataText}}</view>
  30. </view>
  31. </view>
  32. <!-- #endif -->
  33. <view class="cons_arrow" :style="{'left': arrLeft +'px'}"></view>
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. export default {
  39. props: {
  40. data: {
  41. type: Array,
  42. default: () => [{
  43. label: '2022-02',
  44. value: '02'
  45. },
  46. {
  47. label: '2022-01',
  48. value: '01'
  49. },
  50. {
  51. label: '2021-12',
  52. value: '12'
  53. },
  54. {
  55. label: '2021-11',
  56. value: '11'
  57. },
  58. {
  59. label: '2021-10',
  60. value: '10'
  61. },
  62. {
  63. label: '2021-09',
  64. value: '09'
  65. }
  66. ]
  67. },
  68. valueType: {
  69. type: Object,
  70. default:()=>{
  71. return {
  72. label:'label',
  73. value:'value'
  74. }
  75. }
  76. },
  77. value: {
  78. type: [String,Number],
  79. default: ''
  80. },
  81. clearable:{
  82. type: Boolean,
  83. default: false
  84. },
  85. filterable:{
  86. type: Boolean,
  87. default: false
  88. },
  89. searchType:{
  90. type: Number,
  91. default: 1
  92. },
  93. placeholder:{
  94. type: String,
  95. default: '请选择'
  96. },
  97. noDataText:{
  98. type: String,
  99. default: '暂无数据'
  100. },
  101. arrLeft:{
  102. type: Number,
  103. default: 20
  104. },
  105. size:{
  106. type: Number,
  107. default: 240
  108. }
  109. },
  110. data() {
  111. return {
  112. show: false,
  113. readonly:true,
  114. isClick:false,
  115. totalArr:[],
  116. showData:[],
  117. selLabel:''
  118. }
  119. },
  120. watch:{
  121. 'filterable':{
  122. immediate:true,
  123. deep:true,
  124. handler(news){
  125. this.readonly = !news
  126. }
  127. },
  128. 'data':{
  129. immediate:true,
  130. deep:true,
  131. handler(news){
  132. this.showData = news
  133. this.totalArr = news
  134. }
  135. },
  136. 'value':{
  137. immediate:true,
  138. deep:true,
  139. handler(news){
  140. console.log(news,'value',this.data,this.valueType.value)
  141. if(news){
  142. let index = this.data.findIndex(ite=>ite[this.valueType.value]==news)
  143. if(index==-1){
  144. uni.showToast({
  145. title:'传入的value不存在',
  146. icon:'none',
  147. duration:1500
  148. })
  149. }else{
  150. this.selLabel = this.data[index][this.valueType.label]
  151. }
  152. }
  153. }
  154. }
  155. },
  156. created() {},
  157. methods: {
  158. openSelect(){
  159. console.log('openSelect[[[]]]')
  160. // if(!this.filterable){
  161. // this.show=!this.show
  162. // }
  163. this.show=!this.show
  164. this.isClick = !this.isClick
  165. },
  166. change(item) {
  167. if (this.value != item[this.valueType]) {
  168. this.$emit('input', item[this.valueType.value])
  169. this.$emit('change', item[this.valueType.value])
  170. }
  171. this.selLabel = item[this.valueType.label]
  172. this.show = false
  173. this.isClick = false
  174. this.showData = this.data
  175. },
  176. clearItem(){
  177. if(this.clearable){
  178. this.$emit('input', '')
  179. this.$emit('change', '')
  180. }
  181. this.selLabel = ''
  182. },
  183. selectData(e){
  184. let sel = e.detail.value
  185. if(sel){
  186. let arrCons = []
  187. let selVal = []
  188. this.data.forEach(item=>{
  189. arrCons.push(item)
  190. })
  191. arrCons.forEach((item)=>{
  192. if(this.searchType==1){
  193. if(item[this.valueType.label].indexOf(sel)!=-1){
  194. selVal.push(item)
  195. }
  196. }else{
  197. if(item[this.valueType.label]==sel){
  198. selVal.push(item)
  199. }
  200. }
  201. })
  202. this.show = true
  203. this.showData = selVal
  204. }else{
  205. this.showData = this.data
  206. }
  207. }
  208. },
  209. beforeDestroy() {}
  210. }
  211. </script>
  212. <style lang="less" scoped>
  213. .select_wrap {
  214. width: 240px;
  215. display: inline-block;
  216. position: relative;
  217. .select_input {
  218. -webkit-appearance: none;
  219. background-color: #fff;
  220. background-image: none;
  221. border-radius: 4px;
  222. border: 1px solid #dcdfe6;
  223. box-sizing: border-box;
  224. color: #606266;
  225. display: inline-block;
  226. font-size: inherit;
  227. height: 40px;
  228. line-height: 40px;
  229. outline: none;
  230. padding: 0 15px;
  231. box-sizing: border-box;
  232. transition: border-color .2s cubic-bezier(.645, .045, .355, 1);
  233. width: 100%;
  234. padding-right: 30px;
  235. .input_info {
  236. font-size: 18px;
  237. width: 100%;
  238. height: 100%;
  239. .text_tips{
  240. height: 100%;
  241. text{
  242. font-size: 14px;
  243. color: #a0a9b0;
  244. }
  245. }
  246. }
  247. .icon_arrow {
  248. position: absolute;
  249. width: 30px;
  250. height: 40px;
  251. right: 0;
  252. top: 0;
  253. text-align: center;
  254. color: #c0c4cc;
  255. cursor: pointer;
  256. display: flex;
  257. justify-content: center;
  258. align-items: center;
  259. z-index: 999;
  260. .arrow {
  261. width: 10px;
  262. height: 10px;
  263. background-color: transparent;
  264. /* 模块背景为透明 */
  265. border-color: #c0c4cc;
  266. border-style: solid;
  267. border-width: 1px 1px 0 0;
  268. box-sizing: border-box;
  269. transition: all .3s;
  270. box-sizing: border-box;
  271. /*箭头方向可以自由切换角度*/
  272. }
  273. .arrow_down{
  274. transform: rotate(-45deg);
  275. margin-top: 5px;
  276. }
  277. .arrow_up{
  278. transform: rotate(135deg);
  279. margin-top: -5px;
  280. }
  281. .arrow-clear{
  282. width: 14px;
  283. height: 14px;
  284. border: 1px solid #e4e7ed;
  285. color: #e4e7ed;
  286. border-radius: 50%;
  287. display: flex;
  288. justify-content: center;
  289. align-items: center;
  290. font-size: 12px;
  291. }
  292. }
  293. }
  294. .select_input_select{
  295. border-color: #409eff;
  296. }
  297. }
  298. .select_modal_con {
  299. width: 100%;
  300. transform-origin: center top;
  301. z-index: 2062;
  302. position: absolute;
  303. top: 40px;
  304. left: 0;
  305. border: 1px solid #e4e7ed;
  306. border-radius: 4px;
  307. background-color: #fff;
  308. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
  309. box-sizing: border-box;
  310. margin-top: 12px;
  311. .cons_arrow {
  312. position: absolute;
  313. display: block;
  314. width: 0;
  315. height: 0;
  316. border-color: transparent;
  317. border-style: solid;
  318. top: -6px;
  319. left: 10%;
  320. margin-right: 3px;
  321. border-top-width: 0;
  322. border-bottom-color: #ebeef5;
  323. }
  324. .cons_arrow:after {
  325. content: " ";
  326. border-width: 6px;
  327. position: absolute;
  328. display: block;
  329. width: 0;
  330. height: 0;
  331. border-color: transparent;
  332. border-style: solid;
  333. top: 1px;
  334. margin-left: -6px;
  335. border-top-width: 0;
  336. border-bottom-color: #fff;
  337. }
  338. }
  339. .select_modal {
  340. overflow: scroll;
  341. height: 160px;
  342. .select_content{
  343. list-style: none;
  344. padding: 6px 0;
  345. margin: 0;
  346. box-sizing: border-box;
  347. .select_content_li {
  348. font-size: 14px;
  349. padding: 0 20px;
  350. position: relative;
  351. white-space: nowrap;
  352. overflow: hidden;
  353. text-overflow: ellipsis;
  354. color: #606266;
  355. height: 34px;
  356. line-height: 34px;
  357. box-sizing: border-box;
  358. cursor: pointer;
  359. &.selected {
  360. color: #409eff;
  361. font-weight: 700;
  362. background-color: #f5f7fa;
  363. }
  364. }
  365. .select_content_li:hover {
  366. background-color: #f5f7fa;
  367. }
  368. }
  369. }
  370. .select_modal_scroll{
  371. overflow: hidden;
  372. height: 160px;
  373. }
  374. // .select_content {
  375. // background-color: #fff;
  376. // .select_content_li {
  377. // padding: 12rpx;
  378. // }
  379. // }
  380. </style>