ld-select.vue 11 KB

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