brand-select.vue 11 KB

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