我对所有值求和,但为什么它不适用于所有动态合计字段
I Sum All Values But Why It's Not Working For All Dynamic Total Fields
我正在使用 Laravel 5.7
& VueJs 2.5.*
...
简而言之: 当我输入值时,我在总计字段中得到总和,但是当我动态添加项目行时,它不适用于其他项目行,它只适用于第一行。
说明: 我有一些 Taxes_Breakup
的输入字段,我需要对所有这些字段求和并显示在 Total_Tax_BreakUp
字段中。在我的 TicketInvoiceItems
中,我有 Taxes_Breakup
个字段和一个 Total_Taxes_Breakup
字段,当我输入所有值时,我得到 Total_Taxes_Breakup
中的总和,但是当我添加 TicketInvoiceItems
字段时动态地,TicketInvoiceItems
中的其他 Total_Tax_BreakUp
字段与第一个 Total_Tax_BreakUp
相同 field.Below 是更好的图像 understanding.Thank 你。
这是我的 HTML
代码:
<tr v-for="(ticketInvoiceItem, key) in form.ticketInvoiceItems" :key="key">
<!--Passenger Name-->
<td>
<input v-model="ticketInvoiceItem.passenger_name" size="40" type="text" name="passenger_name" class="table-control form-control" :class="{ 'is-invalid': form.errors.has('passenger_name') }">
</td>
<!-------------------Taxes BreakUps------------------->
<input v-model="ticketInvoiceItem.tax_SB" id="tax_SB" type="number" name="tax_SB" placeholder="SB" class="table-control form-control" :class="{ 'is-invalid': form.errors.has('tax_SB') }">
<input v-model="ticketInvoiceItem.tax_SRP" id="tax_SRP" type="number" name="tax_SRP" placeholder="SRP" class="table-control form-control" :class="{ 'is-invalid': form.errors.has('tax_SRP') }">
<!-------------------AND OTHER TAXES------------------->
<!-------------------Total Taxes Break Up------------------->
<td>
<input :value="totalTax" id="total_tax_breakup" type="number" size="10" name="total_tax_breakup" class="table-control form-control" :class="{ 'is-invalid': form.errors.has('total_tax_breakup') }">
</td>
</tr>
我的 VueJs data()
:
data() {
return {
editmode: true,
ticketInvoices: {},
form: new Form({
id: "",
vendor_id: "",
ticket_invoice_no: "",
ticket_invoice_date: "",
ticket_invoice_grand_total: "",
ticketInvoiceItems: [{
id: "",
ticket_invoice_id: "",
passenger_name: "",
sector: "",
tax_SB: 0,
tax_SRP: 0,
tax_YQ: 0,
tax_RG: 0,
total_tax_breakup: 0,
}]
})
};
},
我的 computed:
代码:
computed: {
totalTax() {
let calTaxTotal =
parseInt(this.form.ticketInvoiceItems[0].tax_SB) +
parseInt(this.form.ticketInvoiceItems[0].tax_SRP) +
parseInt(this.form.ticketInvoiceItems[0].tax_YQ) +
parseInt(this.form.ticketInvoiceItems[0].tax_RG);
this.form.ticketInvoiceItems[0].total_tax_breakup = calTaxTotal;
return calTaxTotal;
}
},
<tr v-for="(ticketInvoiceItem, key) in form.ticketInvoiceItems" :key="key">
<!--Passenger Name-->
<td>
<input v-model="ticketInvoiceItem[key].passenger_name" size="40" type="text" name="passenger_name" class="table-control form-control" :class="{ 'is-invalid': form.errors.has('passenger_name') }" @change="totalTax(key)">
</td>
<!-------------------Taxes BreakUps------------------->
<input v-model="ticketInvoiceItem[key].tax_SB" id="tax_SB" type="number" name="tax_SB" placeholder="SB" class="table-control form-control" :class="{ 'is-invalid': form.errors.has('tax_SB') }" @change="totalTax(key)">
<input v-model="ticketInvoiceItem[key].tax_SRP" id="tax_SRP" type="number" name="tax_SRP" placeholder="SRP" class="table-control form-control" :class="{ 'is-invalid': form.errors.has('tax_SRP') }" @change="totalTax(key)">
<!-------------------AND OTHER TAXES------------------->
<!-------------------Total Taxes Break Up------------------->
<td>
<input :value="totalTax" id="total_tax_breakup" type="number" size="10" name="total_tax_breakup" class="table-control form-control" :class="{ 'is-invalid': form.errors.has('total_tax_breakup') }">
</td>
</tr>
对我来说,我将使用 @change
而不是 computed
将下面的代码放在 methods
totalTax(key) {
let calTaxTotal =
parseInt(this.form.ticketInvoiceItems[key].tax_SB) +
parseInt(this.form.ticketInvoiceItems[key].tax_SRP) +
parseInt(this.form.ticketInvoiceItems[key].tax_YQ) +
parseInt(this.form.ticketInvoiceItems[key].tax_RG);
this.form.ticketInvoiceItems[key].total_tax_breakup = calTaxTotal;
return calTaxTotal;
}
问题出在你的计算 属性 上。您所有的索引都在 0 或 table 的第一行。如果你想加起来不止一行,你需要将索引传递到你的 totalTax 中。所以计算 属性 不是最好的选择。而是将它变成一个方法,并将索引(或键)传递给该方法。
<input :value="getTotalTaxes(key)"
id="total_tax_breakup"
type="number" size="10"
name="total_tax_breakup"
class="table-control form-control">
方法
methods: {
/**
* The index parameter is the current row that you are working on
* @param index
* @returns {number}
*/
getTotalTaxes(index) {
let calTaxTotal =
parseInt(this.form.ticketInvoiceItems[index].tax_SB) +
parseInt(this.form.ticketInvoiceItems[index].tax_SRP) +
parseInt(this.form.ticketInvoiceItems[index].tax_YQ) +
parseInt(this.form.ticketInvoiceItems[index].tax_RG);
this.form.ticketInvoiceItems[index].total_tax_breakup = calTaxTotal;
return calTaxTotal;
}
}
我正在使用 Laravel 5.7
& VueJs 2.5.*
...
简而言之: 当我输入值时,我在总计字段中得到总和,但是当我动态添加项目行时,它不适用于其他项目行,它只适用于第一行。
说明: 我有一些 Taxes_Breakup
的输入字段,我需要对所有这些字段求和并显示在 Total_Tax_BreakUp
字段中。在我的 TicketInvoiceItems
中,我有 Taxes_Breakup
个字段和一个 Total_Taxes_Breakup
字段,当我输入所有值时,我得到 Total_Taxes_Breakup
中的总和,但是当我添加 TicketInvoiceItems
字段时动态地,TicketInvoiceItems
中的其他 Total_Tax_BreakUp
字段与第一个 Total_Tax_BreakUp
相同 field.Below 是更好的图像 understanding.Thank 你。
这是我的 HTML
代码:
<tr v-for="(ticketInvoiceItem, key) in form.ticketInvoiceItems" :key="key">
<!--Passenger Name-->
<td>
<input v-model="ticketInvoiceItem.passenger_name" size="40" type="text" name="passenger_name" class="table-control form-control" :class="{ 'is-invalid': form.errors.has('passenger_name') }">
</td>
<!-------------------Taxes BreakUps------------------->
<input v-model="ticketInvoiceItem.tax_SB" id="tax_SB" type="number" name="tax_SB" placeholder="SB" class="table-control form-control" :class="{ 'is-invalid': form.errors.has('tax_SB') }">
<input v-model="ticketInvoiceItem.tax_SRP" id="tax_SRP" type="number" name="tax_SRP" placeholder="SRP" class="table-control form-control" :class="{ 'is-invalid': form.errors.has('tax_SRP') }">
<!-------------------AND OTHER TAXES------------------->
<!-------------------Total Taxes Break Up------------------->
<td>
<input :value="totalTax" id="total_tax_breakup" type="number" size="10" name="total_tax_breakup" class="table-control form-control" :class="{ 'is-invalid': form.errors.has('total_tax_breakup') }">
</td>
</tr>
我的 VueJs data()
:
data() {
return {
editmode: true,
ticketInvoices: {},
form: new Form({
id: "",
vendor_id: "",
ticket_invoice_no: "",
ticket_invoice_date: "",
ticket_invoice_grand_total: "",
ticketInvoiceItems: [{
id: "",
ticket_invoice_id: "",
passenger_name: "",
sector: "",
tax_SB: 0,
tax_SRP: 0,
tax_YQ: 0,
tax_RG: 0,
total_tax_breakup: 0,
}]
})
};
},
我的 computed:
代码:
computed: {
totalTax() {
let calTaxTotal =
parseInt(this.form.ticketInvoiceItems[0].tax_SB) +
parseInt(this.form.ticketInvoiceItems[0].tax_SRP) +
parseInt(this.form.ticketInvoiceItems[0].tax_YQ) +
parseInt(this.form.ticketInvoiceItems[0].tax_RG);
this.form.ticketInvoiceItems[0].total_tax_breakup = calTaxTotal;
return calTaxTotal;
}
},
<tr v-for="(ticketInvoiceItem, key) in form.ticketInvoiceItems" :key="key">
<!--Passenger Name-->
<td>
<input v-model="ticketInvoiceItem[key].passenger_name" size="40" type="text" name="passenger_name" class="table-control form-control" :class="{ 'is-invalid': form.errors.has('passenger_name') }" @change="totalTax(key)">
</td>
<!-------------------Taxes BreakUps------------------->
<input v-model="ticketInvoiceItem[key].tax_SB" id="tax_SB" type="number" name="tax_SB" placeholder="SB" class="table-control form-control" :class="{ 'is-invalid': form.errors.has('tax_SB') }" @change="totalTax(key)">
<input v-model="ticketInvoiceItem[key].tax_SRP" id="tax_SRP" type="number" name="tax_SRP" placeholder="SRP" class="table-control form-control" :class="{ 'is-invalid': form.errors.has('tax_SRP') }" @change="totalTax(key)">
<!-------------------AND OTHER TAXES------------------->
<!-------------------Total Taxes Break Up------------------->
<td>
<input :value="totalTax" id="total_tax_breakup" type="number" size="10" name="total_tax_breakup" class="table-control form-control" :class="{ 'is-invalid': form.errors.has('total_tax_breakup') }">
</td>
</tr>
对我来说,我将使用 @change
而不是 computed
将下面的代码放在 methods
totalTax(key) {
let calTaxTotal =
parseInt(this.form.ticketInvoiceItems[key].tax_SB) +
parseInt(this.form.ticketInvoiceItems[key].tax_SRP) +
parseInt(this.form.ticketInvoiceItems[key].tax_YQ) +
parseInt(this.form.ticketInvoiceItems[key].tax_RG);
this.form.ticketInvoiceItems[key].total_tax_breakup = calTaxTotal;
return calTaxTotal;
}
问题出在你的计算 属性 上。您所有的索引都在 0 或 table 的第一行。如果你想加起来不止一行,你需要将索引传递到你的 totalTax 中。所以计算 属性 不是最好的选择。而是将它变成一个方法,并将索引(或键)传递给该方法。
<input :value="getTotalTaxes(key)"
id="total_tax_breakup"
type="number" size="10"
name="total_tax_breakup"
class="table-control form-control">
方法
methods: {
/**
* The index parameter is the current row that you are working on
* @param index
* @returns {number}
*/
getTotalTaxes(index) {
let calTaxTotal =
parseInt(this.form.ticketInvoiceItems[index].tax_SB) +
parseInt(this.form.ticketInvoiceItems[index].tax_SRP) +
parseInt(this.form.ticketInvoiceItems[index].tax_YQ) +
parseInt(this.form.ticketInvoiceItems[index].tax_RG);
this.form.ticketInvoiceItems[index].total_tax_breakup = calTaxTotal;
return calTaxTotal;
}
}