filter-view.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. <template>
  2. <view>
  3. <view style="padding: 0px 0px;">
  4. <view class="filter-content" v-for="(item, index) in menuList" :key="index" v-if="menuIndex == index">
  5. <view v-if="item.isSort">
  6. <view class="filter-content-list">
  7. <view v-for="(detailItem,idx) in selectDetailList" :key="idx"
  8. :class="detailItem.isSelected?'filter-content-list-item-active':'filter-content-list-item-default'"
  9. :style="{'color': detailItem.isSelected?themeColor:'#666666'}"
  10. @tap="sortTap(idx,selectDetailList,item.key)">
  11. <text>{{detailItem.title}}</text>
  12. </view>
  13. </view>
  14. </view>
  15. <view v-else>
  16. <view class="filter-content-title" v-if="item.detailTitle && item.detailTitle.length">
  17. <text>{{item.detailTitle}}</text>
  18. </view>
  19. <scroll-view scroll-y style="height: 300rpx;" class="filter-content-detail">
  20. <!-- <text v-for="(detailItem,idx) in selectDetailList" :key="idx" class='filter-content-detail-item-default' :style="{'background-color':detailItem.isSelected?themeColor:'#FFFFFF','color':detailItem.isSelected?'#FFFFFF':'#666666'}"
  21. @tap="itemTap(idx,selectDetailList,item.isMutiple,item.key)">
  22. {{detailItem.title}}
  23. </text> -->
  24. <view v-for="(detailItem,idx) in selectDetailList" :key="idx">
  25. <view>
  26. {{detailItem.title}}
  27. </view>
  28. <view v-for="(childrenItem,id) in detailItem.children"
  29. class='filter-content-detail-item-default'
  30. :style="{'background-color':childrenItem.isSelected?themeColor:'#FFFFFF','color':detailItem.children.isSelected?'#FFFFFF':'#666666'}"
  31. @tap="itemTap(id,detailItem.children,item.isMutiple,item.key)">
  32. <text>{{childrenItem.value}}</text>
  33. </view>
  34. </view>
  35. </scroll-view>
  36. <view class="filter-content-footer">
  37. <view class="filter-content-footer-item" style="color: #777777; background-color: #FFFFFF;"
  38. @tap="resetClick(selectDetailList,item.key)">
  39. <text>重置</text>
  40. </view>
  41. <view class="filter-content-footer-item"
  42. :style="{'color': '#FFFFFF', 'background-color': themeColor}" @tap="sureClick">
  43. <text>确定</text>
  44. </view>
  45. </view>
  46. </view>
  47. </view>
  48. </view>
  49. </view>
  50. </template>
  51. <script>
  52. export default {
  53. data() {
  54. return {
  55. selectArr: [],
  56. result: {},
  57. menuIndex: 0,
  58. selectDetailList: [],
  59. independenceObj: {},
  60. selectedKey: '',
  61. cacheSelectedObj: {},
  62. defaultSelectedTitleObj: {}
  63. };
  64. },
  65. props: {
  66. themeColor: {
  67. type: String,
  68. default () {
  69. return '#D1372C'
  70. }
  71. },
  72. menuList: {
  73. type: Array,
  74. default () {
  75. return []
  76. }
  77. },
  78. independence: {
  79. type: Boolean,
  80. default: false
  81. }
  82. },
  83. computed: {
  84. selectedTitleObj() {
  85. let obj = {}
  86. for (let i = 0; i < this.menuList.length; i++) {
  87. let item = this.menuList[i];
  88. obj[item.key] = item.title;
  89. }
  90. return obj;
  91. },
  92. defaultSelectedObj() { // 保存初始状态
  93. return this.getSelectedObj()
  94. },
  95. selectedObj: {
  96. get() {
  97. return this.getSelectedObj()
  98. },
  99. set(newObj) {
  100. return newObj;
  101. }
  102. }
  103. },
  104. mounted() {
  105. this.getSelectedObj();
  106. this.sureClick();
  107. },
  108. methods: {
  109. getSelectedObj() {
  110. let obj = {}
  111. for (let i = 0; i < this.menuList.length; i++) {
  112. let item = this.menuList[i];
  113. if (!this.independence && item.defaultSelectedIndex != null && item.defaultSelectedIndex.toString()
  114. .length > 0) { // 处理并列菜单默认值
  115. if (item.isMutiple) {
  116. obj[item.key] = [];
  117. item.detailList[0].isSelected = false;
  118. if (!Array.isArray(item.defaultSelectedIndex)) { // 如果默认值不是数组
  119. item.defaultSelectedIndex = [item.defaultSelectedIndex];
  120. }
  121. for (let j = 0; j < item.defaultSelectedIndex.length; j++) { // 将默认选中的值放入selectedObj
  122. item.detailList[item.defaultSelectedIndex[j]].isSelected = true;
  123. obj[item.key].push(item.detailList[item.defaultSelectedIndex[j]].value)
  124. }
  125. } else {
  126. obj[item.key] = item.detailList[item.defaultSelectedIndex].value;
  127. this.selectedTitleObj[item.key] = item.detailList[item.defaultSelectedIndex].title;
  128. this.defaultSelectedTitleObj[item.key] = item.detailList[item.defaultSelectedIndex].title;
  129. item.detailList[0].isSelected = false;
  130. item.detailList[item.defaultSelectedIndex].isSelected = true;
  131. }
  132. } else {
  133. if (item.isMutiple) {
  134. obj[item.key] = [];
  135. } else {
  136. obj[item.key] = item.detailList[0].value;
  137. }
  138. }
  139. }
  140. this.result = obj;
  141. console.log(obj, "77777")
  142. return obj;
  143. },
  144. // 重置所有选项,包括默认选项,并更新result
  145. resetAllSelect(callback) {
  146. let titles = [];
  147. for (let i = 0; i < this.menuList.length; i++) {
  148. this.resetSelected(this.menuList[i].detailList, this.menuList[i].key);
  149. titles[this.menuList[i].key] = this.menuList[i].title;
  150. }
  151. let obj = {
  152. 'result': this.result,
  153. 'titles': titles,
  154. 'isReset': true
  155. }
  156. console.log(this.result)
  157. this.$emit("confirm", obj);
  158. callback(this.result);
  159. },
  160. // 重置选项为设置的默认值,并更新result
  161. resetSelectToDefault(callback) {
  162. for (let i = 0; i < this.menuList.length; i++) {
  163. this.selectDetailList = this.menuList[i].detailList;
  164. if (this.menuList[i].defaultSelectedIndex) {
  165. if (Array.isArray(this.menuList[i].defaultSelectedIndex)) { // 把所有默认的为false的点为true
  166. for (let j = 0; j < this.menuList[i].defaultSelectedIndex.length; j++) {
  167. if (this.selectDetailList[this.menuList[i].defaultSelectedIndex[j]].isSelected == false) {
  168. this.itemTap(this.menuList[i].defaultSelectedIndex[j], this.selectDetailList, this
  169. .menuList[i].isMutiple, this
  170. .menuList[i].key)
  171. }
  172. }
  173. } else {
  174. this.itemTap(this.menuList[i].defaultSelectedIndex, this.selectDetailList, this.menuList[i]
  175. .isMutiple, this.menuList[
  176. i].key)
  177. }
  178. // 获取非默认项的下标
  179. let unDefaultSelectedIndexArr = this.getUnDefaultSelectedIndex(this.menuList[i])
  180. // 把所有不是默认的为true的点为false
  181. for (let j = 0; j < unDefaultSelectedIndexArr.length; j++) {
  182. if (this.selectDetailList[unDefaultSelectedIndexArr[j]].isSelected == true) {
  183. this.itemTap(unDefaultSelectedIndexArr[j], this.selectDetailList, this.menuList[i]
  184. .isMutiple, this
  185. .menuList[i].key)
  186. }
  187. }
  188. }
  189. }
  190. this.selectedObj = this.defaultSelectedObj;
  191. this.result = this.defaultSelectedObj;
  192. console.log(this.result)
  193. let obj = {
  194. 'result': this.result,
  195. 'titles': this.defaultSelectedTitleObj,
  196. 'isReset': true
  197. }
  198. this.$emit("confirm", obj);
  199. callback(this.result)
  200. },
  201. getUnDefaultSelectedIndex(menuListItem) { // 获取非默认项
  202. let tempDefault = menuListItem.defaultSelectedIndex;
  203. if (!Array.isArray(tempDefault)) {
  204. tempDefault = [tempDefault];
  205. }
  206. // 获取所有项的下标 组成新的数组
  207. let all = [];
  208. for (let i = 0; i < menuListItem.detailList.length; i++) {
  209. all.push(i)
  210. }
  211. // 将默认选中的数组与所有项的数组的不同值合并为一个新数组
  212. var unDefaultSelectedIndex = tempDefault.filter(function(v) {
  213. return !(all.indexOf(v) > -1)
  214. }).concat(all.filter(function(v) {
  215. return !(tempDefault.indexOf(v) > -1)
  216. }));
  217. return unDefaultSelectedIndex;
  218. },
  219. resetMenuList(val) {
  220. this.menuList = val;
  221. this.$emit('update:menuList', val)
  222. },
  223. menuTabClick(index) {
  224. this.menuIndex = index;
  225. this.selectDetailList = this.menuList[index].detailList;
  226. this.selectedKey = this.menuList[index].key;
  227. // 如果是独立菜单
  228. if (this.independence && !this.menuList[index].isSort) {
  229. if (JSON.stringify(this.independenceObj) == '{}') {
  230. this.initIndependenceObj(index);
  231. } else {
  232. for (let key in this.independenceObj) {
  233. if (key != this.selectedKey) {
  234. this.initIndependenceObj(index);
  235. this.resetSelected(this.menuList[index].detailList, this.selectedKey);
  236. }
  237. }
  238. }
  239. }
  240. if (this.independence && this.menuList[index].isSort) {
  241. this.independenceObj = {};
  242. }
  243. if (this.independence) {
  244. let idx = this.menuList[index].defaultSelectedIndex;
  245. if (idx != null && idx.toString().length > 0) { // 处理独立菜单默认值
  246. if (this.menuList[index].isMutiple) {
  247. for (let i = 0; i < idx.length; i++) {
  248. if (this.menuList[index].detailList[idx[i]].isSelected == false) {
  249. this.itemTap(idx[i], this.menuList[index].detailList, true, this.selectedKey);
  250. }
  251. }
  252. } else {
  253. if (this.menuList[index].detailList[idx].isSelected == false) {
  254. this.itemTap(idx, this.menuList[index].detailList, false, this.selectedKey);
  255. }
  256. }
  257. }
  258. }
  259. // #ifdef H5
  260. this.selectedObj = this.selectedObj;
  261. this.$forceUpdate();
  262. // #endif
  263. },
  264. initIndependenceObj(index) {
  265. this.independenceObj = {};
  266. if (this.menuList[index].isMutiple) {
  267. this.independenceObj[this.selectedKey] = [];
  268. } else {
  269. this.independenceObj[this.selectedKey] = '';
  270. }
  271. },
  272. itemTap(index, list, isMutiple, key) {
  273. if (isMutiple == true) {
  274. list[index].isSelected = !list[index].isSelected;
  275. // this.resetSelected(list, key)
  276. // if (!this.independence) {
  277. // this.selectedTitleObj[key] = list[index].title;
  278. // }
  279. // list[0].isSelected = false
  280. if (list[index].isSelected) {
  281. console.log("777")
  282. if (this.independence) {
  283. this.independenceObj[this.selectedKey].push(list[index].id);
  284. } else {
  285. this.selectedObj[key].push(list[index].id);
  286. }
  287. } else {
  288. list[index].isSelected = false;
  289. if (this.independence) {
  290. var idx = this.independenceObj[this.selectedKey].indexOf(list[index].value);
  291. this.independenceObj[this.selectedKey].splice(idx, 1);
  292. } else {
  293. var idx = this.selectedObj[key].indexOf(list[index].value);
  294. this.selectedObj[key].splice(idx, 1);
  295. }
  296. }
  297. if (this.independence) {
  298. this.result = this.independenceObj;
  299. } else {
  300. this.result = this.selectedObj;
  301. }
  302. } else {
  303. if (index == 0) {
  304. this.resetSelected(list, key)
  305. if (!this.independence) {
  306. this.selectedTitleObj[key] = list[index].title;
  307. }
  308. } else {
  309. list[0].isSelected = false
  310. if (this.independence) {
  311. this.independenceObj[this.selectedKey] = list[index].value;
  312. this.result = this.independenceObj;
  313. } else {
  314. this.selectedObj[key] = list[index].value;
  315. this.result = this.selectedObj;
  316. this.selectedTitleObj[key] = list[index].title;
  317. }
  318. for (let i = 0; i < list.length; i++) {
  319. if (index == i) {
  320. list[i].isSelected = true
  321. } else {
  322. list[i].isSelected = false
  323. }
  324. }
  325. }
  326. }
  327. // #ifdef H5
  328. this.$forceUpdate();
  329. // #endif
  330. },
  331. resetSelected(list, key) {
  332. console.log(list, "ressss")
  333. if (typeof this.result[key] == 'object') {
  334. this.result[key] = [];
  335. this.selectedTitleObj[key] = list[0].title;
  336. } else {
  337. this.result[key] = '';
  338. this.selectedTitleObj[key] = list[0].title;
  339. }
  340. // for (let i = 0; i < list.length; i++) {
  341. // list[i].isSelected = false;
  342. // }
  343. list.forEach(item => {
  344. item.children.forEach(item1 => {
  345. item1.isSelected = false;
  346. })
  347. })
  348. // #ifdef H5
  349. this.$forceUpdate();
  350. // #endif
  351. },
  352. sortTap(index, list, key) {
  353. if (this.independence) {
  354. this.independenceObj[this.selectedKey] = list[index].value;
  355. this.result = this.independenceObj;
  356. } else {
  357. this.selectedObj[key] = list[index].value;
  358. this.result = this.selectedObj;
  359. this.selectedTitleObj[key] = list[index].title;
  360. }
  361. for (let i = 0; i < list.length; i++) {
  362. if (index == i) {
  363. list[i].isSelected = true;
  364. } else {
  365. list[i].isSelected = false;
  366. }
  367. }
  368. let obj = {
  369. 'result': this.result,
  370. 'titles': this.selectedTitleObj,
  371. 'isReset': false
  372. }
  373. this.$emit("confirm", obj);
  374. },
  375. sureClick() {
  376. console.log("44444")
  377. // if(this.selectDetailList[0].isSelected){
  378. // this.result[0] =
  379. // }
  380. let obj = {
  381. 'result': this.result,
  382. 'titles': this.selectedTitleObj,
  383. 'isReset': false
  384. }
  385. console.log(this.result)
  386. this.$emit("confirm", obj);
  387. },
  388. resetClick(list, key) {
  389. this.resetSelected(list, key)
  390. }
  391. }
  392. }
  393. </script>
  394. <style>
  395. .filter-content {
  396. background-color: #F6F7F8;
  397. }
  398. .filter-content-title {
  399. border-bottom: #EEEEEE 1px solid;
  400. padding: 10px 15px;
  401. font-size: 13px;
  402. color: #999999;
  403. }
  404. .filter-content-detail {
  405. padding: 5px 15px;
  406. }
  407. .filter-content-detail-item-active {
  408. background-color: #D1372C;
  409. color: #FFFFFF;
  410. padding: 5px 15px;
  411. border-radius: 20px;
  412. margin-right: 10px;
  413. margin-top: 10px;
  414. display: inline-block;
  415. font-size: 14px;
  416. }
  417. .filter-content-detail-item-default {
  418. background-color: #FFFFFF;
  419. color: #666666;
  420. padding: 5px 15px;
  421. border-radius: 20px;
  422. margin-right: 10px;
  423. margin-top: 10px;
  424. display: inline-block;
  425. font-size: 14px;
  426. }
  427. .filter-content-footer {
  428. display: flex;
  429. justify-content: space-between;
  430. width: 100%;
  431. height: 45px;
  432. margin-top: 10px;
  433. }
  434. .filter-content-footer-item {
  435. width: 50%;
  436. display: flex;
  437. justify-content: center;
  438. align-items: center;
  439. font-size: 16px;
  440. }
  441. .filter-content-list {
  442. text-align: center;
  443. padding: 5px 15px;
  444. }
  445. .filter-content-list-item-default {
  446. color: #666666;
  447. width: 100%;
  448. padding: 10px 0px;
  449. }
  450. .filter-content-list-item-default text {
  451. width: 90%;
  452. font-size: 14px;
  453. display: inline-block;
  454. }
  455. .filter-content-list-item-active {
  456. color: #D1372C;
  457. width: 100%;
  458. padding: 10px 0px;
  459. }
  460. .filter-content-list-item-active text {
  461. font-size: 14px;
  462. display: inline-block;
  463. }
  464. .filter-content-list-item-active:after {
  465. content: '✓';
  466. }
  467. </style>