momo-multipleSelect.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. <template>
  2. <view class="select-container" v-show="show" @touchmove.stop.prevent>
  3. <view
  4. class="mask"
  5. :class="activeClass ? 'mask-show' : ''"
  6. @tap="onCancel(true)"
  7. ></view>
  8. <view class="select-box" :class="activeClass ? 'select-box-show' : ''">
  9. <div style="height: 900rpx">
  10. <div style="display: flex">
  11. <uni-easyinput
  12. type="text"
  13. class="searchinput"
  14. v-model="formData.qymc"
  15. placeholder="输入关联企业名称(模糊查询)"
  16. style="height: 70rpx"
  17. />
  18. <van-button
  19. class="searchbutton"
  20. style="height: 70rpx"
  21. type="primary"
  22. @click="getCompaynList()"
  23. >搜索</van-button
  24. >
  25. </div>
  26. <div class="chooseCompanyTags">
  27. <scroll-view
  28. class="body"
  29. scroll-y="true"
  30. style="position: absolute; z-index: 1000"
  31. >
  32. <div
  33. v-for="(item, index) in companyList"
  34. :key="index"
  35. class="selectlist"
  36. >
  37. <div class="title_2">
  38. {{ item.label }}
  39. </div>
  40. <div
  41. v-if="!item.isSelected"
  42. class="title_1"
  43. @click="chosseTags(item, index)"
  44. >
  45. <uni-icons type="plusempty" size="30rpx"></uni-icons>
  46. </div>
  47. </div>
  48. </scroll-view>
  49. </div>
  50. </div>
  51. <view class="btn-view">
  52. <button
  53. class="btn"
  54. @click="openTagsDialog()"
  55. style="
  56. border-top-left-radius: 5rem;
  57. border-bottom-left-radius: 5rem;
  58. background: white;
  59. color: #1d18bc;
  60. border: 1px solid;
  61. "
  62. >
  63. 已选择({{ isSelectedList.length }})<van-icon name="arrow-down" />
  64. </button>
  65. <button
  66. style="
  67. border-radius: 0;
  68. border: 1px solid #1d18bc;
  69. border-top-right-radius: 5rem;
  70. border-bottom-right-radius: 5rem;
  71. "
  72. class="btn"
  73. @click="updown()"
  74. >
  75. 确认提交
  76. </button>
  77. </view>
  78. </view>
  79. <is-selected-tags
  80. ref="isSelectedTags"
  81. :isSelectedList="isSelectedList"
  82. @clearAll="clearAll"
  83. @updown="updown"
  84. @spiliceIsSelectedList="spiliceIsSelectedList"
  85. />
  86. </view>
  87. </template>
  88. <!-- 多选组件 -->
  89. <script>
  90. import isSelectedTags from "./isSelectedTags.vue";
  91. export default {
  92. data() {
  93. return {
  94. isSelectedList: [],
  95. companyList: [],
  96. isSelectList: [],
  97. formData: {},
  98. show: false, //是否显示
  99. activeClass: false, //激活样式状态
  100. selectedArr: [], //选择对照列表
  101. selectedArrOld: [], //选择对照列表上一次的数据
  102. };
  103. },
  104. components: {
  105. isSelectedTags,
  106. },
  107. onShow() {
  108. this.show = this.value;
  109. },
  110. methods: {
  111. async updown() {
  112. console.log(this.isSelectedList);
  113. this.$emit("confirm", this.isSelectedList);
  114. this.show = false;
  115. },
  116. clearAll() {
  117. this.isSelectedList = [];
  118. this.companyList.forEach((e) => {
  119. e.isSelected = false;
  120. this.$forceUpdate();
  121. });
  122. },
  123. spiliceIsSelectedList(item, index) {
  124. this.isSelectedList.splice(index, 1);
  125. let changeIndex = this.companyList.findIndex(
  126. (e) => e.tagCategoryId == item.tagCategoryId
  127. );
  128. this.companyList[changeIndex].isSelected = false;
  129. this.$forceUpdate();
  130. },
  131. chosseTags(item, index) {
  132. let filterIndex = this.isSelectedList.findIndex(
  133. (e) => e.value == item.value
  134. );
  135. if (filterIndex != -1) {
  136. this.$showToast("该标签与已选择标签重复");
  137. return;
  138. }
  139. if (item.isEffective == 1) {
  140. this.openChooseTimeDialog(item, index);
  141. } else {
  142. this.isSelectedList.push(this.companyList[index]);
  143. this.companyList[index].isSelected = true;
  144. this.companyList = [...this.companyList];
  145. }
  146. },
  147. openTagsDialog() {
  148. this.$refs.isSelectedTags.onOpen([]);
  149. },
  150. getAllList(data) {
  151. this.companyList = data;
  152. },
  153. getCompaynList() {
  154. console.log(this.companyList);
  155. console.log(this.selectedArr);
  156. this.$emit("getCompanyAll", this.formData.qymc);
  157. },
  158. // 设置默认选中通用办法
  159. setItemActiveState() {
  160. if (this.companyList.length && this.defaultSelected.length) {
  161. this.companyList.forEach((item, i) => {
  162. for (let n = 0; n < this.defaultSelected.length; n++) {
  163. if (
  164. !item.disabled &&
  165. item[this.valueName] === this.defaultSelected[n]
  166. ) {
  167. this.selectedArr.splice(i, 1, true);
  168. break;
  169. }
  170. }
  171. });
  172. }
  173. },
  174. /**
  175. * 选择事件
  176. * @index {Number} 点击下标
  177. */
  178. onSelected(index) {
  179. if (this.companyList[index].disabled) return;
  180. console.log(this.companyList[index]);
  181. if (!this.companyList[index].isSelect) {
  182. this.isSelectList.push(this.companyList[index].value);
  183. this.companyList[index].isSelect = true;
  184. } else {
  185. let deleteIndex = this.isSelectList.findIndex(
  186. (e) => e.value == this.companyList[index].value
  187. );
  188. this.isSelectList.splice(deleteIndex, 1);
  189. this.companyList[index].isSelect = false;
  190. }
  191. this.$forceUpdate();
  192. this.companyList = [...this.companyList];
  193. // let index2Active = this.selectedArr[index];
  194. // this.selectedArr.splice(index, 1, !index2Active);
  195. },
  196. // 取消事件
  197. onCancel(isMask) {
  198. if (!isMask || this.maskCloseAble) {
  199. this.show = false;
  200. this.selectedArr = JSON.parse(JSON.stringify(this.selectedArrOld));
  201. } else {
  202. return;
  203. }
  204. this.$emit("cancel");
  205. },
  206. // 返回去除了disabled状态后的对照列表
  207. returnWipeDisabledList() {
  208. let arr = [];
  209. this.selectedArr.forEach((el, index) => {
  210. if (!this.companyList[index].disabled) arr.push(el);
  211. });
  212. return arr;
  213. },
  214. // 全选/非全选事件
  215. onAllToggle() {
  216. let wipeDisabledList = this.returnWipeDisabledList();
  217. // 如果去除了disabled的对照列表有false的数据,代表未全选
  218. if (wipeDisabledList.includes(false)) {
  219. this.selectedArr.forEach((el, index) => {
  220. if (!this.companyList[index].disabled)
  221. this.selectedArr.splice(index, 1, true);
  222. });
  223. } else {
  224. this.selectedArr.forEach((el, index) => {
  225. if (!this.companyList[index].disabled)
  226. el = this.selectedArr.splice(index, 1, false);
  227. });
  228. }
  229. },
  230. // 确定事件
  231. onConfirm() {
  232. this.show = false;
  233. let selectedData = [];
  234. this.companyList.forEach((e, index) => {
  235. if (e.isSelect) {
  236. selectedData.push(this.companyList[index]);
  237. }
  238. });
  239. if (this.mode === "multiple") {
  240. this.$emit("confirm", selectedData);
  241. } else {
  242. let backData = selectedData[0] || {};
  243. this.$emit("confirm", backData);
  244. }
  245. },
  246. },
  247. computed: {
  248. // 返回是否全选
  249. isAll() {
  250. let wipeDisabledList = this.returnWipeDisabledList();
  251. if (!wipeDisabledList.length) return false;
  252. return !wipeDisabledList.includes(false);
  253. },
  254. },
  255. props: {
  256. // 双向绑定
  257. value: {
  258. type: Boolean,
  259. default: false,
  260. },
  261. // 取消按钮文字
  262. cancelText: {
  263. type: String,
  264. default: "取消",
  265. },
  266. // 确认按钮文字
  267. confirmText: {
  268. type: String,
  269. default: "确认",
  270. },
  271. // label对应的key名称
  272. labelName: {
  273. type: String,
  274. default: "label",
  275. },
  276. // value对应的key名称
  277. valueName: {
  278. type: String,
  279. default: "value",
  280. },
  281. // 是否允许点击遮罩层关闭
  282. maskCloseAble: {
  283. type: Boolean,
  284. default: true,
  285. },
  286. // 是否显示全选
  287. allShow: {
  288. type: Boolean,
  289. default: true,
  290. },
  291. // 模式
  292. mode: {
  293. type: String,
  294. default: "multiple",
  295. },
  296. // 默认选中值
  297. defaultSelected: {
  298. type: Array,
  299. default: function () {
  300. return [];
  301. },
  302. },
  303. // 数据源
  304. // data: {
  305. // type: Array,
  306. // required: true,
  307. // default: () => {
  308. // return [];
  309. // },
  310. // },
  311. },
  312. watch: {
  313. async value(newVal) {
  314. this.show = newVal;
  315. await this.$nextTick();
  316. this.activeClass = newVal;
  317. if (newVal) {
  318. this.selectedArrOld = JSON.parse(JSON.stringify(this.selectedArr));
  319. }
  320. },
  321. show(newVal) {
  322. this.$emit("input", newVal);
  323. this.$emit("change", newVal);
  324. },
  325. defaultSelected: {
  326. handler() {
  327. this.setItemActiveState();
  328. },
  329. deep: true,
  330. immediate: true,
  331. },
  332. },
  333. };
  334. </script>
  335. <style lang="scss">
  336. .searchbutton {
  337. button {
  338. height: 69rpx;
  339. line-height: 69rpx;
  340. }
  341. }
  342. .searchinput {
  343. .uni-easyinput__content-input {
  344. height: 70rpx;
  345. }
  346. }
  347. .btn-view {
  348. width: 100%;
  349. height: 100rpx;
  350. padding: 20rpx 10%;
  351. background-color: #ffffff;
  352. position: fixed;
  353. bottom: 0;
  354. left: 0;
  355. z-index: 1000;
  356. button {
  357. &::after {
  358. border: none;
  359. }
  360. }
  361. }
  362. </style>
  363. <style lang="scss" scoped>
  364. .selectlist {
  365. border-bottom: 2rpx solid #cccccc;
  366. display: flex;
  367. align-items: center;
  368. justify-content: space-between;
  369. }
  370. .chooseCompanyTags {
  371. .title_1 {
  372. font-size: 28rpx;
  373. font-weight: 500;
  374. color: #333333;
  375. text-align: right;
  376. height: 64rpx;
  377. line-height: 64rpx;
  378. }
  379. .title_2 {
  380. font-size: 26rpx;
  381. font-weight: 500;
  382. color: #777777;
  383. height: 64rpx;
  384. line-height: 64rpx;
  385. text-align: left;
  386. }
  387. .title_3 {
  388. margin-bottom: 12rpx;
  389. font-size: 26rpx;
  390. font-weight: 500;
  391. color: #777777;
  392. }
  393. .cell_1 {
  394. float: left;
  395. width: 10%;
  396. }
  397. .checkbox {
  398. border: 1px solid #cccccc;
  399. border-radius: 4rpx  4rpx  4rpx  4rpx;
  400. height: 30rpx;
  401. width: 30rpx;
  402. }
  403. .cell_2 {
  404. float: left;
  405. width: 90%;
  406. img {
  407. height: 60rpx;
  408. width: 60rpx;
  409. float: right;
  410. margin: 96rpx 0rpx 94rpx 0;
  411. }
  412. }
  413. .btn {
  414. width: 40%;
  415. height: 90rpx;
  416. background: #1d18bc;
  417. line-height: 90rpx;
  418. color: white;
  419. float: left;
  420. }
  421. .jichuback {
  422. width: calc(100% - 64rpx);
  423. margin: 32rpx;
  424. background: white;
  425. border-bottom: 2rpx solid #cccccc;
  426. }
  427. .tags_type {
  428. width: calc(100% - 64rpx);
  429. margin: 16rpx;
  430. }
  431. }
  432. .btn-view {
  433. width: 100%;
  434. height: 100rpx;
  435. padding: 20rpx 10%;
  436. background-color: #ffffff;
  437. position: fixed;
  438. bottom: 0;
  439. left: 0;
  440. z-index: 1000;
  441. }
  442. .btn {
  443. width: 40%;
  444. height: 90rpx;
  445. background: #1d18bc;
  446. line-height: 90rpx;
  447. color: white;
  448. float: left;
  449. }
  450. .select-container {
  451. width: 100vw;
  452. height: 100vh;
  453. position: fixed;
  454. left: 0;
  455. top: 0;
  456. z-index: 999;
  457. $paddingLR: 18rpx;
  458. .mask {
  459. width: 100%;
  460. height: 100%;
  461. background-color: $uni-bg-color-mask;
  462. opacity: 0;
  463. transition: opacity 0.3s;
  464. &.mask-show {
  465. opacity: 1;
  466. }
  467. }
  468. // 选择器内容区域
  469. .select-box {
  470. width: 100%;
  471. position: absolute;
  472. bottom: 0;
  473. left: 0;
  474. transform: translate3d(0px, 100%, 0px);
  475. background-color: $uni-bg-color;
  476. transition: all 0.3s;
  477. &.select-box-show {
  478. transform: translateZ(0);
  479. }
  480. .header {
  481. display: flex;
  482. box-sizing: border-box;
  483. width: 100%;
  484. justify-content: space-between;
  485. border-bottom: 1px solid $uni-border-color;
  486. line-height: 76rpx;
  487. font-size: 30rpx;
  488. padding: 0 $paddingLR;
  489. .cancel {
  490. color: $uni-text-color-grey;
  491. }
  492. .all {
  493. .all-active {
  494. &::after {
  495. display: inline-block;
  496. content: "✔";
  497. padding-left: 8rpx;
  498. }
  499. }
  500. }
  501. .confirm {
  502. color: $uni-color-primary;
  503. }
  504. }
  505. .body-warp {
  506. width: 100%;
  507. height: 30vh;
  508. box-sizing: border-box;
  509. padding: 20rpx $paddingLR;
  510. }
  511. .body {
  512. width: 96%;
  513. height: 70%;
  514. overflow-y: auto;
  515. padding: 2%;
  516. .empty-tips {
  517. margin-top: 25%;
  518. text-align: center;
  519. font-size: 26rpx;
  520. color: $uni-color-error;
  521. }
  522. }
  523. }
  524. }
  525. </style>