我有创建模板.html
<div data-bind="foreach: productOverGift, visible: expanded" class="free-gift-oder75">
<div class="product-over-info">
<input type="radio" class="product-information" name="productId" data-bind="attr: { value: id, id:'product_over_'+id}, click: $parent.selectItem(id, sku)"/>
<image data-bind="attr: { src: image}" class="product-image" width="120" height="150" alt="Product Image" style="width: 50px; height: 50px"/>
<span data-bind="text: name"></span>
</div>
</div>
并创建文件js
define(
[
'jquery',
'ko',
'uiComponent',
'underscore',
'Magento_Checkout/js/model/step-navigator',
'Magento_Customer/js/model/customer',
'Magento_Checkout/js/model/quote',
'mage/url',
'mage/storage',
'Magento_Checkout/js/action/get-totals',
'mage/translate',
'Magento_Ui/js/model/messageList',
'Magento_Checkout/js/model/full-screen-loader'
],
function (
$,
ko,
Component,
_,
stepNavigator,
customer,
quote,
urlBuilder,
storage,
getTotal,
$t,
messageList,
startLoader
) {
'use strict';
/**
* check-login - is the name of the component's .html template
*/
return Component.extend({
defaults: {
template: 'Custom_Checkout/promotion'
},
//add here your logic to display step,
isVisible: ko.observable(true),
isLogedIn: customer.isLoggedIn(),
//step code will be used as step content id in the component template
stepCode: 'promotion-step',
//step title value
stepTitle: 'Logging Status',
productOverGift: ko.observableArray([]),
freeGiftWithPurchase: ko.observableArray([]),
productImageSelected: ko.observable(""),
productNameSelected: ko.observable(""),
idProductSelected: ko.observable(""),
quoteId: ko.observable(""),
isChecked: ko.observable(true),
skuProductSelected: ko.observable(""),
messageError: ko.observable(""),
message: ko.observable(""),
expanded: ko.observable(true),
arrowClass: ko.observable('fa fa-angle-double-up'),
arrowTitle: ko.observable($t('Shrink')),
dataShippingPnr: ko.observable(""),
dataShippingFullName: ko.observable(""),
dataShippingEmail: ko.observable(""),
dataSegmentDisplay: ko.observable(""),
isCheckedOver: ko.observable(true),
isDeleted: false,
/**
*
* @returns {*}
*/
initialize: function () {
this._super();
//register your step
stepNavigator.registerStep(
this.stepCode,
//step alias
null,
this.stepTitle,
//observable property with logic when display step or hide step
this.isVisible,
_.bind(this.navigate, this),
/**
* sort order value
* 'sort order value' < 10: step displays before shipping step;
* 10 < 'sort order value' < 20 : step displays between shipping and payment step
* 'sort order value' > 20 : step displays after payment step
*/
15
);
return this;
},
/**
* select radio
* @param id
* @param quoteId
* @param sku
*/
selectItem: function (id, sku) {
var self = this;
var listProduct = this.productOverGift();
var itemChecked = $('#product_over_' + id).is(":checked");
if (itemChecked) {
if (listProduct.length > 0) {
listProduct.forEach(function (item, i) {
if (item.id === id) {
self.productImageSelected(item.image);
self.productNameSelected(item.name);
self.idProductSelected(item.id);
self.skuProductSelected(item.sku);
}
});
}
// self.deleteItemInCart(oldProductSku, this.quoteId());
// self.saveProductToOrder(id, this.quoteId(), sku);
}
},
});
}
);
我有一个函数 "selectItem() "用在循环中,用于输入=>标签点击后(选中)会调用 "selectItem "函数。但是现在 "selectItem "函数总是在页面加载的时候被调用,这不是我想要的。我只是想让它在点击输入后被调用,请帮帮我。请帮帮我,谢谢
目前,你是在HTML中执行该函数,而不是将其引用绑定到点击处理程序中。这将在页面加载时执行该函数。
click: $parent.selectItem(id, sku)
相反,你应该在点击处理程序中绑定一个函数的引用。
click: $parent.selectItem
有多种方法来访问你的... ... id
和 sku
属性,但最简单的可能是意识到传递到点击处理程序的第一个参数是当前模型在 forEach: productOverGift
. 所以重写你的签名 selectItem
函数的外观是这样的。
selectItem: function (data) {
let id = data.id
let sku = data.sku
...
这样你就能用了 然而,你可能会考虑看看下面的 checked
绑定。这可以帮助你整顿你的代码,并以一种更像KO的风格来做,但这不是必要的。如果你有兴趣解决这个问题,那么你可以直接将radio的值绑定到 itemChecked
属性,并使用观察者来运行你的selectItem代码。
produceViewModel.itemChecked.subscribe(function(newValue) {
if (newValue) {
// select product image here, etc
}
});
或者用 productImage
选择属性可以是由 itemChecked
.
这将摆脱你的代码中的jQuery依赖,并开始以一种更加反应式的KO风格来做这一切,但要做到这一点,你需要将一些代码从你的 $parent
并进入产品虚拟机。