首页 > 编程技术 > js

vue中filter的应用场景详解

发布时间:2021-11-21 20:50 作者:qq_44221093

filter一般用于过滤某些值,比如我这个字段是空,可是我想在前端显示“–”,就可以使用到它了。

最近碰到一个需求就是要给某些字段可以设置权限去以其他形式显示,比如以“***”显示需要隐藏的金额。

1.获取金额权限

2.通过filter过滤满足条件的字段

3.返回隐藏的样式

看代码:

//其他的看,看我标注的就可以了
//scope.row 获取当前行
<template slot-scope="scope">
            <template v-if="item.formType == 'label'">
              <el-button
                v-if="item.link!=undefined"
                type="text" size="small" @click="handleColumnClick(item.link,scope.row)">
                //filter一般不用的过滤用|
                //showLabelValue就不写出来了
                //方法一个参数对应的filter是两个参数
                //第一个是前一列返回的值
                //第N-1个是你想传的值
                {{ scope.row | showLabelValue(item) | canViewAmount(canViewAmount,xtType,item) }}
              </el-button>
              <template v-else>
                {{ scope.row | showLabelValue(item) | canViewAmount(canViewAmount,xtType,item) }}
              </template>
            </template>
</template>
export default {
 filters: {
 //row就是scope.row返回的数据
 showLabelValue(row,item){
 return 'value'
 }
 //value值, canView权限, xtType哪个页面, item列表数据
 //如果showLabelValue返回的是value,对应的canViewAmount参数的value就是‘value'
    canViewAmount(value, canView, xtType, item) {
    //满足条件用“***”显示(只是显示),保存到数据库还是原列表的内容
      if (!canView && xtType == 'salesOrder') {
        if (item.field == 'priceNoTax' || item.field == 'amountNoTax' || item.field == 'price' || item.field == 'amount') {
          return '***'
        }
      }
      if (!canView && xtType == 'project') {
        if (item.field == 'amount' || item.field == 'amountNoTax') {
          return '***'
        }
      }
      return value
    }
  },

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注猪先飞的更多内容!

原文出处:https://blog.csdn.net/qq_44221093/article/details/121447658

标签:[!--infotagslink--]

您可能感兴趣的文章: