zqs-select.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <template>
  2. <view class="main">
  3. <view class="input" @click="showModal">
  4. <input
  5. v-model="_value"
  6. :style="disabled ? 'color:#c0c4cc;pointer-events: none' : 'pointer-events: none'"
  7. :placeholder="placeholder"
  8. placeholder-style="color: rgba(102, 102, 102, 0.25);"
  9. placeholder-class="zqs-select-placeholder-class"
  10. disabled
  11. />
  12. <image
  13. v-if="showArrow && !_value"
  14. src="@/static/right_icon.png"
  15. class="selector-icon"
  16. ></image>
  17. </view>
  18. <view
  19. class="select-modal"
  20. :class="isShowModal ? 'show' : ''"
  21. @tap="hideModal"
  22. >
  23. <view
  24. class="select-dialog"
  25. @tap.stop=""
  26. :style="{ backgroundColor: bgColor }"
  27. >
  28. <view class="title-main">
  29. <text class="title-detail">{{ title }}</text>
  30. </view>
  31. <view class="search-box" v-if="showSearch">
  32. <input
  33. class="search-input"
  34. confirm-type="search"
  35. v-model="searchInput"
  36. placeholder="输入内容进行查询"
  37. placeholder-style="color:rgba(102, 102, 102, 0.25);"
  38. />
  39. <!-- <text v-if="showSearchBtn" class="search-text" @click="handleSearch">
  40. 搜索
  41. </text> -->
  42. </view>
  43. <view class="select-content">
  44. <view
  45. class="select-item"
  46. v-for="(item, index) in list"
  47. :key="index"
  48. :style="
  49. valueIndexOf(item)
  50. ? 'color:' +
  51. selectColor +
  52. ';background-color:' +
  53. selectBgColor +
  54. ';'
  55. : 'color:' + color + ';'
  56. "
  57. v-show="item.show!=2"
  58. @click="select(item)"
  59. >
  60. <view class="title">{{ getLabelKeyValue(item) }}</view>
  61. <text class="selectIcon icongou" v-if="valueIndexOf(item)"></text>
  62. </view>
  63. </view>
  64. <view class="select-bar bg-white">
  65. <button
  66. plain="true"
  67. class="mini-btn action"
  68. type="default"
  69. size="default"
  70. style="color: #999;
  71. border: 1px solid #999;"
  72. @tap="empty"
  73. >
  74. {{ emptyText }}
  75. </button>
  76. <button
  77. class="mini-btn action"
  78. type="primary"
  79. size="default"
  80. :style="{'background-color':
  81. selectColor}"
  82. @tap="confirmClick"
  83. >
  84. {{ confirmText }}
  85. </button>
  86. </view>
  87. </view>
  88. </view>
  89. </view>
  90. </template>
  91. <script>
  92. export default {
  93. name: 'zqsSelect',
  94. data() {
  95. return {
  96. isShowModal: false,
  97. searchInput: '',
  98. options: [],
  99. }
  100. },
  101. props: {
  102. showSearch: {
  103. // 是否显示搜索框
  104. type: Boolean,
  105. default: true,
  106. },
  107. value: {
  108. type: [Number, String, Array, Object],
  109. default: null,
  110. },
  111. placeholder: {
  112. // 占位符
  113. default: '',
  114. type: String,
  115. },
  116. multiple: {
  117. // 是否多选
  118. default: false,
  119. type: Boolean,
  120. },
  121. list: {
  122. default: () => [],
  123. type: Array,
  124. },
  125. valueKey: {
  126. // 指定list中valueKey的值作为下拉框绑定内容
  127. default: 'value',
  128. type: String,
  129. },
  130. labelKey: {
  131. // 指定list中labelKey的值作为下拉框显示内容
  132. default: 'label',
  133. type: String,
  134. },
  135. disabled: {
  136. default: false,
  137. type: Boolean,
  138. },
  139. clearable: {
  140. default: false,
  141. type: Boolean,
  142. },
  143. emptyText: {
  144. default: '重置',
  145. type: String,
  146. },
  147. title: {
  148. default: '选择内容',
  149. type: String,
  150. },
  151. confirmText: {
  152. default: '确定',
  153. type: String,
  154. },
  155. color: {
  156. default: '#000000',
  157. type: String,
  158. },
  159. selectColor: {
  160. default: '#0081ff',
  161. type: String,
  162. },
  163. bgColor: {
  164. default: '#ffffff',
  165. type: String,
  166. },
  167. selectBgColor: {
  168. default: '#FFFFFF',
  169. type: String,
  170. },
  171. valueType: {
  172. default: 'single',
  173. type: String, // single || all
  174. },
  175. showSearchBtn: {
  176. default: true,
  177. type: Boolean, // single || all
  178. },
  179. showArrow: {
  180. type: Boolean,
  181. default: true,
  182. },
  183. },
  184. emits: ['openDeepScroll', 'closeDeepScroll'],
  185. computed: {
  186. _value: {
  187. get() {
  188. return this.get_value(this.value)
  189. },
  190. set(val) {
  191. this.$emit('input', val)
  192. },
  193. },
  194. },
  195. created() {},
  196. methods: {
  197. handleSearch() {
  198. this.$emit('search', this.searchInput)
  199. },
  200. get_value(val) {
  201. // 将数组值转换为以,隔开的字符串
  202. if (val || val === 0) {
  203. if (Array.isArray(val)) {
  204. let chooseAttr = []
  205. val.forEach((item) => {
  206. let choose = this.list.find((temp) => {
  207. let val_val = this.getValueKeyValue(temp)
  208. return item === val_val
  209. })
  210. // 判断是否存在
  211. if (choose) {
  212. chooseAttr.push(choose)
  213. }
  214. })
  215. let values = ''
  216. if (chooseAttr.length > 0) {
  217. values = chooseAttr
  218. .map((temp) => this.getLabelKeyValue(temp))
  219. .join(',')
  220. }
  221. return values
  222. } else {
  223. let choose = this.list.find((temp) => {
  224. let val_val = this.getValueKeyValue(temp)
  225. return val === val_val
  226. })
  227. if (choose) {
  228. return this.getLabelKeyValue(choose)
  229. } else {
  230. return val
  231. }
  232. }
  233. } else {
  234. return ''
  235. }
  236. },
  237. select(item) {
  238. // 点击选项
  239. let val = this.getValueKeyValue(item)
  240. if (this.multiple) {
  241. let _value = this.value
  242. let index = _value ? _value.indexOf(val) : -1
  243. if (index != -1) {
  244. _value.splice(index, 1)
  245. this.options.splice(index, 1)
  246. this.$emit('input', _value)
  247. } else {
  248. _value.push(val)
  249. this.options.push(item)
  250. this.$emit('input', _value)
  251. }
  252. this.$emit('change', item)
  253. } else {
  254. let label = this.getLabelKeyValue(item)
  255. if (this._value) {
  256. if (label.indexOf(this._value) !== -1) {
  257. this.$emit('input', '')
  258. } else {
  259. this.$emit('input', val)
  260. }
  261. } else {
  262. this.$emit('input', val)
  263. }
  264. // 单选选中的当前项所有
  265. this.$emit('change', item)
  266. this.hideModal()
  267. }
  268. },
  269. valueIndexOf(item) {
  270. let val = this.getValueKeyValue(item)
  271. if (Array.isArray(this.value)) {
  272. return this.value.indexOf(val) != -1
  273. } else {
  274. return this.value === val
  275. }
  276. },
  277. getLabelKeyValue(item) {
  278. // 获取label
  279. return item[this.labelKey]
  280. },
  281. getValueKeyValue(item) {
  282. // 获取value
  283. return item[this.valueKey]
  284. },
  285. empty() {
  286. // 清空
  287. if (this.multiple) {
  288. this.$emit('change', [])
  289. this.$emit('input', [])
  290. } else {
  291. this.$emit('change', '')
  292. this.$emit('input', '')
  293. }
  294. },
  295. // cancelClick() {
  296. // // 点击取消
  297. // this.$emit('cancel', this._value)
  298. // this.hideModal()
  299. // },
  300. confirmClick() {
  301. // 点击确定
  302. if (this.valueType === 'all') {
  303. this.$emit('confirm', this.options)
  304. } else {
  305. this.$emit('confirm', this._value)
  306. }
  307. this.hideModal()
  308. },
  309. showModal() {
  310. // 显示model
  311. if (!this.disabled) {
  312. this.isShowModal = true
  313. // 打开禁止穿透滚动
  314. this.$emit('openDeepScroll')
  315. }
  316. },
  317. hideModal() {
  318. // 隐藏model
  319. this.isShowModal = false
  320. // 关闭禁止穿透滚动
  321. this.$emit('closeDeepScroll')
  322. },
  323. },
  324. watch: {
  325. searchInput(val) {
  326. var _this=this
  327. this.list.find((temp) => {
  328. let label = _this.getLabelKeyValue(temp)
  329. if(val){
  330. if(label.indexOf(val)!=-1){
  331. temp.show=1
  332. }else{
  333. temp.show=2
  334. }
  335. }else{
  336. temp.show=1
  337. }
  338. })
  339. // if (!this.$props.showSearchBtn) this.$emit('search', val)
  340. },
  341. },
  342. }
  343. </script>
  344. <style>
  345. @font-face {
  346. font-family: 'selectIcon';
  347. src: url('//at.alicdn.com/t/font_1833441_ycfzdhg2u3.eot?t=1590375117208');
  348. /* IE9 */
  349. src: url('//at.alicdn.com/t/font_1833441_ycfzdhg2u3.eot?t=1590375117208#iefix')
  350. format('embedded-opentype'),
  351. /* IE6-IE8 */
  352. url('data:application/x-font-woff2;charset=utf-8;base64,d09GMgABAAAAAAMEAAsAAAAABvQAAAK4AAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHEIGVgCDBgqBRIFCATYCJAMMCwgABCAFhQUHNRsfBsg+QCa3uoO0oAJTMwhxVu965keqWBy1hkbwtfzWb2Z279/shRhJisKF6FApKLI7oyBbpAaHo3w24k+ca9EUJbDmjaeznUdZ/FOUlkWdJ33rizZY/Pw6J5Xw0qKYxHTMesePHVT6EFpaC4zV70sKi2bYgNPc1w0WHnDVC/e/UnNTgyP+4Jq6BBpIHoisgypLaIAFEtU0wgeaIG8Yu4nAIZwnUK1QgFfOT6nUUoBpgXjj2lqplTMpiuXtCW3N2iK+aPTS2/Qdnzny8d+5IEiaDMy99exklra//FrKnX48pChmgrq5QcYRQCEe17ruqgqLAKv8WntwqwhpLms/nB5yW/iHRxJEC0QOgT3NnfgF01NBKvOuIzNoZdh5gJuAeGrsozE8vOJ7u5D832oz55039W5G+S52K0H+zNf1TJz07k26kqoQybRfwVFV4rjDS/K8EXUyuF1cXnT3weKS9Rvdm/xe7h8oA1hLwOR18R+Y4n4zwpr4z5SU089Vc+cpfWL+mn5APmT3Z39jeOs/GbWjK+DnmsuL/u6ehMX4j4yedSVkAUUuPh3TY022MtKZUEOtPqCb8Bkvnr5XT6imU0gGrEJW7aAL/gw0OhegVV2F6pC7uTOppirKIA4MFQhTrpCM+AbZlDu64L/QmAkQWlMhQXU75D07O9Gtl0PUYjTBLyAzOLNQYtypIEEjvsXtBLQTooV2nrQrGEau2gKmZlR4L8gwnGtBJbUn1diCOOQUnEkTkRAOeci9KHOQxvFro+tx3ZcGAaeljstCSBNDJuArgIyBYyy6OdZxAhHIELu1IC9AtgShCVtLltEKrSff1XoHJo3RC33hM63o3j6pSNkmqmIWEAtxFHB2OwoRBAfyeqE3r2ogHeF42dBhs7gvf7CukH5MmlUGOCpHihxFfs6TehDyKCqVAA==')
  353. format('woff2'),
  354. url('//at.alicdn.com/t/font_1833441_ycfzdhg2u3.woff?t=1590375117208')
  355. format('woff'),
  356. url('//at.alicdn.com/t/font_1833441_ycfzdhg2u3.ttf?t=1590375117208')
  357. format('truetype'),
  358. /* chrome, firefox, opera, Safari, Android, iOS 4.2+ */
  359. url('//at.alicdn.com/t/font_1833441_ycfzdhg2u3.svg?t=1590375117208#selectIcon')
  360. format('svg');
  361. /* iOS 4.1- */
  362. }
  363. .title-main {
  364. display: flex;
  365. justify-content: center;
  366. width: 100%;
  367. }
  368. .title-detail {
  369. display: flex;
  370. width: 88%;
  371. justify-content: center;
  372. padding: 30rpx 0;
  373. /* border-bottom: 1rpx solid #e6e1e1; */
  374. }
  375. .selectIcon {
  376. font-family: 'selectIcon' !important;
  377. font-size: 16px;
  378. font-style: normal;
  379. -webkit-font-smoothing: antialiased;
  380. -moz-osx-font-smoothing: grayscale;
  381. }
  382. .icongou:before {
  383. content: '\e61c';
  384. }
  385. .iconcross:before {
  386. content: '\e61a';
  387. }
  388. </style>
  389. <style lang="scss" scoped>
  390. .main {
  391. font-size: 28rpx;
  392. }
  393. .bg-white {
  394. background-color: #ffffff;
  395. }
  396. .input {
  397. display: flex;
  398. align-items: center;
  399. // font-size: 28rpx;
  400. // height: 60rpx;
  401. // padding: 10rpx 20rpx;
  402. // border-radius: 10rpx;
  403. // border-style: solid;
  404. // border-width: 1rpx;
  405. // border-color: rgba(0, 0, 0, 0.1);
  406. input {
  407. flex: 1;
  408. text-align: left;
  409. color: #333333;
  410. }
  411. .selector-icon {
  412. width: 32rpx;
  413. height: 36rpx;
  414. vertical-align: middle;
  415. }
  416. }
  417. .select-modal {
  418. position: fixed;
  419. top: 0;
  420. right: 0;
  421. bottom: 0;
  422. left: 0;
  423. z-index: 9999;
  424. opacity: 0;
  425. outline: 0;
  426. text-align: center;
  427. -ms-transform: scale(1.185);
  428. transform: scale(1.185);
  429. backface-visibility: hidden;
  430. perspective: 2000rpx;
  431. background: rgba(0, 0, 0, 0.6);
  432. transition: all 0.3s ease-in-out 0s;
  433. pointer-events: none;
  434. margin-bottom: -1000rpx;
  435. &::before {
  436. content: '\200B';
  437. display: inline-block;
  438. height: 100%;
  439. vertical-align: bottom;
  440. }
  441. .select-dialog {
  442. position: absolute;
  443. left: 0;
  444. bottom: 0;
  445. display: inline-block;
  446. margin-left: auto;
  447. margin-right: auto;
  448. background-color: #f8f8f8;
  449. overflow: hidden;
  450. width: 100%;
  451. border-radius: 0;
  452. .select-content {
  453. // background-color: #F1F1F1;
  454. height: 60vh;
  455. overflow: auto;
  456. .select-item {
  457. text-align: left;
  458. padding: 20rpx 80rpx;
  459. display: flex;
  460. ::after {
  461. content: '';
  462. width: 100%;
  463. height: 1px;
  464. display: block;
  465. margin: 0 auto;
  466. border-bottom: 2px solid #f5f2f2;
  467. padding: 1px;
  468. }
  469. .title {
  470. flex: 1;
  471. }
  472. }
  473. }
  474. }
  475. }
  476. .select-modal.show {
  477. opacity: 1;
  478. transition-duration: 0.3s;
  479. -ms-transform: scale(1);
  480. transform: scale(1);
  481. overflow-x: hidden;
  482. overflow-y: auto;
  483. pointer-events: auto;
  484. margin-bottom: 0;
  485. }
  486. .select-bar {
  487. padding: 0 80rpx;
  488. display: flex;
  489. position: relative;
  490. align-items: center;
  491. min-height: 80rpx;
  492. justify-content: space-between;
  493. margin-bottom: 50rpx;
  494. .action {
  495. display: flex;
  496. align-items: center;
  497. height: 78rpx;
  498. justify-content: center;
  499. max-width: 100%;
  500. padding: 0 100rpx;
  501. }
  502. }
  503. .search-box {
  504. display: flex;
  505. margin: 10rpx 0;
  506. align-items: center;
  507. padding: 10rpx 40rpx;
  508. }
  509. .search-input {
  510. display: flex;
  511. flex: 1;
  512. // width: 560rpx;
  513. height: 67rpx;
  514. line-height: 67rpx;
  515. border-radius: 40rpx;
  516. background: #f5f2f2;
  517. }
  518. .search-text {
  519. padding-left: 30rpx;
  520. }
  521. </style>