Javascript 我如何使用淘汰赛';s";hasfocus“;在具有多个字段:值对的页面上单击以编辑(示例2)

Javascript 我如何使用淘汰赛';s";hasfocus“;在具有多个字段:值对的页面上单击以编辑(示例2),javascript,knockout.js,Javascript,Knockout.js,如何在具有多个字段:值对的页面上使用?我有一个查看客户详细信息的页面,我希望能够在双击时进行编辑。您需要创建PersonViewModels数组,并在视图中foreach循环它们。要重用淘汰页面上的示例,代码可能如下所示: (function () { function PersonViewModel(name) { // Data this.name = ko.observable(name); t

如何在具有多个字段:值对的页面上使用?我有一个查看客户详细信息的页面,我希望能够在双击时进行编辑。

您需要创建PersonViewModels数组,并在视图中foreach循环它们。要重用淘汰页面上的示例,代码可能如下所示:

    (function () {
        function PersonViewModel(name) {
            // Data
            this.name = ko.observable(name);
            this.editing = ko.observable(false);

            // Behaviors
            this.edit = function() { this.editing(true) }
        }

        function ViewModel(personModels) {
            this.persons = ko.observableArray(personModels);
        }

        var personModels = [
            new PersonViewModel('Bert'),
            new PersonViewModel('James'),
            new PersonViewModel('Eddy')
        ];

        ko.applyBindings(new ViewModel(personModels));
    })();
以及以下观点:

<div data-bind="foreach: persons">
    <p>
        Name: 
        <b data-bind="visible: !editing(), text: name, click: edit">&nbsp;</b>
        <input data-bind="visible: editing, value: name, hasfocus: editing" />
    </p>
    <p><em>Click the name to edit it; click elsewhere to apply changes.</em></p>
</div>


姓名:

单击要编辑的名称;单击其他位置以应用更改

下面是一个JSFIDLE演示: