ノックアウト JS を使用して、テーブル内の Web サイト名と URL のペアの編集可能なリストを処理しています。新しいアイテムを追加するためのボタンが下にあります。私のコードは、リストとコレクションのチュートリアルに基づいています。
....
<tbody data-bind="foreach: website">
<tr>
<td><input data-bind="value: name, hasfocus: true" /></td>
<td><input data-bind="value: url" /></td>
<td><a href="#" data-bind="click: $root.removeWebsite">Remove</a></td>
</tr>
</tbody>
</table>
<button data-bind="click: addWebsite" class="btn">Add another</button>
hasfocus: true
新しい空白のフィールドが追加されるたびに、マウスでクリックする必要がないように使用することに注意してください。以下のコード (viewmodel 関数から) を見ると、リストの最後のテキスト ボックスの内容をチェックしてから、別のテキスト ボックスを追加できるようになっていることがわかります。
// Add and remove
self.addWebsite = function() {
var length = self.website().length;
if (self.website()[length - 1].name == '') {
// Last site in list has no name, don't allow them to add another
return false;
}
self.website.push(new dashboardApp.website());
}
実行時にバインディングを変更する方法はありますか?そのため、次のような空白行をさらに作成するのではなく、入力する必要があることをユーザーにプロンプトとして最後の項目にフォーカスを与えることができます。
self.website()[length - 1].name.hasfocus = true;