0
Im使用foreach函數列出數據。每行都包含一個編輯按鈕,可以響應點擊事件。但是當你點擊它時,列表中的每個按鈕都在改變。我希望每個按鈕選擇每個項目,以獨立工作。 請檢查此琴fiddleDEMOknockoutjs選擇對象在單擊內部foreach
var ViewModel = function() {
var self = this;
this.selectedIndex = ko.observable(0);
this.onIndexSelected = function (index) {
this.selectedIndex(index);
};
self.buttonState = ko.observable();
self.editar = ko.observable('Edit');
self.changeState = function(state){
console.log('changed value');
console.log(state)
self.buttonState(state);
}
self.changeButtonValue = function(){
if (self.buttonState() == false) {
console.log('entro al if');
self.changeState(true);
self.editar('Edit');
} else {
console.log('else');
self.changeState(false);
self.editar('Save');
}
}
this.items = [
{id:1, name: 'Northwoods Prestige'},
{id:2, name: 'Forest Bay'},
{id:3, name: 'Timberland'}
];
};
var viewModel = new ViewModel();
ko.applyBindings(viewModel);
謝謝!
我沒有看到你在這段代碼中使用'ko.observablearray'。我敢肯定,如果沒有它,你將無法得到你想要的行爲。另外,你可以分享你的「視圖」嗎?我們需要了解如何處理HTML中'foreach'循環中的每個項目。 – coralvanda