2014-08-29 59 views
0

我試圖根據下拉選擇更改標題和模板,然後單擊搜索按鈕。既然有很多行代碼,我就把所有必要的東西放在一起。我得到這個錯誤LastCPRDate is not defined。以下是完整的錯誤消息的更多詳細信息。Knockout根據下拉選擇更改模板

代碼

vmSearchDetailsModel = function() { 

    var self = this; 
    self.SearchResults = ko.observableArray([]); // Populate the Search Results 

    self.Partners = ko.observableArray([]); // Populate the Partners 
    self.selectedPartner = ko.observable("0"); 

    self.Filters = ko.observableArray([]); // Populate the Filters 
    self.selectedFilter = ko.observable("0"); // not using this show/hide 

    self.ShowAData = ko.observable(false); 
    self.ShowBData= ko.observable(false); 
    self.ShowCData= ko.observable(false); 

    self.GetSearchDetails = function (data) { 

     $.ajax({ 
      type: 'POST', 
      url: 'some url', 
      dataType: 'json', 
      data: {"PartnerID": self.selectedPartner }, 
      success: function (result) { 
       if (result.ErrorMsg == "") { 
        ko.mapping.fromJSON(result.data, {}, self.SearchResults); 
        var selPartner = $("#ReportType").val(); 
        console.log(selPartner) // the first time I get the selected valuer here. 

        if(selPartner=="1") // show the the header and template of the first and hid the others 
        { 
         //Show A Data 
         self.ShowAData(true); 
         self.ShowBData(false); 
         self.ShowCData(false); 
         console.log(self.ShowAData()); //shows true 
        } 
        if(selPartner=="2") 
        { 
         //Show B Data 
         self.ShowAData(false); 
         self.ShowBData(true); 
         self.ShowCData(false); 
         console.log(self.ShowBData()); 
        } 

        if(selPartner=="3") 
        { 
         //Show C Data 
         self.ShowAData(false); 
         self.ShowBData(false); 
         self.ShowCData(true); 
         console.log(self.ShowCData()); 
        } 

       } else { 

        self.ClearData(); // just clears the data 
       } 

      }, 
      error: function (xhr, ajaxOptions, thrownError) { 
       self.ClearData(); 
      } 
      }); 
     } 
} 

綁定

$(document).ready(function() { 
    objvmSearchDetails = new vmSearchDetailsModel() 
    ko.applyBindings(objvmSearchDetails, $("#GridContent")[0]); 
    objvmSearchDetails.GetReportTypes(); 
}); 

HTML頭

這裏基礎上,下拉頭的變化。 LastCPR僅在第二個和第三個下拉列表中可用。 Gridsort是自定義綁定

第一次當我選擇一個下拉菜單並單擊搜索按鈕時,數據和標題顯示正確。再次將下拉菜單更改爲第二個選項,然後單擊搜索它是正確的。當我將下拉菜單更改爲首選項時,標題將隱藏而不是數據。

爲什麼標題被隱藏。我假設自從A的可觀察性被設定爲假,它就會被隱藏起來。那麼爲什麼模板仍然顯示B的數據。 (下面的模板代碼)。

所以我試着點擊搜索按鈕來查看A的數據是否會出現,但我得到了下面的錯誤。 TextualEditBinding is another custom binding。 LastCPR屬於B的數據。我在這裏做錯了什麼?

Uncaught ReferenceError: Unable to process binding "if: function(){return (objvmSearchDetails.ShowBData()==true) }" Message: Unable to process binding "textualEditDate: function(){return LastCPRDate }" Message: LastCPRDate is not defined

<!-- ko if: (objvmSearchDetails.ShowAData()==true) --> 
<div style="display: none" class="grid_ccf_Lay_title" data-bind="visible:SearchResults().length>0"> 
<div> 
    <div > 
       <a data-bind="GridSort:'SearchResults.ClientID'">Child ID</a></div> 
    </div> 
    <div> 
     <div > 
       <a data-bind="GridSort:'SearchResults.InterActionDesc'">Interaction Desc</a></div> 
     </div> 
</div> 
<!-- /ko --> 

<!-- ko if: (objvmSearchDetails.ShowBData()==true) --> 
<div style="display: none" class="grid_ccf_Lay_title" data-bind="visible:SearchResults().length>0"> 
<div> 
    <div > 
       <a data-bind="GridSort:'SearchResults.ClientID'">Child ID</a></div> 
    </div> 
    <div> 
     <div > 
      <a data-bind="GridSort:'SearchResults.LastCPRDate'">Last CPR</a></div> 
     </div> 
</div> 
<!-- /ko --> 

<!-- ko if: (objvmSearchDetails.ShowCData()==true) --> 
<div style="display: none" class="grid_ccf_Lay_title" data-bind="visible:SearchResults().length>0"> 
<div> 
    <div > 
       <a data-bind="GridSort:'SearchResults.ClientID'">Child ID</a></div> 
    </div> 
    <div> 
     <div > 
      <a data-bind="GridSort:'SearchResults.LastCPRDate'">Last CPR</a></div> 
     </div> 
</div> 
<!-- /ko --> 

** **模板

<script type="text/html" id="TmplSearchResults"> 
    <!-- ko if: (ShowAData()==true) --> 
      <!-- Lay --> 
      <div> 
       <div data-bind="text: ClientID"> 
        </div> 
      </div> 
      <div > 
       <div data-bind="textualEditDate: CorrespondenceDate"> 

       </div> 
      </div> 
      <!-- /ko --> 


    <!-- ko if: (ShowBData()==true) --> 
      <!-- Lay --> 
      <div> 
       <div data-bind="text: ClientID"> 
        </div> 
      </div> 
      <div > 
       <div data-bind="textualEditDate: LastCPR"> 

       </div> 
      </div> 
      <!-- /ko --> 


    <!-- ko if: (ShowCData()==true) --> 
      <!-- Lay --> 
      <div> 
       <div data-bind="text: ClientID"> 
        </div> 
      </div> 
      <div > 
       <div data-bind="textualEditDate: LastCPR"> 

       </div> 
      </div> 
      <!-- /ko --> 
</script> 

更新1

我有textualEditDate這又是另一種自定義綁定,只是發生在MM-日期DD-YYYY格式並使用momentjs將其轉換爲DD-MMM-YYYY格式

更新2

我被映射之前清除observableArray也if語句後移動映射擺脫錯誤的。我認爲這不是正確的方法。任何建議將是有益的

更改代碼

self.SearchResults([])清除observablearray

後,如果塊ko.mapping.fromJSON(result.data, {}, self.SearchResults);這樣

self.SearchResults([]); 

if(selPartner=="1") // show the the header and template of the first and hid the others 
{ 
    .... 
} 
'''' 
'''' 
ko.mapping.fromJSON(result.data, {}, self.SearchResults); 

現在我需要找出如何停止頭遷此從我將下拉菜單更改爲原始選項後清除

+0

如果定義了lastCRRdate,請檢查您的回覆 – 2014-08-29 12:07:39

+0

當我將下拉菜單更改爲第一個選項時,最後一個CPR不存在。爲什麼它仍然在尋找我不確定 – Adrian 2014-08-29 12:46:34

+0

我更新了我的問題我如何阻止錯誤來臨,但不知道它是否是正確的方式 – Adrian 2014-08-29 15:32:26

回答

0

SearchResults是一個observableArray。所以SearchResults.LastCPRDate是未定義的,因爲observableArray沒有該屬性,而我懷疑數組中的單個項目具有該屬性。

GridSort想要什麼作爲參數?它可能是NAME列嗎?如果是這樣,請將其作爲字符串傳遞。現在你傳遞一個根本不存在的屬性。

+0

是''SearchResults.LastCPRDate''不存在可觀察數組。它爲什麼一直在尋找它。我第一次搜索observable有這些'''ClientID「:11abc,」CorrespondenceDate「:」2014年6月「。第二次 - 「LastCPRDate」:「2013-05-28T00:00:00」並回到第一個不在可觀察數組 – Adrian 2014-08-29 13:11:23

+0

並且GridSort需要列名稱。這種綁定只是對網格進行排序 – Adrian 2014-08-29 13:16:28

+0

但是,當我單擊搜索按鈕時,我想根據下拉列表更改標題和模板數據 – Adrian 2014-08-29 13:21:46