2014-08-29 74 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