fa-orderby-select.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <template>
  2. <view class="">
  3. <view class="orderby-select" @click="show = true"><image src="../../static/image/select.png" mode="aspectFit"></image></view>
  4. <u-popup v-model="show" mode="right" width="88%" :customStyle="customStyle">
  5. <view class="fa-popup">
  6. <scroll-view class="fa-scroll-view" scroll-y="true">
  7. <view class="fa-select-list u-m-b-15">
  8. <view class="u-border-bottom u-p-20 u-text-weight">排序</view>
  9. <view class="u-flex u-flex-wrap list u-p-20">
  10. <view class="item u-m-b-15" :style="[orderStyle(item)]" @click="orderBy(item, index)" v-for="(item, index) in orderList" :key="index">
  11. <text class="u-m-r-5">{{ item.title }}</text>
  12. <u-icon :name="orderIcon(item, index)"></u-icon>
  13. </view>
  14. </view>
  15. </view>
  16. <view class="">
  17. <view class="u-flex u-bg-white u-row-between u-m-b-15">
  18. <view class="u-p-20 u-text-weight">筛选</view>
  19. <view class="" v-if="multiple">
  20. <u-checkbox v-model="checked" name="1" :active-color="theme.bgColor"><text>多选模式</text></u-checkbox>
  21. </view>
  22. </view>
  23. <view class="fa-select-list u-m-b-15 u-border-top" v-for="(item, index) in filterList" :key="index">
  24. <view class="u-p-20 u-border-bottom u-text-weight">{{ item.title }}</view>
  25. <view class="u-flex list u-flex-wrap u-p-20">
  26. <view
  27. class="item u-m-b-15"
  28. :style="[itemStyle(index, key)]"
  29. v-for="(res, key) in item.content"
  30. :key="key"
  31. @click="select(index, key)"
  32. >
  33. {{ res.title }}
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. </scroll-view>
  39. <view class="footer u-flex u-border-top">
  40. <view class="u-flex-6" @click="close">取消</view>
  41. <view class="u-flex-6" :style="{ backgroundColor: theme.bgColor, color: theme.color }" @click="toGo">确定</view>
  42. </view>
  43. </view>
  44. </u-popup>
  45. </view>
  46. </template>
  47. <script>
  48. // 获取系统状态栏的高度
  49. let systemInfo = uni.getSystemInfoSync();
  50. export default {
  51. name: 'fa-orderby-select',
  52. props: {
  53. multiple:{
  54. type:Boolean,
  55. default:false
  56. },
  57. separator: {
  58. type: String,
  59. default: ';'
  60. },
  61. orderList: {
  62. type: [Array],
  63. default() {
  64. return [];
  65. }
  66. },
  67. filterList: {
  68. type: [Array],
  69. default() {
  70. return [];
  71. }
  72. }
  73. },
  74. computed: {
  75. // 转换字符数值为真正的数值
  76. navbarHeight() {
  77. // #ifdef H5
  78. return 44;
  79. // #endif
  80. // #ifdef APP-PLUS
  81. return 44+systemInfo.statusBarHeight;
  82. // #endif
  83. // #ifdef MP
  84. // 小程序特别处理,让导航栏高度 = 胶囊高度 + 两倍胶囊顶部与状态栏底部的距离之差(相当于同时获得了导航栏底部与胶囊底部的距离)
  85. let height = systemInfo.platform == 'ios' ? 44 : 48;
  86. return height+systemInfo.statusBarHeight;
  87. // #endif
  88. },
  89. customStyle(){
  90. return {
  91. height:'calc(100% - '+this.navbarHeight+'px)',
  92. top:this.navbarHeight+'px'
  93. }
  94. },
  95. orderStyle() {
  96. return item => {
  97. let style = {
  98. backgroundColor: this.theme.bgColor,
  99. color: this.theme.color
  100. };
  101. if (item.name == this.orderby) {
  102. return style;
  103. }
  104. return {};
  105. };
  106. },
  107. itemStyle() {
  108. return (index, key) => {
  109. let style = {
  110. backgroundColor: this.theme.bgColor,
  111. color: this.theme.color
  112. };
  113. if (!this.checked) {
  114. if (this.singleList[index] == key) {
  115. return style;
  116. }
  117. } else {
  118. let arr = this.manyList[index];
  119. if (arr && arr.indexOf(key) != -1) {
  120. return style;
  121. }
  122. }
  123. return {};
  124. };
  125. },
  126. orderIcon() {
  127. return (item, index) => {
  128. if (item.name == this.orderby || this.orderway[index] != undefined) {
  129. if (this.orderway[index] == 'asc') {
  130. return 'arrow-upward';
  131. } else {
  132. return 'arrow-downward';
  133. }
  134. }
  135. let orderway = this.$util.getQueryString('orderway', item.url);
  136. if (orderway == 'asc') {
  137. return 'arrow-upward';
  138. }
  139. return 'arrow-downward';
  140. };
  141. }
  142. },
  143. mounted() {
  144. this.init();
  145. },
  146. data() {
  147. return {
  148. show: false,
  149. checked: false,
  150. orderIndex: 0,
  151. singleList: [],
  152. manyList: [],
  153. orderway: [],
  154. orderby: ''
  155. };
  156. },
  157. methods: {
  158. init() {
  159. //默认排序
  160. this.orderList.forEach((item, index) => {
  161. if (item.active) {
  162. this.orderby = item.name;
  163. this.orderway[index] = this.$util.getQueryString('orderway', item.url);
  164. }
  165. });
  166. //默认选中
  167. let len = (this.filterList && this.filterList.length) || 0;
  168. this.singleList = [];
  169. this.manyList = [];
  170. for (let i = 0; i < len; i++) {
  171. this.singleList.push(0);
  172. this.manyList.push([0]);
  173. }
  174. },
  175. close() {
  176. this.show = false;
  177. this.init();
  178. },
  179. orderBy(item, index) {
  180. this.orderIndex = index;
  181. //当前已选中
  182. if (item.name == this.orderby) {
  183. if (this.orderway[index] == 'asc') {
  184. this.$set(this.orderway, index, 'desc');
  185. } else {
  186. this.$set(this.orderway, index, 'asc');
  187. }
  188. return;
  189. }
  190. if (this.orderway[index] == undefined) {
  191. this.$set(this.orderway, index, this.$util.getQueryString('orderway', item.url));
  192. }
  193. this.orderby = this.$util.getQueryString('orderby', item.url);
  194. },
  195. select(index, key) {
  196. if (!this.checked) {
  197. this.$set(this.singleList, index, key);
  198. } else {
  199. //全部,就只有全部了
  200. if (key == 0) {
  201. this.$set(this.manyList, index, [key]);
  202. return;
  203. }
  204. let arr = this.manyList[index];
  205. if (!arr) {
  206. this.$set(this.manyList, index, [key]);
  207. } else {
  208. //全部存在,要移除
  209. let all = arr.indexOf(0);
  210. if (all != -1) {
  211. arr.splice(all, 1);
  212. }
  213. let index = arr.indexOf(key);
  214. if (index != -1) {
  215. //存在,移除
  216. arr.splice(index, 1);
  217. } else {
  218. arr.push(key);
  219. }
  220. }
  221. }
  222. },
  223. //取筛选条件
  224. toGo() {
  225. let query = {};
  226. //单选
  227. if (!this.checked) {
  228. this.singleList.forEach((item, index) => {
  229. if (item && this.filterList[index]) {
  230. let row = this.filterList[index].content[item];
  231. query[this.filterList[index].name] = row.value;
  232. }
  233. });
  234. } else {
  235. //多选
  236. query.multiple = 1;
  237. this.manyList.forEach((item, index) => {
  238. let arr = [];
  239. item.forEach(res => {
  240. if (res && this.filterList[index]) {
  241. let row = this.filterList[index].content[res];
  242. arr.push(row.value);
  243. }
  244. });
  245. if (arr.length) {
  246. query[this.filterList[index].name] = !this.separator ? arr : arr.join(this.separator);
  247. }
  248. });
  249. }
  250. //赋值排序
  251. query.orderby = this.orderby;
  252. query.orderway = this.orderway[this.orderIndex];
  253. this.show = false;
  254. this.$emit('change', query);
  255. }
  256. }
  257. };
  258. </script>
  259. <style lang="scss" scoped>
  260. .orderby-select {
  261. image {
  262. width: 40rpx;
  263. height: 40rpx;
  264. }
  265. }
  266. .fa-select-list {
  267. background-color: #ffffff;
  268. .title {
  269. }
  270. .list {
  271. .item {
  272. background-color: #eeeeee;
  273. padding: 10rpx 30rpx;
  274. border-radius: 100rpx;
  275. margin-right: 15rpx;
  276. color: $u-tips-color;
  277. }
  278. }
  279. }
  280. .fa-popup {
  281. height: 100%;
  282. background-color: #f7f7f7;
  283. .fa-scroll-view {
  284. height: calc(100% - 100rpx);
  285. }
  286. .footer {
  287. background-color: #ffffff;
  288. height: 100rpx;
  289. line-height: 100rpx;
  290. display: flex;
  291. width: 100%;
  292. text-align: center;
  293. }
  294. }
  295. </style>