大佬们,请问wx:for中套if的如何显示序号?

小程序 文章 2022-03-08 18:40 0 全屏看文

大佬们,请问wx:for中套if的如何显示序号?Guys, how to display the serial number of if in wx:for?

大佬们,请问wx:for中套if的如何显示序号?

需求是一组数据,类似如下:

[{name:张1,sex:女},{name:张2,sex:男},{name:张3,sex:女},{name:张4,sex:男},{name:张5,sex:女},]


要求渲染后可以有性别过滤(全部、男、女三种)并输出序号,类似:

1、张12、张33、张5


我用for循环嵌套if过滤然后用{{index+1}}充当序号是不行的(会不连续,出现1、3、5这种情况),请问有没有好的实现方法?

Requirements are a set of data, similar to the following: [{Name: Zhang 1, sex: female}, {Name: Zhang 2, sex: male}, {Name: Zhang 3, sex: female}, {Name: Zhang 4, sex: male}, {Name: Zhang 5, sex: female},] after rendering, gender filtering (all, male and female) and output sequence numbers, similar to: 1, Zhang 12, Zhang 33 Zhang 5 I can't use for loop nested if filtering and then use {{index + 1}} as the serial number (it will be discontinuous, such as 1, 3 and 5). Is there a good implementation method?

回答:

SUN:

这个应该是后台给你处理吧,如果前端搞,可以查分两个数组,分别为

man:[{name:张2,sex:男},{name:张4,sex:男},]woman:[{name:张1,sex:女},{name:张3,sex:女},]然后根据条件绑定页面变量的获取数组的范围:key = "all"setData({list:list})key = "woman"setData({list:woman})key = "man"setData({list:man})然后继续下标当序号,不成熟想法,仅供参考,个人还是建议后台去处理数据问题,毕竟前端主要任务还是交互与渲染



brave:
// page.jsPage({  data: {    type: 0,    list:[      {name:"张1",sex:"女"},      {name:"张2",sex:"男"},      {name:"张3",sex:"女"},      {name:"张4",sex:"男"},      {name:"张5",sex:"女"}    ]  }})// page.wxml<view wx:for="{{filter.getData(type, list)}}" wx:key="name">{{index}}、{{item.name}}</view><wxs module="filter">  function getData(type, list) {    if (type == 0) {      return list    } else {      return list.filter(function(item){        return type == 1 ? item.sex === "男" : item.sex === "女"       })    }  }  module.exports = {    getData: getData  }</wxs>
15:

是的,小程序里面同一标签for的优先级比if高。还是建议先在js处理好数据再循环吧

拾忆:

先用JS处理下,生成一个新的数组,然后前端显示。

-EOF-