【问题标题】:Implementing Json Autocomplete for category为类别实现 Json 自动完成
【发布时间】:2013-02-24 17:36:30
【问题描述】:

我正在尝试实现使用 rails 分类的 jquery 自动完成功能。

我能够报告如下所示的 json 响应

["Air Canada to appeal Quebec court ruling on Aveos","First Nations meeting with PM in jeopardy","Neanderthals, humans may have missed each other","New York state enacts tougher gun control measures","Ontario high school teachers drop next week's walkout plan","test","Jean-Sebastien","Serge","Dean","feng","Heather","Carmen","Frank"]

他是 user.firstname 和 article.titles 的组合,由以下响应生成。

render :json => (@titles.map(&:title) || []) | (@cus.map(&:first_name) || [])

问题是这对正常的 jquery 自动完成很有用,但我正在寻找分类,我想知道如何创建带有标题所在的标签以及类别标签将存在的标签的对象,我会根据对象的类型插入“用户”或“文章”。

这个想法是为 jquery 类别提供以下输出

  { label: "annttop C13", category: "Products" },
  { label: "anders andersson", category: "People" },
  { label: "andreas andersson", category: "People" },
  { label: "andreas johnson", category: "People" }

【问题讨论】:

    标签: jquery ruby-on-rails ruby json ruby-on-rails-3


    【解决方案1】:

    我没有看到任何快速的方法,但像这样

    @titles.map { |title| { label: title.title, category: 'Products' } } +
    @cus.map { |cus| { label: cus.first_name, category: 'People' } }
    

    【讨论】:

    • 谢谢,我希望这对分类的 jquery json 有用,但这是有道理的,谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-06-23
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 2013-01-18
    • 2013-01-28
    相关资源
    最近更新 更多