You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

527 lines
12 KiB

6 months ago
  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') 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:left;
  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. </style>