ld-select.vue 11 KB

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