zqs-select.vue 12 KB

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