刺激如何从不同子控制器中的值(购物车 - ; CART项目)中计算出父控制器中的值
我正在建造一个用刺激的购物车。购物车摘要页面显示多个购物车项目。购物车物品具有单价和数量。 每个购物车项目组件都有一个数量选择器,当数量数更改Cartitem元素的总价格totalCartitem = uttercartitem = UnitPrice x量
,如果还必须重新计算code code
code
total cartcartcartprice
和总价= sum(totalcartitem)
这是对HTML结构的简化
<div class="cart" data-controller='cart'>
<div class="cart-item" data-controller='cart-item'>
<p class='unit-price'>20</p>
<input type="number" name="quantity" value="1">
<p class='total-price' data-target='cart-item.totalPrice' data-action="change->cart-item#update" data-target='cart-item.quantity'>20</p>
</div>
<div class="cart-item" data-controller='cart-item'>
<p class='unit-price'>10</p>
<input type="number" name="quantity" value="2" data-action="change->cart-item#update" data-target='cart-item.quantity'>
<p class='total-price' data-target='cart-item.totalPrice'>20</p>
</div>
<div class="cart-total">
40
</div>
</div>
我的购物车项目控制器的工作正常,并正确更新了UI中的CART项目的TotalPrice。
export default class extends Controller {
static targets = ["totalPrice", "quantity"]
static values = {
unitPrice: String,
}
connect() {
console.log("hello from StimulusJS")
}
update(){
this.totalPriceTarget.textContent = parseFloat(this.unitPriceValue) * parseInt(this.quantityTarget.value)
}
}
但是,我现在对如何更新totalcartprice
感到迷失。我觉得这应该是cartcontroller
包装Cartitems元素的责任,但我不知道正确实现此目的的ID是什么ID。 我觉得我应该在每个编号输入选择器上添加更改
fording ,以适用于每个购物车的数量,但是我应该将什么添加到购物车#更新方法中,以重新计算总计每个购物车物品?
export default class extends Controller {
static targets = ["total"]
connect() {
console.log("hello from StimulusJS")
}
update(){
// let details = event.detail;
// this.cartItemChildren.forEach((item) => console.log(item.totalValue))
}
}
I am building a cart with stimulus. The cart recap page displays multiple cart items. A cart item has a unit price and a quantity.
Each cart item component has a quantity selector, when the quantity number changes the total price of the cartitem element is recalculated totalCartItem = unitPrice x Quantity
and the total price if the cart must also be recalculated totalCartPrice = sum(totalCartItem)
Here's a simplification of the html structure
<div class="cart" data-controller='cart'>
<div class="cart-item" data-controller='cart-item'>
<p class='unit-price'>20</p>
<input type="number" name="quantity" value="1">
<p class='total-price' data-target='cart-item.totalPrice' data-action="change->cart-item#update" data-target='cart-item.quantity'>20</p>
</div>
<div class="cart-item" data-controller='cart-item'>
<p class='unit-price'>10</p>
<input type="number" name="quantity" value="2" data-action="change->cart-item#update" data-target='cart-item.quantity'>
<p class='total-price' data-target='cart-item.totalPrice'>20</p>
</div>
<div class="cart-total">
40
</div>
</div>
My cart item controller works perfectly fine and updates correctly the totalPriceFor a cart item in the UI.
export default class extends Controller {
static targets = ["totalPrice", "quantity"]
static values = {
unitPrice: String,
}
connect() {
console.log("hello from StimulusJS")
}
update(){
this.totalPriceTarget.textContent = parseFloat(this.unitPriceValue) * parseInt(this.quantityTarget.value)
}
}
However, I am now lost on how to update the totalCartPrice
. I feel like this should be the responsability of the cartController
that wrapps the cartItems elements, but I have no idea on what id the correct way to achieve this.
I feel like I should add change->cart#update
on each number input selector for quantity for each cart-item, but then what should I add to the cart#update method to recalculate the total from each individual cart item ?
export default class extends Controller {
static targets = ["total"]
connect() {
console.log("hello from StimulusJS")
}
update(){
// let details = event.detail;
// this.cartItemChildren.forEach((item) => console.log(item.totalValue))
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据刺激文档,始终最好开始使用HTML 。通过扩展,最好以可访问的方式构建HTML,并查看浏览器为您提供的东西而不重新发明内容。
当您构建表单时,即使没有提交此表单,也最好使用
表单
元素。 https://developer.mozilla.orgg/en-us/docs/ Web/api/htmlformelement还有一个非常有用的
输出
元素,可以帮助您根据某些输入值分配输出值。 https://develoverer.mozilla.org/ en-us/doc/web/html/element/umput#attr-for最后,形式元素有一个DOM API,可让您通过其
name name 属性,这是
form.elements
。 https:// https://developer.mozilla.org/en-us/ docs/web/api/htmlformelement/elements将其结合在一起,我们可以从以下HTML开始...
html
输入
都有一个唯一的ID,每个输出
元素引用使用此ID的输入使用 for 属性 - 这将更好当我们想解析值并更新输出时,请用方便。JavaScript(控制器)
this.Element
。更新
方法,并尝试使用updateTotals
更具体。elements
api,以获取具有名称'item-total'
的元素(请注意,输出名称未提交)。 https:// https://developer.mozilla.org/en-us/ doc/web/api/htmlformelement/elementsAs per the Stimulus documentation, it is always best to start with the HTML. By extension it is best to structure your HTML in an accessible way and see what the browser gives you without reinventing things.
When you are building forms, it is best to use the
form
element, even if this form is not being submitted. https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElementThere is also a very useful
output
element that helps you assign an output value based on some input value. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/output#attr-forFinally, there is a DOM API for the form element that lets you access various parts of the form via their
name
attribute, this isform.elements
. https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/elementsBringing this together we can start with the following HTML...
HTML
input
has a unique ID and eachoutput
element references the input via this id using thefor
attribute - this will be better for accessibility but comes in handy when we want to parse the values and update outputs.JavaScript (Controller)
this.element
on the base form's attached controller.update
method and trying to be more specific withupdateTotals
.elements
API on the form to get the elements with the name'item-total'
(note that output names do not get submitted). https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/elementsfor
and the input's id and then the data attribute to find the price.