exercises
observableArray 内に配列がありworkouts
ます。そこからアイテムを削除する機能を追加したいと思います。それをobservableArrayにしようとしました:
self.exercises = ko.observableArray();
アイテムを削除する機能を追加しました:
self.removeExercise = function() {
self.exercises.remove(this);
};
しかし、何も起こりません。同じ関数がworkouts
親配列で機能しますが。これに加えて、配列内のアイテムを編集するための簡単な関数があります。これは次のようになります。
this.edit = function() { this.editing(true) }
そして、それを私のビューにバインドしてみてください:
<span data-bind="text: name, click: edit"> </span>
しかし、うまくいきません。問題はどこにありますか?
これが私の見解です
<div class="content">
<ul data-bind="foreach: workouts">
<li>
<span data-bind="text: name, click: edit"> </span>
<a href="#" data-bind="click: $parent.removeWorkout">Remove</a>
<ul data-bind="foreach: exercises">
<li>
<span data-bind="text: name"> </span>
<a href="#" data-bind="click: $parent.removeExercise">remove</a>
</li>
</ul>
</li>
</ul>
</div>
そしてViewModel:
function AppViewModel() {
var self = this;
self.workouts = ko.observableArray([
{name: "Workout1", exercises:[
{ name: "Exercise1.1" },
{ name: "Exercise1.2" },
{ name: "Exercise1.3" }
]},
]);
self.exercises = ko.observableArray();
self.removeWorkout = function() {
self.workouts.remove(this);
};
self.removeExercise = function() {
self.exercises.remove(this);
};
this.editing = ko.observable(false);
this.edit = function() { this.editing(true) }
}
ko.applyBindings(new AppViewModel);
jsfiddle の例を次に示します: http://jsfiddle.net/c9fQB/
ありがとう!