group-select.vue 11 KB

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