简体   繁体   English

kickout.js observablearray推送未绑定单击

[英]knockout.js observablearray push not binding click

I have some html as follows: 我有一些HTML,如下所示:

<table id="table">
    <thead>
        <tr>
            <th>Employee ID</th>
            <th>Name</th>
            <th>Role</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: Employees">        
        <tr>
            <td data-bind="text: Empid"></td>
            <td data-bind="text: Name"></td>
            <td data-bind="text: Role"></td>
            <td>
                <a data-bind="click: editEmployee">Edit</a>
            </td>
        </tr>            

    </tbody>
</table>

<div id="createForm">
    <input id="empidC" />
    <input id="nameC" />
    <input id="roleC" />
    <button type="button" id="insertEmployee">Create</button>
</div>

And I have a javascript file containing knockout code: 而且我有一个包含敲除代码的javascript文件:

    $('#insertEmployee').click(function () {

        var empidC = $("#empidC").val(),
            nameC = $("#nameC").val(),
            roleC = $("#roleC").val();

        $.ajax({
            type: "POST",
            url: "/Employees/Create",
            data: JSON.stringify({ "Empid": empidC, "Name": nameC, "Role": roleC }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {

                // Data gets inserted in DB and alert statement is executing
                alert(msg);              

                eList.Employees.push({
                    Empid:empidC,
                    Name:nameC,
                    Role:roleC
                });

            },
            error: function (xhr, status, error) {
                alert(xhr.responseText);
            }
        });

    });

    var getEmployees=function(){
        return $.get("/Employees/FetchEmployees");
    }

    //View Model for all Employees
    var EmployeeListVM = function () {
        var self = this;

        self.Employees = ko.observableArray();         

        getEmployees().done(function (data) {
            var emp = data.map(function (e) {
                return (new Employees(e));
            });
            self.Employees(emp);
        });

    };

    //Model for all Employees
    function Employees(data) {

        var self = this;

        this.Empid = ko.observable(data.Empid);
        this.Name = ko.observable(data.Name);
        this.Role = ko.observable(data.Role);

        this.editEmployee = function (record) {

            var eEdit = new SingleEmployee(record);

            var cont = document.getElementById("editForm");
            ko.cleanNode(cont);
            ko.applyBindings(eEdit, cont);

            $("#editForm").dialog({
                autoOpen: false,
                modal: true,
                width: 450,
                title: "Edit Selected Record(s)"                
            });

            $("#editForm").dialog("open");           

        };

    }

    // Viewmodel for editing single record
    function SingleEmployee(data) {

        var self = this;
        this.Empid = ko.observable(data.Empid);
        this.Name = ko.observable(data.Name);
        this.Role = ko.observable(data.Role);
    };

    var eList = new EmployeeListVM();
    ko.applyBindings(eList, document.getElementById("table"));

When I insert any new record in div "createForm", I get an error: 当我在div“ createForm”中插入任何新记录时,出现错误:

Uncaught ReferenceError: Unable to process binding "click: function (){return editEmployee }" Message: editEmployee is not defined 未捕获ReferenceError:无法处理绑定“单击:函数(){返回editEmployee}”消息:未定义editEmployee

The new row is getting displayed, but the Edit link for that newly inserted record does not work 将显示新行,但是该新插入记录的“编辑”链接不起作用

EDIT: 编辑:

I solved the problem. 我解决了问题。 The problem was that "Object" was getting passed to Employees(data) and not "Employees". 问题是“对象”被传递给Employees(data),而不是“ Employees”。 So in the insertEmployee click, I changed code to: 因此,在insertEmployee单击中,我将代码更改为:

            var newEmp = new Employees({
                Empid: empidC,
                Name: nameC,
                Role: roleC
            });

            eList.Employees.push(newEmp);

Is the data actually getting to your server side controller and updating the DB? 数据实际上是否到达服务器端控制器并更新数据库? The ajax call to post data and call the Create method on the server, in the Success section assumes that all is ok on the server. “成功”部分中的ajax调用用于发布数据并调用服务器上的Create方法,并假定在服务器上一切正常。 All the success path does here is to say that the client side call was ok, It's not a server success. 成功路径在这里所做的所有事情就是说客户端调用可以,这不是服务器成功。 Where you currently have your alert statement, you need to ensure that the server side actually posted the data before attempting to retrieve it later in the refresh of the page. 当前您有警报语句的位置,您需要确保服务器端实际发布了数据,然后在稍后刷新页面之前尝试检索它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM