3
每當我救我的項目我重新映射模型執行以下操作:集淘汰賽CSS結合基礎上可觀察到的數組項
ko.mapping.fromJS(data, {}, deal);
我的模型看起來像:
{
"DealId": 0,
"BrokenRules": [
{
"Property": "EndDate",
"Description": "End Date is required."
},
{
"Property": "CustomerId",
"Description": "Customer is required."
},
{
"Property": "LiveState",
"Description": "Live State is required."
},
{
"Property": "WorkState",
"Description": "Work State is required."
}
}
我想設置css課div
基於BrokenRules
陣列的內容,並希望我能做類似的事情:
<div class="control-group" data-bind="css: { error: BrokenRules.filterByProperty('Property', 'EndDate').length !== 0 }">
<label class="control-label">End Date</label>
<div class="controls">
<input type="text" class="span2" name="EndDate" data-bind="value: EndDate, enable: $index() === 0" />
</div>
</div>
但這似乎並不奏效。我的filterByProperty在第一次發射時沒有任何物品,並且由於某種原因,從不再發射。
ko.observableArray.fn.filterByProperty = function (propName, matchValue) {
return ko.computed(function() {
var allItems = this(), matchingItems = [];
for (var i = 0; i < allItems.length; i++) {
var current = allItems[i];
if (ko.utils.unwrapObservable(current[propName]) === matchValue)
matchingItems.push(current);
}
return matchingItems;
}, this);
}
filterByProperty是從knockoutjs網站取得的。
這樣做的任何幫助將不勝感激!謝謝!
,完美的工作!我發誓那些額外的括號將會是我的死亡! :) – mattruma 2013-05-02 15:30:07
如果你設置你的計算器返回一個observableArray,那麼你可以有兩套:) – 2013-05-02 15:31:55