group-select.vue 12 KB

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