有時候一個循環列表中展示的數據,需要做簡單的處理,比如顯示字符串第一個字,拆分字符串,保留兩位小數等。又不想再寫一次for循環,可以在wxml文檔中處理。
1、新建一個公共的方法文件 utils/common.wxs
var filter = {
getOneStr: function (value) {
return value.substring(0, 1);
},
splitTag: function (value) {
if (value == '' || !value){
return [];
} else {
return value.split('<');
}
}
}
module.exports = {
getOneStr: filter.getOneStr,
splitTag: filter.splitTag
}
2、在wxml頁面引用 pages/index/index
<wxs module="filter" src="../../utils/common.wxs"></wxs>
<view wx:for='{{list}}' wx:key=''>
<view class='name'>{{filter.getOneStr(item.name)}}</view>
</view>