ld-select.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <template>
  2. <view class="main">
  3. <view class="input" :style="disabled?'background-color:#f5f7fa':''">
  4. <input v-model="_value" :style="disabled?'color:#c0c4cc':''" :placeholder="placeholder"
  5. disabled />
  6. <text v-if="clearable&&!disabled" @click="empty" class="selectIcon iconcross"></text>
  7. <view class="mask" @click="showModal"></view>
  8. </view>
  9. <view class="select-modal" :class="isShowModal?'show':''" @tap="hideModal">
  10. <view class="select-dialog" @tap.stop="" :style="{backgroundColor:bgColor}">
  11. <view class="select-bar bg-white">
  12. <view class="action text-blue" @tap="cancelClick">{{cancelText}}</view>
  13. <view class="action text-green" @tap="confirmClick">{{confirmText}}</view>
  14. </view>
  15. <scroll-view scroll-y class="select-content">
  16. <view class="select-item" v-for="(item,index) in list" :key="index"
  17. :style="valueIndexOf(item)?'color:'+selectColor+';background-color:'+selectBgColor+';':'color:'+color+';'"
  18. @click="select(item)">
  19. <view class="title">{{getLabelKeyValue(item)}}</view>
  20. <text class="selectIcon icongou" v-if="valueIndexOf(item)"></text>
  21. </view>
  22. </scroll-view>
  23. </view>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. export default {
  29. data() {
  30. return {
  31. isShowModal: false
  32. };
  33. },
  34. props: {
  35. value: {
  36. type: [Number, String, Array, Object],
  37. default: null
  38. },
  39. placeholder: { // 占位符
  40. default: "",
  41. type: String
  42. },
  43. multiple: { // 是否多选
  44. default: false,
  45. type: Boolean
  46. },
  47. list: {
  48. default: () => [],
  49. type: Array
  50. },
  51. valueKey: { // 指定list中valueKey的值作为下拉框绑定内容
  52. default: 'value',
  53. type: String
  54. },
  55. labelKey: { // 指定list中labelKey的值作为下拉框显示内容
  56. default: 'label',
  57. type: String
  58. },
  59. disabled: {
  60. default: false,
  61. type: Boolean
  62. },
  63. clearable: {
  64. default: false,
  65. type: Boolean
  66. },
  67. cancelText: {
  68. default: "取消",
  69. type: String
  70. },
  71. confirmText: {
  72. default: "确定",
  73. type: String
  74. },
  75. color: {
  76. default: "#000000",
  77. type: String
  78. },
  79. selectColor: {
  80. default: "#49b856",
  81. type: String
  82. },
  83. bgColor: {
  84. default: "#F1F1F1",
  85. type: String
  86. },
  87. selectBgColor: {
  88. default: "#FFFFFF",
  89. type: String
  90. }
  91. },
  92. computed: {
  93. _value: {
  94. get() {
  95. return this.get_value(this.value);
  96. },
  97. set(val) {
  98. this.$emit('change', val);
  99. }
  100. }
  101. },
  102. created() {},
  103. methods: {
  104. get_value(val) { // 将数组值转换为以,隔开的字符串
  105. if (val || val === 0) {
  106. if (Array.isArray(val)) {
  107. let chooseAttr = []
  108. val.forEach(item => {
  109. let choose = this.list.find(temp => {
  110. let val_val = this.getValueKeyValue(temp)
  111. return item === val_val
  112. })
  113. chooseAttr.push(choose)
  114. })
  115. let values = chooseAttr.map(temp => this.getLabelKeyValue(temp)).join(',')
  116. return values
  117. } else {
  118. let choose = this.list.find(temp => {
  119. let val_val = this.getValueKeyValue(temp)
  120. return val === val_val
  121. })
  122. return this.getLabelKeyValue(choose)
  123. }
  124. } else {
  125. return ""
  126. }
  127. },
  128. select(item) { // 点击选项
  129. let val = this.getValueKeyValue(item);
  130. if (this.multiple) {
  131. let _value = this.value;
  132. let index = _value.indexOf(val);
  133. if (index != -1) {
  134. _value.splice(index, 1)
  135. this.$emit('change', _value)
  136. } else {
  137. _value.push(val)
  138. this.$emit('change', _value)
  139. }
  140. } else {
  141. this.$emit('change', val)
  142. this.hideModal()
  143. }
  144. },
  145. valueIndexOf(item) {
  146. let val = this.getValueKeyValue(item);
  147. if (Array.isArray(this.value)) {
  148. return this.value.indexOf(val) != -1
  149. } else {
  150. return this.value === val
  151. }
  152. },
  153. getLabelKeyValue(item) { // 获取label
  154. return item[this.labelKey]
  155. },
  156. getValueKeyValue(item) { // 获取value
  157. return item[this.valueKey]
  158. },
  159. empty() { // 清空
  160. if (this.multiple) {
  161. this.$emit('change', [])
  162. } else {
  163. this.$emit('change', '')
  164. }
  165. },
  166. cancelClick() { // 点击取消
  167. this.$emit('cancel', this._value)
  168. this.hideModal()
  169. },
  170. confirmClick() { // 点击确定
  171. this.$emit('confirm', this._value)
  172. this.hideModal()
  173. },
  174. showModal() { // 显示model
  175. if (!this.disabled) {
  176. this.isShowModal = true
  177. this.$emit('parentCancel', 1)
  178. }
  179. },
  180. hideModal() { // 隐藏model
  181. this.isShowModal = false
  182. this.$emit('parentCancel', 0)
  183. }
  184. }
  185. }
  186. </script>
  187. <style>
  188. @font-face {
  189. font-family: "selectIcon";
  190. src: url('../../static/font_components/font_1833441_ycfzdhg2u3.eot?t=1590375117208');
  191. /* IE9 */
  192. src: url('../../static/font_components/font_1833441_ycfzdhg2u3.eot?t=1590375117208#iefix') format('embedded-opentype'),
  193. /* IE6-IE8 */
  194. 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'),
  195. url('../../static/font_components/font_1833441_ycfzdhg2u3.woff?t=1590375117208') format('woff'),
  196. url('../../static/font_components/font_1833441_ycfzdhg2u3.ttf?t=1590375117208') format('truetype'),
  197. /* chrome, firefox, opera, Safari, Android, iOS 4.2+ */
  198. url('../../static/font_components/font_1833441_ycfzdhg2u3.svg?t=1590375117208#selectIcon') format('svg');
  199. /* iOS 4.1- */
  200. }
  201. .selectIcon {
  202. font-family: "selectIcon" !important;
  203. font-size: 16px;
  204. font-style: normal;
  205. -webkit-font-smoothing: antialiased;
  206. -moz-osx-font-smoothing: grayscale;
  207. }
  208. .icongou:before {
  209. content: "\e61c";
  210. }
  211. .iconcross:before {
  212. content: "\e61a";
  213. }
  214. </style>
  215. <style lang="scss" scoped>
  216. .main {
  217. font-size: 28rpx;
  218. }
  219. .bg-white {
  220. background-color: #FFFFFF;
  221. }
  222. .text-blue {
  223. color: #ccc;
  224. }
  225. .text-green {
  226. color: #49b856;
  227. }
  228. .input {
  229. display: flex;
  230. align-items: center;
  231. font-size: 28rpx;
  232. height: 60rpx;
  233. padding: 0 20rpx;
  234. border-radius: 10rpx;
  235. border-style: solid;
  236. border-width: 1rpx;
  237. border-color: rgba(0, 0, 0, 0.1);
  238. position: relative;
  239. .mask{
  240. content: '';
  241. position: absolute;
  242. top: 0;
  243. right: 0;
  244. bottom: 0;
  245. left: 0;
  246. }
  247. input {
  248. flex: 1;
  249. }
  250. }
  251. .select-modal {
  252. width: 100vw;
  253. height: 100vh;
  254. position: fixed;
  255. top: 0;
  256. right: 0;
  257. z-index: 9999;
  258. opacity: 0;
  259. outline: 0;
  260. text-align: center;
  261. -ms-transform: scale(1.185);
  262. transform: scale(1.185);
  263. backface-visibility: hidden;
  264. perspective: 2000rpx;
  265. background: rgba(0, 0, 0, 0.6);
  266. transition: all 0.1s ease-in-out 0s;
  267. pointer-events: none;
  268. margin-bottom: -1000rpx;
  269. &::before {
  270. content: "\200B";
  271. display: inline-block;
  272. height: 100%;
  273. vertical-align: bottom;
  274. }
  275. .select-dialog {
  276. position: relative;
  277. display: inline-block;
  278. margin-left: auto;
  279. margin-right: auto;
  280. background-color: #f8f8f8;
  281. overflow: hidden;
  282. width: 100%;
  283. border-radius: 0;
  284. .select-content {
  285. max-height: 420rpx;
  286. overflow: auto;
  287. .select-item {
  288. padding: 20rpx;
  289. display: flex;
  290. .title {
  291. flex: 1;
  292. }
  293. }
  294. }
  295. }
  296. }
  297. .select-modal.show {
  298. opacity: 1;
  299. transition-duration: 0.1s;
  300. -ms-transform: scale(1);
  301. transform: scale(1);
  302. overflow-x: hidden;
  303. overflow-y: auto;
  304. pointer-events: auto;
  305. margin-bottom: 0;
  306. }
  307. .select-bar {
  308. padding: 0 20rpx;
  309. display: flex;
  310. position: relative;
  311. align-items: center;
  312. min-height: 80rpx;
  313. justify-content: space-between;
  314. .action {
  315. display: flex;
  316. align-items: center;
  317. height: 100%;
  318. justify-content: center;
  319. max-width: 100%;
  320. }
  321. }
  322. </style>