brand-select.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <template>
  2. <view class="main">
  3. <view class="input" :class="{ disabledColor: disabled, inputWrap: border }">
  4. <!-- <view @click="showModal">
  5. <u--input :class="{disabledFontColor: disabled}" :value="_value" inputAlign="left" border="none" style="pointer-events: none;" :placeholder="placeholder"></u--input>
  6. </view> -->
  7. <view @click="showModal" class="ldSelectInput">
  8. <input v-model="_value" :class="{ disabledFontColor: disabled }" :placeholder="placeholder"
  9. style='pointer-events: none !important' />
  10. </view>
  11. <up-icon name="arrow-down-fill" color="#aaa" size="10" v-if="isShowIcon"></up-icon>
  12. <text v-if="clearable && !disabled" @click="empty" class="selectIcon iconcross"></text>
  13. </view>
  14. <view class="select-modal" :class="{ show: isShowModal }" @tap="hideModal">
  15. <view class="select-dialog" @tap.stop="" :style="{ backgroundColor: bgColor }">
  16. <view class="select-bar bg-white">
  17. <view class="action text-blue" @tap="cancelClick">{{ cancelText }}</view>
  18. <view class="action text-green" @tap="confirmClick">{{ confirmText }}</view>
  19. </view>
  20. <view class="select-content" v-if="list && list.length > 0">
  21. <up-search placeholder="请输入你要搜索的品牌名称" @change='handleSearch' v-model="searchKeyword" shape="square"
  22. :showAction="false" margin="10rpx 0 30rpx" :clearabled="true"></up-search>
  23. <view :class="['select-item', valueIndexOf(item) ? 'select-active-item' : '']"
  24. v-for="(item, index) in filterList" :key="index" @click="select(item)">
  25. <!-- :style="valueIndexOf(item) ? 'color:' +selectColor +' ;background-color:'+selectBgColor+';':'color:'+color+';'" -->
  26. <view class="title">{{ getLabelKeyValue(item) }}</view>
  27. <up-icon name="checkbox-mark" v-if="valueIndexOf(item)" class="checkboxMark" color="#108cff">
  28. </up-icon>
  29. <!-- <text class="selectIcon icongou" ></text> -->
  30. </view>
  31. </view>
  32. <view class="empty_wrap" v-else>
  33. {{ emptyText }}
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. export default {
  41. data() {
  42. return {
  43. isShowModal: false,
  44. searchKeyword: '',//搜索关键词
  45. filterList: []//过滤后的列表
  46. };
  47. },
  48. watch: {
  49. value(val) {
  50. this.syncDisplayFromValue(val);
  51. },
  52. modelValue(val) {
  53. this.syncDisplayFromValue(val);
  54. },
  55. list(newVal) {
  56. if (newVal && newVal.length > 0) {
  57. this.filterList = newVal
  58. }
  59. },
  60. },
  61. props: {
  62. isShowIcon: {
  63. type: Boolean,
  64. default: () => false,
  65. },
  66. border: {
  67. type: Boolean,
  68. default: () => false,
  69. },
  70. modelValue: {
  71. type: [Number, String, Array, Object],
  72. default: undefined
  73. },
  74. value: {
  75. type: [Number, String, Array, Object],
  76. default: null
  77. },
  78. name: {
  79. type: String,
  80. default: undefined
  81. },
  82. placeholder: { // 占位符
  83. default: "",
  84. type: String
  85. },
  86. emptyText: {
  87. default: "暂无数据",
  88. type: String
  89. },
  90. multiple: { // 是否多选
  91. default: false,
  92. type: Boolean
  93. },
  94. list: {
  95. default: () => [],
  96. type: Array
  97. },
  98. valueKey: { // 指定list中valueKey的值作为下拉框绑定内容
  99. default: 'value',
  100. type: String
  101. },
  102. labelKey: { // 指定list中labelKey的值作为下拉框显示内容
  103. default: 'label',
  104. type: String
  105. },
  106. disabled: {
  107. default: false,
  108. type: Boolean
  109. },
  110. clearable: {
  111. default: false,
  112. type: Boolean
  113. },
  114. cancelText: {
  115. default: "取消",
  116. type: String
  117. },
  118. confirmText: {
  119. default: "确定",
  120. type: String
  121. },
  122. color: {
  123. default: "#000000",
  124. type: String
  125. },
  126. selectColor: {
  127. default: "#0081ff",
  128. type: String
  129. },
  130. bgColor: {
  131. default: "#FFFFFF",
  132. type: String
  133. },
  134. selectBgColor: {
  135. default: "#FFFFFF",
  136. type: String
  137. }
  138. },
  139. computed: {
  140. boundValue() {
  141. return this.modelValue !== undefined && this.modelValue !== null ? this.modelValue : this.value;
  142. },
  143. _value: {
  144. get() {
  145. return this.boundValue != null ? this.get_value(this.boundValue) : "";
  146. },
  147. set(val) {
  148. this.$emit('input', val);
  149. }
  150. }
  151. },
  152. created() { },
  153. methods: {
  154. syncDisplayFromValue(val) {
  155. if (val !== undefined && val !== null) {
  156. const data = this.list.find((v) => this.getValueKeyValue(v) == val);
  157. if (data) {
  158. this.$emit("update:name", this.getLabelKeyValue(data));
  159. } else {
  160. this.$emit("update:name", val);
  161. }
  162. } else {
  163. this.$emit("update:name", undefined);
  164. }
  165. },
  166. get_value(val) { // 将数组值转换为以,隔开的字符串
  167. if (val || val === 0) {
  168. if (Array.isArray(val)) {
  169. let chooseAttr = []
  170. val.forEach(item => {
  171. let choose = this.list.find(temp => {
  172. let val_val = this.getValueKeyValue(temp)
  173. return item === val_val
  174. })
  175. chooseAttr.push(choose)
  176. })
  177. let values = chooseAttr.map(temp => {
  178. if (temp) {
  179. return this.getLabelKeyValue(temp)
  180. }
  181. return temp
  182. }).filter(temp => temp !== undefined && temp !== null).join(',')
  183. return values
  184. } else {
  185. let choose = this.list.find(temp => {
  186. let val_val = this.getValueKeyValue(temp)
  187. return val === val_val
  188. })
  189. // 如果找不到对应项,返回原值
  190. return choose ? this.getLabelKeyValue(choose) : val
  191. }
  192. } else {
  193. return ""
  194. }
  195. },
  196. select(item) { // 点击选项
  197. let val = this.getValueKeyValue(item);
  198. if (this.multiple) {
  199. let _value = Array.isArray(this.boundValue) ? this.boundValue.slice() : [];
  200. let index = _value.indexOf(val);
  201. if (index != -1) {
  202. _value.splice(index, 1);
  203. this.$emit('input', _value);
  204. this.$emit('update:modelValue', _value);
  205. this.$emit("change", item);
  206. } else {
  207. _value.push(val);
  208. this.$emit('input', _value);
  209. this.$emit('update:modelValue', _value);
  210. this.$emit("change", item);
  211. }
  212. } else {
  213. this.$emit('input', val);
  214. this.$emit('update:modelValue', val);
  215. this.$emit("update:name", item[this.labelKey]);
  216. this.$emit("change", item);
  217. this.hideModal();
  218. this.searchKeyword = '';
  219. }
  220. },
  221. valueIndexOf(item) {
  222. let val = this.getValueKeyValue(item);
  223. const v = this.boundValue;
  224. if (Array.isArray(v)) {
  225. return v.indexOf(val) != -1;
  226. }
  227. return v === val;
  228. },
  229. getLabelKeyValue(item) { // 获取label
  230. return item[this.labelKey]
  231. },
  232. getValueKeyValue(item) { // 获取value
  233. return item[this.valueKey]
  234. },
  235. empty() { // 清空
  236. if (this.multiple) {
  237. this.$emit('input', []);
  238. this.$emit('update:modelValue', []);
  239. } else {
  240. this.$emit("update:name", '');
  241. this.$emit('input', '');
  242. this.$emit('update:modelValue', '');
  243. }
  244. this.$emit("change", null);
  245. },
  246. cancelClick() { // 点击取消
  247. this.$emit('cancel', this._value)
  248. this.hideModal()
  249. this.searchKeyword = ''
  250. },
  251. confirmClick() { // 点击确定
  252. this.$emit('confirm', this._value)
  253. this.hideModal()
  254. },
  255. showModal() { // 显示model
  256. if (!this.disabled) {
  257. this.isShowModal = true
  258. }
  259. },
  260. hideModal() { // 隐藏model
  261. this.isShowModal = false
  262. },
  263. handleSearch(value) {
  264. //用户在搜索框中输入了内容,触发搜索事件
  265. // console.log('用户点击了搜索按钮,里面的值是', value)
  266. this.filterList = this.list.filter(item => {
  267. return this.getLabelKeyValue(item).indexOf(value) !== -1
  268. })
  269. }
  270. }
  271. }
  272. </script>
  273. <style>
  274. @font-face {
  275. font-family: "selectIcon";
  276. src: url('//at.alicdn.com/t/font_1833441_ycfzdhg2u3.eot?t=1590375117208');
  277. /* IE9 */
  278. src: url('//at.alicdn.com/t/font_1833441_ycfzdhg2u3.eot?t=1590375117208#iefix') format('embedded-opentype'),
  279. /* IE6-IE8 */
  280. 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==') format('woff2'),
  281. url('//at.alicdn.com/t/font_1833441_ycfzdhg2u3.woff?t=1590375117208') format('woff'),
  282. url('//at.alicdn.com/t/font_1833441_ycfzdhg2u3.ttf?t=1590375117208') format('truetype'),
  283. /* chrome, firefox, opera, Safari, Android, iOS 4.2+ */
  284. url('//at.alicdn.com/t/font_1833441_ycfzdhg2u3.svg?t=1590375117208#selectIcon') format('svg');
  285. /* iOS 4.1- */
  286. }
  287. .selectIcon {
  288. font-family: "selectIcon" !important;
  289. font-size: 16px;
  290. font-style: normal;
  291. -webkit-font-smoothing: antialiased;
  292. -moz-osx-font-smoothing: grayscale;
  293. }
  294. .icongou:before {
  295. content: "\e61c";
  296. }
  297. .iconcross:before {
  298. content: "\e61a";
  299. }
  300. </style>
  301. <style lang="scss" scoped>
  302. .ldSelectInput {
  303. width: 100%;
  304. }
  305. .disabledColor {
  306. background: #f5f7fa;
  307. }
  308. .disabledFontColor {
  309. color: #c0c4cc
  310. }
  311. .main {
  312. font-size: 28rpx;
  313. width: 100%;
  314. }
  315. .bg-white {
  316. background-color: #FFFFFF;
  317. }
  318. .text-blue {
  319. color: #0081ff;
  320. }
  321. .text-green {
  322. color: #39b54a;
  323. }
  324. .inputWrap {
  325. border: 2px solid #ddd;
  326. border-radius: 4px;
  327. }
  328. .input {
  329. display: flex;
  330. align-items: center;
  331. font-size: 28rpx;
  332. padding: 10rpx;
  333. // height: 60rpx;
  334. // padding: 10rpx 20rpx;
  335. // border-radius: 10rpx;
  336. // border-style: solid;
  337. // border-width: 1rpx;
  338. // border-color: rgba(0, 0, 0, 0.1);
  339. input {
  340. flex: 1;
  341. }
  342. }
  343. .select-modal {
  344. position: fixed;
  345. top: 0;
  346. right: 0;
  347. bottom: 0;
  348. left: 0;
  349. z-index: 9999;
  350. opacity: 0;
  351. outline: 0;
  352. text-align: center;
  353. -ms-transform: scale(1.185);
  354. transform: scale(1.185);
  355. backface-visibility: hidden;
  356. perspective: 2000rpx;
  357. background: rgba(0, 0, 0, 0.6);
  358. transition: all 0.3s ease-in-out 0s;
  359. pointer-events: none;
  360. margin-bottom: -1000rpx;
  361. &::before {
  362. content: "\200B";
  363. display: inline-block;
  364. height: 100%;
  365. vertical-align: bottom;
  366. }
  367. .select-dialog {
  368. display: inline-block;
  369. margin-left: auto;
  370. margin-right: auto;
  371. background-color: #f8f8f8;
  372. overflow: hidden;
  373. width: 100%;
  374. border-radius: 0;
  375. position: absolute;
  376. bottom: 0%;
  377. left: 50%;
  378. transform: translate(-50%, 0%);
  379. .empty_wrap {
  380. text-align: center;
  381. padding: 30rpx;
  382. color: #999;
  383. }
  384. .select-content {
  385. // background-color: #F1F1F1;
  386. max-height: 420rpx;
  387. overflow: auto;
  388. padding: 0 60rpx;
  389. .select-item {
  390. padding: 20rpx;
  391. display: flex;
  392. position: relative;
  393. background: #f4f8fe;
  394. margin-bottom: 30rpx;
  395. border-radius: 10rpx;
  396. .title {
  397. flex: 1;
  398. overflow-wrap: break-word;
  399. word-break: break-all;
  400. }
  401. .checkboxMark {
  402. position: absolute;
  403. right: 0%;
  404. top: 50%;
  405. transform: translate(-50%, -50%);
  406. }
  407. ::v-deep .u-icon__icon {
  408. font-size: 40rpx !important;
  409. display: inline-block;
  410. }
  411. }
  412. .select-active-item {
  413. box-sizing: border-box;
  414. background: #FFFFFF;
  415. border: #0081ff 1px solid !important;
  416. }
  417. }
  418. }
  419. }
  420. .select-modal.show {
  421. opacity: 1;
  422. transition-duration: 0.3s;
  423. -ms-transform: scale(1);
  424. transform: scale(1);
  425. overflow-x: hidden;
  426. overflow-y: auto;
  427. pointer-events: auto;
  428. margin-bottom: 0;
  429. }
  430. .select-bar {
  431. padding: 0 20rpx;
  432. display: flex;
  433. position: relative;
  434. align-items: center;
  435. min-height: 80rpx;
  436. justify-content: space-between;
  437. .action {
  438. display: flex;
  439. align-items: center;
  440. height: 100%;
  441. justify-content: center;
  442. max-width: 100%;
  443. }
  444. }
  445. </style>