华域联盟 JavaScript vue中filter的应用场景详解

vue中filter的应用场景详解

文章目录[隐藏]

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
    }
  },

总结

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

本文由 华域联盟 原创撰写:华域联盟 » vue中filter的应用场景详解

转载请保留出处和原文链接:https://www.cnhackhy.com/147315.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们