【问题标题】:Jquery tokenInput not prePopulatingJquery tokenInput 未预先填充
【发布时间】:2011-06-16 20:52:12
【问题描述】:

您好,我正在关注 this tutorial,但在编辑我的模型时预填充该字段不起作用..

请注意,text_field category_tokens 在编辑时未预先填充类别。

这里是代码 sn-ps,text_field 的 HTML 输出显示了值。

  <input id="product_category_tokens" type="text" size="30" name="product[category_tokens]" data-pre="[{"created_at":"2010-09-13T03:33:17Z","description":"","id":x,"name":"Kitchen & Dining ","parent_id":xx,"permalink":"kitchen-dining","updated_at":"2011-01-05T11:17:10Z"}]" style="display: none;">

  application.js
    $(function() {
      $("#product_category_tokens").tokenInput("/categories.json", {
          crossDomain: false,
          prePopulate: $('#product_category_tokens').data('pre'),
          preventDuplicates: true
      });
    });

    <%= f.text_field :category_tokens, "data-pre" => @product.categories.map(&:attributes).to_json %>

【问题讨论】:

  • 你的开发日志是什么样的? categories.json 中的任何错误?
  • categories.json 显示正确的 json 格式。事实上,该插件可用于创建新类别。它只是在编辑产品时不会预先填充当前类别。
  • 它应该从那个 json url 预填充。您确定您按照指南进行操作吗?
  • 是的,我遵循了指南,不应该基于“data-pre”字段的预填充工作 @product.categories。 map(&:attributes).to_json %>
  • @anshu 所以你已经设置好了所有东西,你只是把教程中的所有东西都改成了类别?

标签: javascript jquery ruby-on-rails jquery-plugins jquery-tokeninput


【解决方案1】:

这是重复的:Jquery tokenInput not prepopulating the text_field

答案就在最后:

<%= f.text_field :category_tokens, input_html => { "data-pre" => @product.categories.map(&:attributes).to_json } %>

我没有对此表示赞赏(这不是我的答案)只是想我会指出这一点,以防它帮助其他人,因为我也被它抓住了。

我还想指出,在 Shawn 的评论中,您可以通过将其添加到您的 js.coffee 文件中来解决这个问题:

propertyToSearch: "author"

author 更改为您的列名,这将起作用。 :name 只是默认值,但您可以在此方法中覆盖它。 jquery tokeninput 页面上的更多文档:http://loopj.com/jquery-tokeninput/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 2013-11-12
    相关资源
    最近更新 更多