【问题标题】:rails, getting bootstrap's typeahead to work with rails and jquery (which work normally)rails,让引导程序的 typeahead 与 rails 和 jquery 一起工作(正常工作)
【发布时间】:2012-04-24 23:53:43
【问题描述】:

我想知道是否有人可以帮助我使用 rails 和 jquery 自动完成引导“预先输入”

在我的“新”动作模板中,我有...

        <div class="tags">
            <%= f.label :tag_names, "Tags" %>
            <%= f.text_field :tag_names,
                data: { autocomplete_source: tags_path} %>
        </div>

然后转到我的标签控制器的索引操作

    def index
       @tags = Tag.order(:name).where("name like ?", "%#{params[:term]}%")
       render json: @tags.map{ |tag| {:label => "#{tag.name} x #{tag.count}", :value => tag.name} }
    end

下面是我的 jquery ui 自动完成代码,虽然我认为这里不是很有必要...但这里是 incase

  $(function() {
  var extractLast, split;
  split = function(val) {
    return val.split(/,\s*/);
  };
  extractLast = function(term) {
    return split(term).pop();
  };
  return $("#lesson_tag_names").bind("keydown", function(event) {
    if (event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) {
      return event.preventDefault();
    }
  }).autocomplete({
    source: function(request, response) {
      return $.getJSON($("#lesson_tag_names").data("autocomplete-source"), {
        term: extractLast(request.term)
      }, response);
    },
    search: function() {
      var term;
      term = extractLast(this.value);
      if (term.length < 1) {
        return false;
      }
    },
    focus: function() {
      return false;
    },
    select: function(event, ui) {
      var terms;
      terms = split(this.value);
      terms.pop();
      terms.push(ui.item.value);
      terms.push("");
      this.value = terms.join(", ");
      return false;
    }
  });
});

自动完成功能有效,但我似乎无法让它与 twitters 引导程序一起使用。 我尝试提供

data: { autocomplete_source: tags_path, provide: "typeahead"} %>

$('.typeahead').typeahead()

进入我的 application.js 文件。但它似乎不起作用。我还尝试给我的表单一个 id 属性...

                <%= f.text_field :tag_names, id: "autocomplete",
                data: { autocomplete_source: tags_path} %>

所以我可以做类似的事情

    $('#autocomplete').typeahead()

但是,通过给我的表单一个 :id 属性,我的自动完成功能将停止工作。所以...我不太确定出了什么问题。还有其他方法吗?

我们将不胜感激。谢谢

【问题讨论】:

    标签: jquery ruby-on-rails twitter-bootstrap typeahead


    【解决方案1】:

    我遇到了类似的问题,最终将 jQuery UI 自动完成设置为像 bootstrap typeahead 一样的样式(基本上将 bootstrap CSS 应用于 jQuery UI 元素)。

    我是怎么做到的: http://criticalthinking.tumblr.com/post/17940560421/making-jquery-ui-look-like-bootstrap

    【讨论】:

    • 哦,太好了,你有没有找到一种方法让字母在有人打字时变粗?
    • @Crystal.. 没有尝试过 - 但我认为它可能需要一些自定义 JS..
    • 嗯,我想我只是不加粗体了。它不适合我哈哈。感谢您回答并发送有用的链接 = )
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 2017-01-20
    • 2021-06-28
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    相关资源
    最近更新 更多