momo-multipleSelect.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 = [];
  152. this.companyList = data;
  153. },
  154. getCompaynList() {
  155. console.log(this.companyList);
  156. console.log(this.selectedArr);
  157. this.$emit("getCompanyAll", this.formData.qymc);
  158. },
  159. // 设置默认选中通用办法
  160. setItemActiveState() {
  161. if (this.companyList.length && this.defaultSelected.length) {
  162. this.companyList.forEach((item, i) => {
  163. for (let n = 0; n < this.defaultSelected.length; n++) {
  164. if (
  165. !item.disabled &&
  166. item[this.valueName] === this.defaultSelected[n]
  167. ) {
  168. this.selectedArr.splice(i, 1, true);
  169. break;
  170. }
  171. }
  172. });
  173. }
  174. },
  175. /**
  176. * 选择事件
  177. * @index {Number} 点击下标
  178. */
  179. onSelected(index) {
  180. if (this.companyList[index].disabled) return;
  181. console.log(this.companyList[index]);
  182. if (!this.companyList[index].isSelect) {
  183. this.isSelectList.push(this.companyList[index].value);
  184. this.companyList[index].isSelect = true;
  185. } else {
  186. let deleteIndex = this.isSelectList.findIndex(
  187. (e) => e.value == this.companyList[index].value
  188. );
  189. this.isSelectList.splice(deleteIndex, 1);
  190. this.companyList[index].isSelect = false;
  191. }
  192. this.$forceUpdate();
  193. this.companyList = [...this.companyList];
  194. // let index2Active = this.selectedArr[index];
  195. // this.selectedArr.splice(index, 1, !index2Active);
  196. },
  197. // 取消事件
  198. onCancel(isMask) {
  199. if (!isMask || this.maskCloseAble) {
  200. this.show = false;
  201. this.selectedArr = JSON.parse(JSON.stringify(this.selectedArrOld));
  202. } else {
  203. return;
  204. }
  205. this.$emit("cancel");
  206. },
  207. // 返回去除了disabled状态后的对照列表
  208. returnWipeDisabledList() {
  209. let arr = [];
  210. this.selectedArr.forEach((el, index) => {
  211. if (!this.companyList[index].disabled) arr.push(el);
  212. });
  213. return arr;
  214. },
  215. // 全选/非全选事件
  216. onAllToggle() {
  217. let wipeDisabledList = this.returnWipeDisabledList();
  218. // 如果去除了disabled的对照列表有false的数据,代表未全选
  219. if (wipeDisabledList.includes(false)) {
  220. this.selectedArr.forEach((el, index) => {
  221. if (!this.companyList[index].disabled)
  222. this.selectedArr.splice(index, 1, true);
  223. });
  224. } else {
  225. this.selectedArr.forEach((el, index) => {
  226. if (!this.companyList[index].disabled)
  227. el = this.selectedArr.splice(index, 1, false);
  228. });
  229. }
  230. },
  231. // 确定事件
  232. onConfirm() {
  233. this.show = false;
  234. let selectedData = [];
  235. this.companyList.forEach((e, index) => {
  236. if (e.isSelect) {
  237. selectedData.push(this.companyList[index]);
  238. }
  239. });
  240. if (this.mode === "multiple") {
  241. this.$emit("confirm", selectedData);
  242. } else {
  243. let backData = selectedData[0] || {};
  244. this.$emit("confirm", backData);
  245. }
  246. },
  247. },
  248. computed: {
  249. // 返回是否全选
  250. isAll() {
  251. let wipeDisabledList = this.returnWipeDisabledList();
  252. if (!wipeDisabledList.length) return false;
  253. return !wipeDisabledList.includes(false);
  254. },
  255. },
  256. props: {
  257. // 双向绑定
  258. value: {
  259. type: Boolean,
  260. default: false,
  261. },
  262. // 取消按钮文字
  263. cancelText: {
  264. type: String,
  265. default: "取消",
  266. },
  267. // 确认按钮文字
  268. confirmText: {
  269. type: String,
  270. default: "确认",
  271. },
  272. // label对应的key名称
  273. labelName: {
  274. type: String,
  275. default: "label",
  276. },
  277. // value对应的key名称
  278. valueName: {
  279. type: String,
  280. default: "value",
  281. },
  282. // 是否允许点击遮罩层关闭
  283. maskCloseAble: {
  284. type: Boolean,
  285. default: true,
  286. },
  287. // 是否显示全选
  288. allShow: {
  289. type: Boolean,
  290. default: true,
  291. },
  292. // 模式
  293. mode: {
  294. type: String,
  295. default: "multiple",
  296. },
  297. // 默认选中值
  298. defaultSelected: {
  299. type: Array,
  300. default: function () {
  301. return [];
  302. },
  303. },
  304. // 数据源
  305. // data: {
  306. // type: Array,
  307. // required: true,
  308. // default: () => {
  309. // return [];
  310. // },
  311. // },
  312. },
  313. watch: {
  314. async value(newVal) {
  315. this.show = newVal;
  316. await this.$nextTick();
  317. this.activeClass = newVal;
  318. if (newVal) {
  319. this.selectedArrOld = JSON.parse(JSON.stringify(this.selectedArr));
  320. }
  321. },
  322. show(newVal) {
  323. this.$emit("input", newVal);
  324. this.$emit("change", newVal);
  325. },
  326. defaultSelected: {
  327. handler() {
  328. this.setItemActiveState();
  329. },
  330. deep: true,
  331. immediate: true,
  332. },
  333. },
  334. };
  335. </script>
  336. <style lang="scss">
  337. .searchbutton {
  338. button {
  339. height: 69rpx;
  340. line-height: 69rpx;
  341. }
  342. }
  343. .searchinput {
  344. .uni-easyinput__content-input {
  345. height: 70rpx;
  346. }
  347. }
  348. .btn-view {
  349. width: 100%;
  350. height: 100rpx;
  351. padding: 20rpx 10%;
  352. background-color: #ffffff;
  353. position: fixed;
  354. bottom: 0;
  355. left: 0;
  356. z-index: 1000;
  357. button {
  358. &::after {
  359. border: none;
  360. }
  361. }
  362. }
  363. </style>
  364. <style lang="scss" scoped>
  365. .selectlist {
  366. border-bottom: 2rpx solid #cccccc;
  367. display: flex;
  368. align-items: center;
  369. justify-content: space-between;
  370. }
  371. .chooseCompanyTags {
  372. .title_1 {
  373. font-size: 28rpx;
  374. font-weight: 500;
  375. color: #333333;
  376. text-align: right;
  377. height: 64rpx;
  378. line-height: 64rpx;
  379. }
  380. .title_2 {
  381. font-size: 26rpx;
  382. font-weight: 500;
  383. color: #777777;
  384. height: 64rpx;
  385. line-height: 64rpx;
  386. text-align: left;
  387. }
  388. .title_3 {
  389. margin-bottom: 12rpx;
  390. font-size: 26rpx;
  391. font-weight: 500;
  392. color: #777777;
  393. }
  394. .cell_1 {
  395. float: left;
  396. width: 10%;
  397. }
  398. .checkbox {
  399. border: 1px solid #cccccc;
  400. border-radius: 4rpx  4rpx  4rpx  4rpx;
  401. height: 30rpx;
  402. width: 30rpx;
  403. }
  404. .cell_2 {
  405. float: left;
  406. width: 90%;
  407. img {
  408. height: 60rpx;
  409. width: 60rpx;
  410. float: right;
  411. margin: 96rpx 0rpx 94rpx 0;
  412. }
  413. }
  414. .btn {
  415. width: 40%;
  416. height: 90rpx;
  417. background: #1d18bc;
  418. line-height: 90rpx;
  419. color: white;
  420. float: left;
  421. }
  422. .jichuback {
  423. width: calc(100% - 64rpx);
  424. margin: 32rpx;
  425. background: white;
  426. border-bottom: 2rpx solid #cccccc;
  427. }
  428. .tags_type {
  429. width: calc(100% - 64rpx);
  430. margin: 16rpx;
  431. }
  432. }
  433. .btn-view {
  434. width: 100%;
  435. height: 100rpx;
  436. padding: 20rpx 10%;
  437. background-color: #ffffff;
  438. position: fixed;
  439. bottom: 0;
  440. left: 0;
  441. z-index: 1000;
  442. }
  443. .btn {
  444. width: 40%;
  445. height: 90rpx;
  446. background: #1d18bc;
  447. line-height: 90rpx;
  448. color: white;
  449. float: left;
  450. }
  451. .select-container {
  452. width: 100vw;
  453. height: 100vh;
  454. position: fixed;
  455. left: 0;
  456. top: 0;
  457. z-index: 999;
  458. $paddingLR: 18rpx;
  459. .mask {
  460. width: 100%;
  461. height: 100%;
  462. background-color: $uni-bg-color-mask;
  463. opacity: 0;
  464. transition: opacity 0.3s;
  465. &.mask-show {
  466. opacity: 1;
  467. }
  468. }
  469. // 选择器内容区域
  470. .select-box {
  471. width: 100%;
  472. position: absolute;
  473. bottom: 0;
  474. left: 0;
  475. transform: translate3d(0px, 100%, 0px);
  476. background-color: $uni-bg-color;
  477. transition: all 0.3s;
  478. &.select-box-show {
  479. transform: translateZ(0);
  480. }
  481. .header {
  482. display: flex;
  483. box-sizing: border-box;
  484. width: 100%;
  485. justify-content: space-between;
  486. border-bottom: 1px solid $uni-border-color;
  487. line-height: 76rpx;
  488. font-size: 30rpx;
  489. padding: 0 $paddingLR;
  490. .cancel {
  491. color: $uni-text-color-grey;
  492. }
  493. .all {
  494. .all-active {
  495. &::after {
  496. display: inline-block;
  497. content: "✔";
  498. padding-left: 8rpx;
  499. }
  500. }
  501. }
  502. .confirm {
  503. color: $uni-color-primary;
  504. }
  505. }
  506. .body-warp {
  507. width: 100%;
  508. height: 30vh;
  509. box-sizing: border-box;
  510. padding: 20rpx $paddingLR;
  511. }
  512. .body {
  513. width: 96%;
  514. height: 70%;
  515. overflow-y: auto;
  516. padding: 2%;
  517. .empty-tips {
  518. margin-top: 25%;
  519. text-align: center;
  520. font-size: 26rpx;
  521. color: $uni-color-error;
  522. }
  523. }
  524. }
  525. }
  526. </style>