2013-07-29 167 views
0

以下爲對象的樣本結構:KnockoutJS添加,編輯,刪除

[{ 
    MasterType: "mtype1", 
    Types: [{ 
     TypeStage: "st1", 
     TypeDate: "12/02/2001", 
     SubTypes: [{ 
      SubTypeData: "st1" 
     }, { 
      AnotherData: "ad1" 
     }] 
    }, { 
     TypeStage: "st3", 
     TypeDate: "15/02/2001", 
     SubTypes: [{ 
      SubTypeData: "st3" 
     }, { 
      AnotherData: "ad3" 
     }] 
    }] 
}, 

{ 
    MasterType: "mtype2", 
    Types: [{ 
     TypeStage: "st2", 
     TypeDate: "12/04/2001", 
     SubTypes: [{ 
      SubTypeData: "st2" 
     }, { 
      AnotherData: "ad2" 
     }] 
    }] 
}]; 

注:在頁面加載,對象是空的。

我需要顯示每個行的編輯/刪除的MasterType表。 我也需要一個「添加」按鈕,然後隱藏表格並顯示輸入字段以輸入新的MasterType的值。 保存新的MasterType後,我需要隱藏輸入字段並再次顯示錶格。

請幫

我也做了以下內容:

   <table> 
        <tbody data-bind="foreach: MasterTypes"> 
         <tr><td data-bind='text: MasterType'></td></tr> 
        </tbody> 
       </table> 

       <div > 
        <table> 
         <tbody data-bind="foreach: MasterTypes"> 
          <tr> 
           <td> 
            <input data-bind="value: MasterType,valueUpdate: 'afterkeydown'" /> 
            <div><a href='#' data-bind='click: $root.removeMasterType'>Delete</a></div> 
           </td> 
           <td> 
            <table> 
             <tbody data-bind="foreach: Types"> 
              <tr> 
               <td><input data-bind='value: TypeStage' /></td> 
               <td><input data-bind='value: TypeDate' /></td> 
               <td><a href='#' data-bind='click: $root.removeType'>Delete</a></td> 
               <td> 
               <table> 
                <tbody data-bind="foreach: SubTypes"> 
                 <tr> 
                  <td><input data-bind='value: Discharge' /></td> 
                  <td><a href='#' data-bind='click: $root.removeSubType'>Delete</a></td> 
                 </tr> 
                </tbody> 
               </table> 
               <a href='#' data-bind='click: $root.addSubType'>Add New Sub type</a> 
              </tr> 
             </tbody> 
            </table> 
            <a href='#' data-bind='click: $root.addType'>Add new Type</a> 
           </td> 
          </tr> 
         </tbody> 
        </table> 
       </div> 

       <p> 
        <button data-bind='click: addMasterType'>Add New MasterType</button> 
        <button data-bind='click: save, enable: MasterTypes().length > 0'>Save to JSON</button> 
       </p> 

       <textarea data-bind='value: lastSavedJson' rows='5' cols='60' disabled='disabled'> </textarea> 
       <script type="text/javascript"> 

        var MasterTypesModel = function (mastertypes) { 
         var self = this; 
         self.tableVisible = ko.observable(true); 
         self.MasterTypes = ko.observableArray(ko.utils.arrayMap(mastertypes, function (mastertype) { 
          return { MasterType: mastertype.MasterType, Types: ko.observableArray(ko.utils.arrayMap(mastertype.Types, function (type) { 
           return { TypeStage: type.TypeStage, TypeDate: type.TypeDate, SubTypes: ko.observableArray(type.SubTypes) }; 
          })) 
          }; 
         })); 

         self.addMasterType = function() { 
          // self.tableVisible(false); 
          self.MasterTypes.push({ 
           MasterType: "", 
           Types: ko.observableArray(
             [{ TypeStage: "", TypeDate: "", SubTypes: ko.observableArray()}] 
            ) 
          }); 
         }; 

         self.removeMasterType = function (mastertype) { 
          self.MasterTypes.remove(mastertype); 
         }; 

         self.addType = function (mastertype) { 
          mastertype.Types.push({ 
           TypeStage: "", 
           TypeDate: "", 
           SubTypes: ko.observableArray() 
          }); 
         }; 

         self.removeType = function (type) { 
          $.each(self.MasterTypes(), function() { 
           this.Types.remove(type) 
          }) 
         }; 

         self.addSubType = function (type) { 
          type.SubTypes.push({ 
           Discharge: "" 
          }); 
         }; 

         self.removeSubType = function (subtype) { 
          $.each(self.MasterTypes(), function() { 
           $.each(this.Types(), function() { 
            this.SubTypes.remove(subtype) 
           }) 
          }) 
         }; 

         self.save = function() { 
          self.lastSavedJson(JSON.stringify(ko.toJS(self.MasterTypes), null, 2)); 
          $.ajax({ 
           url: "/MyController/UpdateMasterType", 
           contentType: 'application/json', 
           type: 'POST', 
           data: ko.toJSON(self.MasterTypes), 
           success: function (data) { 
            self.lastSavedJson = ko.observable("") 
            alert('That is it!'); 
           } 
          }); 
         }; 

         self.lastSavedJson = ko.observable("") 
        };     

        ko.applyBindings(new MasterTypesModel()); 

       </script> 
+0

你還做了什麼? – Damien

+0

請參閱編輯後的文章,看看我有什麼。我無法將其添加爲評論,因爲它超過了允許的字符數。 – user1918553

+0

感謝您的時間每個人...我找到了答案....請找到最後得到答案的原文。 – user1918553

回答

0

的OP寫道:

謝謝大家....我已經找到了答案:

   <table data-bind="visible:tableVisible">  
        <tr><td> <button data-bind='click: addMasterType'>Add New MasterType</button></td></tr> 
         <!-- ko foreach: MasterTypes --> 
         <tr> 
          <td data-bind='text: MasterType'></td> 
          <td><button data-bind='click: $root.editMasterType'>Edit</button></td>         
         </tr> 
         <!-- /ko -->  
       </table> 

       <div > 
        <table> 
         <tbody data-bind="with: SelectedMasterType"> 
          <tr> 
           <td> 
            <input data-bind="value: MasterType,valueUpdate: 'afterkeydown'" />           
           </td> 
           <td> 
            <table> 
             <tbody data-bind="foreach: Types"> 
              <tr> 
               <td><input data-bind='value: TypeStage' /></td> 
               <td><input data-bind='value: TypeDate' /></td> 
               <td><a href='#' data-bind='click: $root.removeType'>Delete</a></td> 
               <td> 
               <table> 
                <tbody data-bind="foreach: SubTypes"> 
                 <tr> 
                  <td><input data-bind='value: Discharge' /></td> 
                  <td><a href='#' data-bind='click: $root.removeSubType'>Delete</a></td> 
                 </tr> 
                </tbody> 
               </table> 
               <a href='#' data-bind='click: $root.addSubType'>Add New Sub type</a> 
              </tr> 
             </tbody> 
            </table> 
            <a href='#' data-bind='click: $root.addType'>Add new Type</a> 
           </td> 
          </tr> 
         </tbody> 
        </table> 
       </div> 

       <p> 
        <button data-bind='click: addMasterType'>Add New MasterType</button> 
        <button data-bind='click: save, enable: MasterTypes().length > 0'>Save to JSON</button> 
       </p> 

       <textarea data-bind='value: lastSavedJson' rows='5' cols='60' disabled='disabled'> </textarea> 
       <script type="text/javascript"> 

        var MasterTypesModel = function (mastertypes) { 
         var self = this; 
         self.SelectedMasterType = ko.observable(null); 
         self.tableVisible = ko.observable(true); 
         self.MasterTypes = ko.observableArray(ko.utils.arrayMap(mastertypes, function (mastertype) { 
          return { MasterType: ko.observable(mastertype.MasterType), Types: ko.observableArray(ko.utils.arrayMap(mastertype.Types, function (type) { 
           return { TypeStage: type.TypeStage, TypeDate: type.TypeDate, SubTypes: ko.observableArray(type.SubTypes) }; 
          })) 
          }; 
         })); 

         self.addMasterType = function() { 
          self.tableVisible(false); 
          self.MasterTypes.push({ 
           MasterType: ko.observable(""), 
           Types: ko.observableArray(
             [{ TypeStage: "", TypeDate: "", SubTypes: ko.observableArray()}] 
            ) 
          }); 
          self.SelectedMasterType(self.MasterTypes()[self.MasterTypes().length - 1]); 
         }; 

         self.editMasterType = function (masteryype) { 
          self.tableVisible(false); 
          self.SelectedMasterType(masteryype); 
         }; 

         self.removeMasterType = function (mastertype) { 
          self.MasterTypes.remove(mastertype); 
         }; 

         self.addType = function (mastertype) { 
          mastertype.Types.push({ 
           TypeStage: "", 
           TypeDate: "", 
           SubTypes: ko.observableArray() 
          }); 
         }; 

         self.removeType = function (type) { 
          $.each(self.MasterTypes(), function() { 
           this.Types.remove(type) 
          }) 
         }; 

         self.addSubType = function (type) { 
          type.SubTypes.push({ 
           Discharge: "" 
          }); 
         }; 

         self.removeSubType = function (subtype) { 
          $.each(self.MasterTypes(), function() { 
           $.each(this.Types(), function() { 
            this.SubTypes.remove(subtype) 
           }) 
          }) 
         }; 

         self.save = function() { 
         self.lastSavedJson(JSON.stringify(ko.toJS(self.MasterTypes), null, 2)); 
          $.ajax({ 
           url: "/MyController/UpdateMasterType", 
           contentType: 'application/json', 
           type: 'POST', 
           data: ko.toJSON(self.MasterTypes), 
           success: function (data) { 
            self.lastSavedJson = ko.observable("") 
            self.SelectedMasterType(null); 
            self.tableVisible(true); 
            alert('That is it!'); 
           } 
          }); 
         }; 

         self.lastSavedJson = ko.observable("") 
        }; 

        ko.applyBindings(new MasterTypesModel()); 

       </script> 
+0

([在編輯中回答問題並轉換爲社區wiki](http://meta.stackoverflow.com/questions/267434/what-is-the-woole-action-when-the-answer-to-a-問題 - 被添加到所述闕))。 –

相關問題