首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Vuejs2-如何从方法调用过滤器函数

Vuejs2-如何从方法调用过滤器函数
EN

Stack Overflow用户
提问于 2018-06-04 20:34:13
回答 1查看 2.1K关注 0票数 0

我使用"moneyFormat“过滤器来格式化货币值。它正在格式化已经定义好的值。我想格式化动态值。因此,我通过一个名为"displayValue“的方法调用了过滤器函数,但我得到了错误

并且给定的输入字段也不更新。

下面是我的代码:

代码语言:javascript
复制
<template>
    <b-card>
        <div class="panel-body" id="app">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th style="width: 20px;">No.</th>
                        <th style="width: 330px;">Description</th>
                        <th style="width: 130px;" class="text-right">Charges</th>
                        <th style="width: 130px;">Total</th>
                        <th style="width: 130px;"></th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(row, index) in rows" :key="row.qty">
                        <td>
                            {{ index +1 }}
                        </td>
                        <td>

                        <select name="" id="" class="form-control" v-model="row.billChgDesc">
                            <option v-for="option in billChgDescOpt" v-bind:value="option.value" 
                                :key="option.value"> {{ option.text }} 
                            </option>
                        </select>
                        </td>

                                <td> 
                                  <input @input="displayValue" class="form-control text-right" type="text" v-model="row.charges"  data-type="currency" v-validate="'required'" :name="'charges' + index">
                                     <span v-show="vErrors.has('charges' + index)" class="is-danger">{{ vErrors.first('charges' + index) }}</span>
                                <td>
                                    <input class="form-control text-right" :value="row.qty * row.charges | moneyFormat" number readonly />
                                    <input type="hidden" :value="row.qty * row.charges * row.tax / 100"  number/>
                                </td>

                                <td>
                                    <button class="btn btn-primary btn-sm" @click="addRow(index)"><i class="fa fa-plus"></i></button>
                                    <button class="btn btn-danger btn-sm" @click="removeRow(index)"><i class="fa fa-minus"></i></button>
                                </td>
                            </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="3" class="text-right">DELIVERY</td>
                        <td colspan="1" class="text-right"><input class="form-control text-right" v-model="delivery" number/></td>
                        <td></td>
                    </tr>
                </tfoot>
            </table>

        </div>
    </b-card>
</template>

<script>
import Vue from 'vue'
import accounting from 'accounting'

export default {

filters:{
moneyFormat: function (val){
     if (val > 0) {
            return accounting.formatMoney(val, " ₹ ", 2, ",", ".");
        }
}
},
   data: function () {
    return {
        billChgDescOpt: [
            { value: '', text: 'Select' },
            { value: 'M', text: 'Maintenance Fee'},
            { value: 'W', text: 'Water Charges'},
            { value: 'P', text: 'Penalty Fee'},
            ],
        rows: [
            {qty: 5, billChgDesc: '', charges: 55.20, tax: 10},
            {qty: 19, billChgDesc: '', charges: 1255.20, tax: 20},
        ],
        grandtotal: 0,
        delivery: 40
    }

  },
    computed: {
        total: function () {
            var t = 0;
            $.each(this.rows, function (i, e) {
                t += accounting.unformat(e.total, ",");
            });
            return t;
        },
        taxtotal: function () {
            var tt = 0;
            $.each(this.rows, function (i, e) {
                tt += accounting.unformat(e.tax_amount, ",");
            });
            return tt;
        }
    },
    methods: {

        addRow: function (index) {
            try {
                this.rows.splice(index + 1, 0, {});
            } catch(e)
            {
                console.log(e);
            }
        },
        removeRow: function (index) {
            this.rows.splice(index, 1);
        },
        displayValue:function (e) {
            var value = e.target.value
         var a = this.filters.moneyFormat(value);
         return a;
        }
    }
}
</script>

<style lang="scss" scoped>
.is-danger{
  color:  RED;
}
</style>
EN

回答 1

Stack Overflow用户

发布于 2018-06-05 03:44:34

正如前面所说的,如果你想使用过滤器,你需要做this.$options.filters.moneyFormat(value)

您试图实现的是在输入中呈现moneyFormat,而显示的值是v-model。这是你必须格式化的。

因此,您可以初始化一个新的数据属性,其中填充了在装载时格式化的每个row.charges

代码语言:javascript
复制
data: function () {
  return {
    rows: [
      //...
    ],
    currentCharges: []
  }
},

mounted() {
  this.rows.forEach(row => {
    let formattedCharges = this.$options.filters.moneyFormat(row.charges)
    this.currentCharges.push(formattedCharges)
  })
},

并使用此数据来完成您的输入:

代码语言:javascript
复制
<tr v-for="(row, index) in rows">
  <td> 
    <input v-model="currentCharges[index]" @input="displayValue($event, index)">
  <td>

若要使当前模型保持更新,请在v- row.charges更新时重新指定它:

代码语言:javascript
复制
methods: {
  displayValue:function (e, index) {
    // the target value is a string like this " ₹ 55.20"
    // split it and convert the last element to Float type
    let arrValue = e.target.value.split(" ")
    let parseValue = parseFloat(arrValue[arrValue.length -1])

    // reassign the row.charges with the new float value
    this.rows[index].charges = parseValue
  }
},
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50680751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档