我認爲你需要的東西記錄資產對象的原始值,以便您可以將當前值與原始值進行比較。使用計算器可以輸出所需的CSS。
http://jsfiddle.net/gonefishern/c7y2A/3/
HTML
<input type="text" data-bind="value: filter, valueUpdate: 'afterkeydown'" placeholder="Filter assets" />
<table>
<thead>
<tr>
<th>Asset Name</th>
<th>Serial Number</th>
<th>Manufacturer</th>
</tr>
</thead>
<tbody data-bind="foreach: {data: filteredAssets}">
<tr data-bind="css: flashCss, click: $parent.selectAsset">
<td data-bind="text: name"></td>
<td data-bind="text: serialNumber"></td>
<td data-bind="text: manufacturer"></td>
</tr>
</tbody>
</table>
<br/>
<br/>
<div data-bind="with: selectedAsset">
<p>Selected Asset</p>
<label>Asset Name</label>
<input type="text" data-bind="value: name"/>
</div>
的JavaScript
var asset = function (name, serialNumber, manufacturer) {
var self = this;
self.name = ko.observable(name);
self.serialNumber = ko.observable(serialNumber);
self.manufacturer = ko.observable(manufacturer);
self.name.original = name;
self.serialNumber.original = serialNumber;
self.manufacturer.original = manufacturer;
self.hasChanged = ko.computed(function(){
if (self.name() == self.name.original && self.serialNumber() == self.serialNumber.original && self.manufacturer() == self.manufacturer.original) {
return false;
}
return true;
});
self.flashCss = ko.computed(function() {
//just an example
if (self.hasChanged()){
return 'flash';
}
return '';
});
};
var viewmodel = function() {
var self = this;
self.filter = ko.observable('');
self.assets = ko.observableArray([
new asset("asset 1", "12345", "Dell"),
new asset("asset 2", "67890", "Dell"),
new asset("asset 3", "12098", "HP"),
new asset("asset 4", "55443", "Dell")]);
// Method to filter the assets table based on user input. Computed observable is throttled to ensure it doesn't kick in too quickly.
self.filteredAssets = ko.computed(function() {
var filter = self.filter().toLowerCase();
if (!filter) {
return self.assets();
} else {
return ko.utils.arrayFilter(self.assets(), function (item) {
return item.name().toLowerCase().indexOf(filter) > -1;
});
}
}).extend({
throttle: 500
});
self.selectedAsset = ko.observable();
self.selectAsset = function(item){
self.selectedAsset(item);
};
};
ko.applyBindings(new viewmodel());
CSS
.flash {
background-color: yellow
}
'return item.name()。toLowerCase()。indexOf(filter)> -1;'將讓你的過濾器工作。您是否有一個可以添加,編輯和刪除項目的例子?你可以通過給每行一個ID('myID'+ $ index)來做到這一點,然後用計算的jQuery設置類 –