【问题标题】:Vue Js How to calculate the value on the table and display the sum on the footerVue Js如何计算表格上的值并在页脚显示总和
【发布时间】:2019-01-01 05:03:08
【问题描述】:

我想使用引导表和 Vue Js 创建简单的发票。

基本上,我想要的如下图所示:

我已经尝试过如下代码,但我对两件事感到困惑,

我应该怎么做

1) 计算total cost 并将其显示为footer summary

2)rateqnty相乘,显示在cost对应的输入框上。

new Vue({
  el: '#app',
  methods: {
    addService() {
      this.model.services.push({});
    }
  },
  data: {
    model: {
      services: []
    },
    fields: [{
        key: "rate",
        label: "Rate"
      },
      {
        key: "qnty",
        label: "Qnty"
      },
      {
        key: "cost",
        label: "Cost"
      }
    ]
  }
})
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css" />
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css" />

<script src="https://unpkg.com/vue"></script>
<script src="//unpkg.com/babel-polyfill@latest/dist/polyfill.min.js"></script>
<script src="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.js"></script>


<div id="app">
  <b-card header-tag="header" footer-tag="footer">
    <template slot="header" class="mb-0">
                    <button type="button" class="btn btn-primary btn-sm" @click.prevent="addService">
                        <icons :icon="['fas', 'plus']" /> Add Items/Service</button>
                </template>
    <b-card-body>
      <b-table responsive bordered striped hover caption-top :fields="fields" :items="model.services" foot-clone>
        <template slot="rate" slot-scope="data">

<b-form-input size="sm" class="form-control" v-model="data.item.rate" :name="`rate_${data.index}`" type="text" />

        </template>
        <template slot="qnty" slot-scope="data">
        
 <b-form-input size="sm" class="form-control" v-model="data.item.qnty" :name="`qnty_${data.index}`" type="text" />
 
         </template>
        <template slot="cost" slot-scope="data">
                            
         <b-form-input size="sm" class="form-control" v-model="data.item.cost" :name="`cost_${data.index}`" type="text" />
                        
        </template>
      </b-table>
    </b-card-body>
  </b-card>
</div>

我想要的方式很容易通过使用普通的tdtr 和计算函数来实现。

但我对如何使用Bootstrap-vue 实现感到困惑。

请帮忙!

【问题讨论】:

    标签: vuejs2 bootstrap-vue


    【解决方案1】:

    这是一种快速计算物品成本的方法

    &lt;b-form-input :value="(data.item.rate * data.item.qnty) || 0" type="text" /&gt;

    可以在此处进行改进以更新项目中的项目总数,方法是使用手表更新数据。

    不过,总数是使用计算值完成的,该计算值使用reduce 来查找总数

      computed: {
        total: function() {
          return this.model.services.reduce(function(a, c){return a + Number((c.rate*c.qnty) || 0)}, 0)
        }
      },
    

    这里是完整的代码:

    Vue.config.productionTip = false
    Vue.component('icons', {
      template: '<a><slot></slot></a>'
    })
    new Vue({
      el: '#app',
      methods: {
        addService() {
          this.model.services.push({});
        }
      },
      computed: {
        total: function() {
          return this.model.services.reduce(function(a, c){return a + Number((c.rate*c.qnty) || 0)}, 0)
        }
      },
      data: {
        model: {
          services: []
        },
        fields: [{
            key: "rate",
            label: "Rate"
          },
          {
            key: "qnty",
            label: "Qnty"
          },
          {
            key: "cost",
            label: "Cost"
          }
        ]
      }
    })
    <link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css" />
    <link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css" />
    
    <script src="https://unpkg.com/vue"></script>
    <script src="//unpkg.com/babel-polyfill@latest/dist/polyfill.min.js"></script>
    <script src="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.js"></script>
    
    
    <div id="app">
      <b-card header-tag="header" footer-tag="footer">
        <template slot="header" class="mb-0">
                        <button type="button" class="btn btn-primary btn-sm" @click.prevent="addService">
                            <icons :icon="['fas', 'plus']" /> Add Items/Service</button>
                    </template>
        <b-card-body>
          <b-table responsive bordered striped hover caption-top :fields="fields" :items="model.services" foot-clone>
            <template slot="rate" slot-scope="data">
    
    <b-form-input size="sm" class="form-control" v-model="data.item.rate" :name="`rate_${data.index}`" type="text" />
    
            </template>
            <template slot="qnty" slot-scope="data">
            
     <b-form-input size="sm" class="form-control" v-model="data.item.qnty" :name="`qnty_${data.index}`" type="text" />
     
             </template>
            <template slot="cost" slot-scope="data">
                                
             <b-form-input size="sm" class="form-control" :value="(data.item.rate * data.item.qnty) || 0" :name="`cost_${data.index}`" type="text" />
                            
            </template>
            <template slot="bottom-row" slot-scope="data">
            <td/><td>Total</td>
              <td>{{total}}</td>
            </template>
          </b-table>
          
        </b-card-body>
      </b-card>
    </div>

    【讨论】:

    • 感谢您的回答,我已经得到了解决方案或我的第二个问题,但对于第一个问题,您已将 Total 移到 b-table 之外并使用计算方法显示总数.但我有一些特定的布局来显示总数,这在问题的屏幕截图上。
    • 对模板使用bottom-row,如本文所述bootstrap-vue.js.org/docs/components/table
    • bottom-row。是我需要的。感谢您为我指明正确的方向。
    猜你喜欢
    • 2019-08-30
    • 1970-01-01
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    相关资源
    最近更新 更多