2016-07-11 23 views
0

我有一段代碼,當我的頁面加載有兩個獵犬數據源引導預輸入

//Bloodhound - Fetches all Project numbers for the current user 
     var prsysList = new Bloodhound({ 
      datumTokenizer: Bloodhound.tokenizers.whitespace, 
      queryTokenizer: Bloodhound.tokenizers.whitespace, 
      prefetch: { 
       url: "../Helper/LookUpProjectNumber/", 
       cache: false 
      } 
     }); 


     //Typeahead on project numbers 
     $('.typeahead').typeahead({ 
      hint: true, 
      highlight: true, 
      minLength: 1 
     }, 
      { 
       name: 'prsys', 
       source: prsysList 
      }); 

現在,當用戶選擇一個值,我想從後端獲取信息,根據他以前的選擇限制他未來的選擇。要做到這一點,我還有一個警犬返回我的信息的更新列表

//Bloodhound - Fetches all Project numbers for the current user 
      var newPrsysList = new Bloodhound({ 
       datumTokenizer: Bloodhound.tokenizers.whitespace, 
       queryTokenizer: Bloodhound.tokenizers.whitespace, 
       prefetch: { 
        url: '../Helper/GetCommonPrsys?prsys=' + encodeURIComponent($selectedPrsys), 
        cache: false 
       } 
      }); 


      //Init the new variable 
      newPrsysList.initialize(); 

      //Rebind all Typeaheads on project numbers 
      $('.typeahead').typeahead({ 
       hint: true, 
       higlight: true, 
       minlength: 1 
      }, 
       { 
        name: 'prsys', 
        source: newPrsysList 
       }); 

我的問題是,儘管我的第二獵犬有數據可用的更新版本供用戶選擇,列表中的事實沒有被更新。

我在這裏做錯了什麼?

最佳

回答

0

我最初的想法是簡單地創建第二個警犬變量給我的預輸入有了它,在希望的來源將被自動更新(如上面的問題解釋)。事實證明,情況並非如此,我的類型數據未更新。

爲了使它工作,我不得不做一個單獨的ajax查詢,將結果存儲到一個jQuery變量中,使用jQuery變量創建一個血腥的(使用本地的)變量,並最終提供我的typeahead。

$.ajax({ 
       url: "../Helper/FunctionName", 
       data: ({ variable1: $var1 }), 
       type: "GET", 
       success: function (data) { 

        //Destroy all typeaheads 
        $('.typeahead').typeahead('destroy'); 

        var newData= new Bloodhound({ 
         datumTokenizer: Bloodhound.tokenizers.whitespace, 
         queryTokenizer: Bloodhound.tokenizers.whitespace, 
         local: data 
        }); 


        //Init the new variable 
        newData.initialize(); 

        //Rebind all Typeaheads on project numbers 
        $('.typeahead').typeahead({ 
         hint: true, 
         higlight: true, 
         minlength: 1 
        }, 
         { 
          name: 'data', 
          source: newData 
         }); 


        return false; 
       }, 
       error: function (data) { 
        //Destroy all typeaheads 
        $('.typeahead').typeahead('destroy'); 
        return false; 
       } 
      }); 

IMO這應該採取幾分鐘來實現,但我想無論是我沒有找到一個簡單的解決方案或負責typeaheads的人沒有實現數據源更新。

祝你好運