Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将Knockout.JS viewmodel绑定到jQuery对话框_Javascript_Jquery_Knockout.js - Fatal编程技术网

Javascript 将Knockout.JS viewmodel绑定到jQuery对话框

Javascript 将Knockout.JS viewmodel绑定到jQuery对话框,javascript,jquery,knockout.js,Javascript,Jquery,Knockout.js,我有一个jQuery模式对话框,我想在其中传递来自淘汰视图模型的数据。对话框按原样工作正常-但是,下面的代码被破坏 理想情况下,我希望能够单击触发模式对话框的URI,并让对话框从敲除viewmodel加载数据。任何帮助都将不胜感激 标记: <a href="#" id="listNames">List Names</a> <div id="listNames" data-bind="dataModel: { autoOpen: false, modal: tr

我有一个jQuery模式对话框,我想在其中传递来自淘汰视图模型的数据。对话框按原样工作正常-但是,下面的代码被破坏

理想情况下,我希望能够单击触发模式对话框的URI,并让对话框从敲除viewmodel加载数据。任何帮助都将不胜感激

标记:

<a href="#" id="listNames">List Names</a>

<div  id="listNames"  data-bind="dataModel: { autoOpen: false, modal: true }">
<div> 
    <form action=''>
        <p>You have added <span data-bind='text: name().length'>&nbsp;</span> 
            person(s)</p>
        <table data-bind='visible: name().length > 0'>
            <thead>
                <tr><th>Select</th>
                    <th>Name</th>
                    <th>Age</th>
                    <th />
                </tr>
            </thead>
            <tbody data-bind='foreach: metrics'>
                <tr>
                    <td><input type="checkbox" /></td>
                    <td><span data-bind='text: name' >&nbsp;</span></td>
                    <td><span  data-bind='text: age'>&nbsp;</span></td> 
                </tr>
            </tbody>
        </table>
    </form>
  </div>
</div>
jQuery对话框:

var dataModel = function (edata) {
    var self = this;
    self.edata = ko.observableArray(edata);

    self.addname = function () {
        self.edata.push({
            name: "",
            age: ""
        });
    };

    self.removename = function (name) {
        self.edata.remove(name);
    };

    self.save = function (form) {
        alert("Could now transmit to server: " 
              + ko.utils.stringifyJson(self.edata));
        // To actually transmit to server as a regular form post, write this: 
        // ko.utils.postJson($("form")[0], self.edata);
    };
};

var viewModel = new dataModel([
    { name: "Jack", age: "41" },
    { name: "Jill", age: "33" }
]);
ko.applyBindings(new viewModel);
$("#listNames, ").dialog({
    autoOpen: false,
    width: 300,
    modal: true,
    buttons: {
        "OK": function () {
            $(this).dialog("destroy");
        },
        Cancel: function () {
            $(this).dialog("close");
        }
    }
});

$("#openList")
    .click(function () {
        $("#listNames").dialog("open");
    });
        <a href="#" data-bind="click: $root.openDialog"> Open dialog </a> //you had 2 elements with the same ID, I removed the ID on the link and bound it to a method in the view model

    <div id="listNames">   <div>
            <form action=''>
                <p>You have added <span data-bind='text: name.length'>&nbsp;</span> person(s)</p> // name item is not observable, so you cannot use name().length
                <table data-bind='visible: name.length > 0'> // same remark for name
                    <thead>
                        <tr>
                            <th>Select</th>
                            <th>Name</th>
                            <th>Age</th>
                            <th />
                        </tr>
                    </thead>
                    <tbody data-bind='foreach: edata'>
                        <tr>
                            <td>
                                <input type="checkbox" />
                            </td>
                            <td><span data-bind='text: name'>&nbsp;</span>

                            </td>
                            <td><span data-bind='text: age'>&nbsp;</span>

                            </td>
                        </tr>
                    </tbody>
                </table>
            </form>
        </div>
    </div>
$("#listNames").dialog({
    autoOpen: false,
    width: 300,
    modal: true,
    buttons: {
        "OK": function () {
            // do something
            $(this).dialog("close"); // I replaced destroy by close, so it can be opened after ok has been clicked
        },
        Cancel: function () {
            $(this).dialog("close");
        }
    }
});

var dataModel = function (edata) {
    var self = this;
    self.edata = ko.observableArray(edata);

    self.addname = function () {
        self.edata.push({
            name: "",
            age: ""
        });
    };

    self.openDialog = function () {
        $("#listNames").dialog("open");
    };

    self.removename = function (name) {
        self.edata.remove(name);
    };

    self.save = function (form) {
        alert("Could now transmit to server: " + ko.utils.stringifyJson(self.edata));
        // To actually transmit to server as a regular form post, write this: ko.utils.postJson($("form")[0], self.edata);
    };
};

var viewModel = new dataModel([{
    name: "Jack",
    age: "41"
}, {
    name: "Jill",
    age: "33"
}]);

ko.applyBindings(viewModel); // you have created a variable viewModel with data, but you bound ko with a new object of type viewModel, you must either call ko with viewModel you created, or inline the creation of a new "dataModel"

您发布的代码中有一些错误。 我这里有一个工作版本:

以下是HTML:

var dataModel = function (edata) {
    var self = this;
    self.edata = ko.observableArray(edata);

    self.addname = function () {
        self.edata.push({
            name: "",
            age: ""
        });
    };

    self.removename = function (name) {
        self.edata.remove(name);
    };

    self.save = function (form) {
        alert("Could now transmit to server: " 
              + ko.utils.stringifyJson(self.edata));
        // To actually transmit to server as a regular form post, write this: 
        // ko.utils.postJson($("form")[0], self.edata);
    };
};

var viewModel = new dataModel([
    { name: "Jack", age: "41" },
    { name: "Jill", age: "33" }
]);
ko.applyBindings(new viewModel);
$("#listNames, ").dialog({
    autoOpen: false,
    width: 300,
    modal: true,
    buttons: {
        "OK": function () {
            $(this).dialog("destroy");
        },
        Cancel: function () {
            $(this).dialog("close");
        }
    }
});

$("#openList")
    .click(function () {
        $("#listNames").dialog("open");
    });
        <a href="#" data-bind="click: $root.openDialog"> Open dialog </a> //you had 2 elements with the same ID, I removed the ID on the link and bound it to a method in the view model

    <div id="listNames">   <div>
            <form action=''>
                <p>You have added <span data-bind='text: name.length'>&nbsp;</span> person(s)</p> // name item is not observable, so you cannot use name().length
                <table data-bind='visible: name.length > 0'> // same remark for name
                    <thead>
                        <tr>
                            <th>Select</th>
                            <th>Name</th>
                            <th>Age</th>
                            <th />
                        </tr>
                    </thead>
                    <tbody data-bind='foreach: edata'>
                        <tr>
                            <td>
                                <input type="checkbox" />
                            </td>
                            <td><span data-bind='text: name'>&nbsp;</span>

                            </td>
                            <td><span data-bind='text: age'>&nbsp;</span>

                            </td>
                        </tr>
                    </tbody>
                </table>
            </form>
        </div>
    </div>
$("#listNames").dialog({
    autoOpen: false,
    width: 300,
    modal: true,
    buttons: {
        "OK": function () {
            // do something
            $(this).dialog("close"); // I replaced destroy by close, so it can be opened after ok has been clicked
        },
        Cancel: function () {
            $(this).dialog("close");
        }
    }
});

var dataModel = function (edata) {
    var self = this;
    self.edata = ko.observableArray(edata);

    self.addname = function () {
        self.edata.push({
            name: "",
            age: ""
        });
    };

    self.openDialog = function () {
        $("#listNames").dialog("open");
    };

    self.removename = function (name) {
        self.edata.remove(name);
    };

    self.save = function (form) {
        alert("Could now transmit to server: " + ko.utils.stringifyJson(self.edata));
        // To actually transmit to server as a regular form post, write this: ko.utils.postJson($("form")[0], self.edata);
    };
};

var viewModel = new dataModel([{
    name: "Jack",
    age: "41"
}, {
    name: "Jill",
    age: "33"
}]);

ko.applyBindings(viewModel); // you have created a variable viewModel with data, but you bound ko with a new object of type viewModel, you must either call ko with viewModel you created, or inline the creation of a new "dataModel"
编辑:我在我的更改中添加了一些注释

编辑2:我更新了指向JSFIDLE的链接以获得正确的版本;)