2014-08-29 75 views
0

我已經在我的應用程序中實現了nested_form gem和twitter類型。但是我不能在動態添加的字段中使用這個typeahead。nested_form和twitter typeahead

我的線,其將所述動態線是 <%= ff.link_to_add噸( 「add_author」),:book_authors,:數據=> {:目標=> 「#authors」}%>

而第一行的鍵入的JS是:

$(function(){ 
    var authors = new Bloodhound({ 
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('author'), 
    queryTokenizer: Bloodhound.tokenizers.whitespace, 
    limit: 10, 
    remote: { 
     url: '/load_suggestions?q=%QUERY', 
     filter: function(list) { 
     return $.map(list, function(author) { return {author: book }; }); 
     } 
    } 
    }); 

    authors.initialize(); 

    $('#load_suggestions').typeahead({ 
     hint: true, 
     highlight: true, 
     minLength: 1 
    }, 
    { 
     name: 'authors', 
     displayKey: 'author', 

     source: authors.ttAdapter() 
    }); 
}); 

如何修改上述內容以適用於動態添加的行?謝謝

回答

0

您可以偵聽點擊事件並激活字段添加後的打字, 處理JavaScript事件在nested_form gem的README中給出。

例如

$(document).on('nested:fieldAdded', function(event){ 
     // this field was just inserted into your form 
     var field = event.field; 
     field.typeahead({ 
     hint: true, 
     highlight: true, 
     minLength: 1 
     }, 
     { 
     name: 'authors', 
     displayKey: 'author', 

     source: authors.ttAdapter() 
    }); 
}); 
+0

讓我試試看,我會回來。非常感謝 – Tiamon 2014-08-29 18:36:47

+0

解決。非常感謝! – Tiamon 2014-08-29 18:56:58

+0

您好,我已經在正常的頁面上工作,但它不適用於引導模式。任何建議爲模態?謝謝 – Tiamon 2014-09-01 22:38:16