如何在嵌套的foreach中调用后方处理程序功能?
我曾备有语句,并用后面来调用该功能。
<div data-bind="foreach: { data: Object.keys(pricings()), as: '_propkey', afterRender: pricingTableRenderedHandler }">
在此迭代中,_propkey是一个字符串。然后,我不得不将foreach语句更改为以下:
<div data-bind="foreach: { data: pricings(), as: '_propertykey', afterRender: pricingTableRenderedHandler}">
<div data-bind="foreach: { data: _propertykey.states, as: '_secondarypropkey'}">
。
self.pricingTableRenderedHandler = function (data, stateName) {
var tableSelector = "#" + stateName.states.state.replace(/\s/g, '');
hideEmptyTableColumns.hide(tableSelector);
replaceEmptyTableCells.replaceBy(self.emptyCellValue, tableSelector);
}
如果旧foreach,则在statename参数中接收了字符串 现在,它收到一个看起来像这样的对象。
[
"cropYear" : "2020"
"states": [
{
"state": "Tbilisi",
"details": {
"PriceHeaders": [
],
"Comment": "TBi",
"Pricings": [
]
}
}
{
"state": "Texas",
"details": {
"PriceHeaders": [
],
"Comment": "dasdsa",
"Pricings": [
]
}
}
]
]
我只需要采用“状态”属性值。 我假设我需要在Inner foreach中调用Handler功能,但是由于某种原因,我不能。除非将其写在第一个foreach中,否则它不会被调用。
I had foreach statement and used afterRender to call the function.
<div data-bind="foreach: { data: Object.keys(pricings()), as: '_propkey', afterRender: pricingTableRenderedHandler }">
During this iteration, _propkey was a single string. Then I had to change foreach statement to this :
<div data-bind="foreach: { data: pricings(), as: '_propertykey', afterRender: pricingTableRenderedHandler}">
<div data-bind="foreach: { data: _propertykey.states, as: '_secondarypropkey'}">
handler function
self.pricingTableRenderedHandler = function (data, stateName) {
var tableSelector = "#" + stateName.states.state.replace(/\s/g, '');
hideEmptyTableColumns.hide(tableSelector);
replaceEmptyTableCells.replaceBy(self.emptyCellValue, tableSelector);
}
In case of old foreach, it received string in stateName parameter. Now, it receives an object that looks like this.
[
"cropYear" : "2020"
"states": [
{
"state": "Tbilisi",
"details": {
"PriceHeaders": [
],
"Comment": "TBi",
"Pricings": [
]
}
}
{
"state": "Texas",
"details": {
"PriceHeaders": [
],
"Comment": "dasdsa",
"Pricings": [
]
}
}
]
]
I only need to take "state" property value.
I'm assuming that I need to call handler function in inner foreach but for some reason, I can not. Unless it is written in the first foreach, it does not get called.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
foreach
绑定为每个元素创建一个新的绑定上下文。如果在模板中,您将传递给,则需要访问原始上下文的属性,则可以使用
$ parent
访问该属性。请注意,该函数将以当前元素(
$ data
)为this
来调用。如果您的处理程序依赖于此
,则可以使用$ parent.methodname.bind($ parent)
。The
foreach
binding creates a new binding context for every element. If, within the template you pass toforeach
, you need to access a property of the original context, you can access that using$parent
.Note that the function will be called with the current element (
$data
) asthis
. If your handler relies onthis
, you can use$parent.methodName.bind($parent)
.