微信小程序实现WXML中直接处理数据,并且保留两位小数

1.新开一个页面,叫addmul.wxs,写入下面的函数等内容

var filters = {
  //百分比数据
    Percentage: function(number1,number2){
      return (Math.round(number1/(number2)*10000)/100.00+"%")
  }
}
module.exports = {
  Percentage: filters.Percentage
}

2.在需要处理数据的地方引入
在这里插入图片描述

3.如果有循环的数据直接将数据带入

<view class="tr" wx:for="{{YWHHXRS}}" wx:key="index">
                    <view class="td"  style="width:150rpx">{{item.XM}}</view>
                    <view class="td">{{item.XB == 1? '男': '女'}}</view>
                    <view class="td">{{item.AGE}}</view>
                    <view class="td">{{item.ZZMM}}</view>
                    <view class="td">{{item.LZFH}}</view>
                    <view class="td">{{item.TPZS}}</view>
                    <view class="td">{{filters.Percentage(item.TPZS,item.TPRS)}}</view>
                    <view class="td">{{item.TPMJ}}</view>
                    <view class="td">{{filters.Percentage(item.TPMJ,item.TPMJ)}}</view>
                </view>

其中filters.Percentage(item.TPZS,data.TPRS)具体是指:
执行函数 Percentage(69,99)
结果会在页面中返回69.7%

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: You can execute a method in an array in a WeChat mini-program's WXML by using the `wx:for` directive to loop through the array and bind the method to an event, such as a button click. Here is an example: ``` <!-- in the data section --> data: { myArray: [ { name: 'item1', method: function() { console.log('method 1 called'); } }, { name: 'item2', method: function() { console.log('method 2 called'); } } ] } <!-- in the template section --> <template wx:for="{{myArray}}" wx:key="index"> <view>{{item.name}}</view> <button bindtap="{{item.method}}">Call Method</button> </template> ``` In this example, the `wx:for` directive is used to loop through the `myArray` array and bind each method to the button's `bindtap` event. When the button is tapped, the corresponding method will be executed. ### 回答2: 在微信小程序的wxml无法直接执行数组的方法,因为wxml是可视化界面的展示层,主要负责数据的渲染和展示,并不具备直接执行方法的能力。不过,我们可以通过在wxml绑定事件,然后在对应的事件处理函数调用数组的方法来达到间接执行的效果。 具体操作如下: 1. 在wxml使用`{{}}`的插值表达式来绑定需要执行的方法,例如`{{methodName}}`。 2. 将需要执行的方法名赋值给`methodName`,可以通过在页面的js文件定义一个与数组方法同名的函数,并将该函数名赋值给`methodName`。 3. 在wxml绑定事件,例如`<view bind:tap="handleTap"></view>`,这里通过`bind:tap`事件绑定一个名为`handleTap`的事件处理函数。 4. 在相应的事件处理函数(`handleTap`)通过调用`methodName`来间接执行数组的方法。 例如,假设有一个数组`methods`包含了多个方法名: ```javascript Page({ data: { methodName: '', methods: ['methodA', 'methodB', 'methodC'], }, methodA() { console.log('执行了方法A'); }, methodB() { console.log('执行了方法B'); }, methodC() { console.log('执行了方法C'); }, handleTap(e) { const { index } = e.currentTarget.dataset; const methodName = this.data.methods[index]; // 通过索引获取对应的方法名 this.setData({ methodName }); // 设置methodName为对应方法名 }, }); ``` 然后在wxml,通过`wx:for`循环遍历数组,并绑定点击事件: ```html <view wx:for="{{methods}}" wx:for-index="index" bind:tap="handleTap" data-index="{{index}}"> 点击执行 {{item}} 方法 </view> ``` 当用户点击对应的视图时,会触发`handleTap`事件处理函数,然后通过`methodName`间接调用相应的数组的方法。这样就实现了在微信小程序的wxml通过数组间接执行方法的效果。 ### 回答3: 在微信小程序的wxml,我们可以通过以下步骤来执行数组的方法: 1. 首先,在wxml使用{{}}的双大括号语法引用数组。例如,如果数组名称为array,我们可以在wxml使用{{array}}来引用该数组。 2. 接着,我们可以使用{{}}内嵌JavaScript表达式的形式,执行数组的方法。例如,如果数组有一个方法为myMethod,我们可以通过{{myMethod()}}来执行该方法。 3. 如果数组的方法需要传递参数,我们可以在{{}}内的括号传递参数。例如,如果myMethod需要接收一个参数x,则可以使用{{myMethod(x)}}的形式来执行该方法并传递参数x。 需要注意的是,wxml的方法执行是在小程序的逻辑层进行的,而不是在视图层执行的。因此,如果数组的方法需要实时更新视图层的数据,我们需要在方法使用setData来更新数据。 总结起来,要在微信小程序的wxml执行数组的方法,我们可以使用{{}}双大括号引用数组,并在{{}}内的表达式调用需要执行的方法。如果有参数,可以在括号传递参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值