2014-02-27 69 views
0

以下是聲明和基因敲除的初始化對象淘汰賽編輯工作不

var EmployeeModel = function(data) { 
    //Make the self as 'this' reference 
    var self = this; 

    //Declare observable which will be bind with UI 
    self.EmployeeID = ko.observable(""); 
    self.FirstName = ko.observable(""); 
    self.LastName = ko.observable(""); 

    var Employee = { 
     EmployeeID: self.EmployeeID, 
     FirstName: self.FirstName, 
     LastName: self.LastName 
    }; 


    self.Employees = ko.observableArray(data); // Contains the list of employees 
    self.Employee = ko.observable(); 



    // Edit Employee details 
    self.edit = function (Employee) { 
     alert("edit"); 
     self.Employee(Employee); 

     $("#EmployeePreview").css("display", "none"); 
     $("#PaneEdit").css("display", "block"); 
    } 

    // Edit Employee details 
    self.preview = function (Employee) { 
     alert("preview"); 
     self.Employee(Employee); 

     $("#PaneEdit").css("display", "none"); 
     $("#EmployeePreview").css("display", "block"); 
    } 

    // Cancel Employee details 
    self.cancel = function() { 
     self.Employee(null); 
    } 
} 

$(document).ready(function() { 
    $.getJSON("/Employee/GetData", null, function (data) { 
     var viewModel = new EmployeeModel(data); 
     ko.applyBindings(viewModel); 
     viewModel.Employee(viewModel.Employees()[0]); 
    }); 
}); 

以下是我的HTML,這是工作的罰款。在編輯的點擊,視圖模型的編輯方法也越來越稱爲

<table cellspacing="10px" border="0"> 
    <tr> 
    <td width="50%" style="vertical-align:text-top;" > 
     <table class="List" border="0" cellspacing="4px" cellpadding="4px"> 
     <!----> 
     <tbody data-bind="foreach: Employees"> 

      <tr> 
       <td rowspan="2" width="5%"><img src="../../Images/photo.jpg" height="40px" width="40px" alt="Projects" style="cursor:pointer;" onclick=""/></td> 
       <td width="5%"> 
         <img data-bind="click: $root.preview" src="../../Images/Preview.png" height="16px" width="16px" alt="Projects" style="cursor:pointer;" "/> 
       </td> 
       <td data-bind="text: FirstName"></td> 
      </tr> 
      <tr> 
       <td width="5%"> 
        <img data-bind="click: $root.edit" src="../../Images/Edit.png" height="16px" width="16px" alt="Projects" style="cursor:pointer;" onclick="EditEmp();"/></td> 
       <td data-bind="text: LastName"></td> 
      </tr> 
      <!----> 
      </tbody> 
      </table> 
     </td> 

但問題是下面由編輯方法聲明中甚至嘗試很多事情

self.Employee(Employee); 

可以請人幫我後不工作這 在此先感謝

+0

Projects的onclick刪除,你已經有點擊約束力,沒有定義EditEmp。 – Akhlesh

+0

$ root.edit正在被調用...因爲警報正在顯示。但下面一行是給self self.Employee(Employee);即使刪除EditEmp調用它不起作用 – user3357040

回答

1

你有3個員工的變量一個是簡單的對象即VAR員工 2是觀察到的,第三個是編輯方法參數,這樣就不會感到困惑與參數錄用ee它只是返回當前點擊的數據。

var Employee = { //this variable is useless 
    EmployeeID: self.EmployeeID, 
    FirstName: self.FirstName, 
    LastName: self.LastName 
}; 

希望它會工作http://jsfiddle.net/CLCYC/1/

+0

謝謝!它的工作..但前進我面臨新問題,我指的是下面的網站http://www.c-sharpcorner.com/UploadFile/raj1979/add-edit-delete-data-using-knockout-in-mvc- 4 /它具有以下代碼來取消更新self.cancel = function(){self.Employee(null); }即使在調用此方法之後...我的原始對象正在更改 – user3357040