selectList.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <template>
  2. <view class="select-view">
  3. <view class="select-mark-view" v-show="showList" @click="touchMarkEvent">
  4. </view>
  5. <view class="content-view" @click.stop="touchContent">
  6. <slot> </slot>
  7. <view class="filter-view">
  8. <view class="filter-header">
  9. <view
  10. class="filter-title"
  11. v-for="(item, listIndex) in dataList"
  12. :key="listIndex"
  13. >
  14. <view @click="showListEvent(listIndex)">
  15. <text>{{ getListCheckName(item) }}</text>
  16. <text
  17. v-if="listIndex == curIndex"
  18. class="iconfont iconsanjiaoshang"
  19. ></text>
  20. <text v-else class="iconfont iconsanjiao_xia"></text>
  21. </view>
  22. </view>
  23. </view>
  24. <view class="filter-list" v-if="showList">
  25. <view
  26. class="filter-item"
  27. v-for="(item, index) in dataList[curIndex].data"
  28. :key="index"
  29. @click="selectItemEvent(item)"
  30. >
  31. <text class="iconfont icondagou" v-if="item.checked"> </text>
  32. <text>
  33. {{ item.name }}
  34. </text>
  35. </view>
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. </template>
  41. <script>
  42. import { copyObject } from "@/utils/util";
  43. const SELECT_TYPES = {
  44. RADIO: "radio",
  45. MULTIPLE: "multiple"
  46. };
  47. export default {
  48. props: {
  49. list: {
  50. type: Array,
  51. default() {
  52. return [
  53. // {
  54. // title:'', //标题
  55. // key:'', //字段名
  56. // data:[
  57. // {
  58. // name:'',
  59. // value:''
  60. // }
  61. // ]
  62. // }
  63. ];
  64. }
  65. },
  66. type: {
  67. type: String,
  68. default: SELECT_TYPES.RADIO //multiple
  69. }
  70. },
  71. data() {
  72. return {
  73. curValue: "",
  74. curIndex: -1,
  75. showList: false,
  76. dataList: []
  77. };
  78. },
  79. onLoad() {},
  80. created() {
  81. this.initList();
  82. },
  83. computed: {
  84. curItemName() {
  85. let curlist = [];
  86. if (this.dataList && this.dataList[this.curIndex]) {
  87. curlist = this.dataList[this.curIndex].data;
  88. }
  89. let item = curlist.find(ele => {
  90. return ele.value == this.curValue;
  91. });
  92. if (item) {
  93. return item.name;
  94. }
  95. return false;
  96. }
  97. },
  98. methods: {
  99. initList() {
  100. this.dataList = [];
  101. for (const item of this.list) {
  102. let ele = copyObject(item);
  103. ele.data.forEach(element => {
  104. if (element.value == ele.default) {
  105. element.checked = true;
  106. } else {
  107. element.checked = false;
  108. }
  109. });
  110. this.dataList.push(ele);
  111. }
  112. },
  113. getListCheckName(info) {
  114. let name = info.title;
  115. for (const item of info.data) {
  116. if (item.checked) {
  117. name = item.name;
  118. break;
  119. }
  120. }
  121. return name;
  122. },
  123. touchContent() {},
  124. touchMarkEvent() {
  125. this.showList = false;
  126. this.curIndex = -1;
  127. },
  128. selectItemEvent(item) {
  129. if (SELECT_TYPES.RADIO == this.type) {
  130. let list = this.dataList[this.curIndex].data;
  131. list.forEach(element => {
  132. element.checked = false;
  133. });
  134. console.log(444444, list);
  135. }
  136. this.curValue = item.value;
  137. item.checked = true;
  138. this.$nextTick(() => {
  139. this.searchEvent();
  140. });
  141. },
  142. showListEvent(index) {
  143. this.curIndex = index;
  144. this.showList = true;
  145. },
  146. searchEvent() {
  147. this.showList = false;
  148. let params = this.getParams();
  149. this.$emit("search", params);
  150. },
  151. getParams() {
  152. let params = {};
  153. for (const item of this.dataList) {
  154. for (const listItem of item.data) {
  155. params[item.key] = "";
  156. if (listItem.checked) {
  157. params[item.key] = listItem.value;
  158. break;
  159. }
  160. }
  161. }
  162. return params;
  163. }
  164. },
  165. watch: {
  166. list(val, old) {
  167. if (val && val instanceof Array) {
  168. this.initList();
  169. }
  170. }
  171. }
  172. };
  173. </script>
  174. <style lang="scss" scoped>
  175. .select-view {
  176. display: fixed;
  177. width: 100%;
  178. .select-mark-view {
  179. /* #ifndef H5 */
  180. height: 100vh;
  181. /* #endif */
  182. /* #ifdef H5 */
  183. height: 100%;
  184. /* #endif */
  185. position: absolute;
  186. width: 100%;
  187. background-color: rgba(0, 0, 0, 0.5);
  188. }
  189. .content-view {
  190. position: relative;
  191. z-index: 1;
  192. .filter-view {
  193. .filter-header {
  194. display: flex;
  195. background-color: #fff;
  196. .filter-title {
  197. position: relative;
  198. flex: 1;
  199. height: 80upx;
  200. line-height: 80upx;
  201. text-align: center;
  202. &:not(:last-child):after {
  203. position: absolute;
  204. content: "";
  205. width: 1px;
  206. height: 50%;
  207. background-color: #dddddd;
  208. top: 50%;
  209. right: 0;
  210. transform: translateY(-50%);
  211. }
  212. .iconfont {
  213. font-size: 30upx;
  214. }
  215. }
  216. }
  217. .filter-list {
  218. position: absolute;
  219. top: 100%;
  220. left: 0;
  221. width: 100%;
  222. padding: 20upx 30upx;
  223. max-height: 400upx;
  224. background-color: #f8f9fb;
  225. border-radius: 0 0 25upx 25upx;
  226. .filter-item {
  227. width: 100%;
  228. height: 70upx;
  229. line-height: 70upx;
  230. font-size: 26upx;
  231. .iconfont {
  232. font-size: 36upx;
  233. color: $mainColor;
  234. }
  235. text {
  236. margin: 0 10upx;
  237. }
  238. }
  239. }
  240. }
  241. }
  242. }
  243. </style>