zqs-select.vue 12 KB

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