使用没有默认' name'的jquery TokenInput [英] Using jquery TokenInput without default 'name'

查看:48
本文介绍了使用没有默认' name'的jquery TokenInput的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用在此处找到的jquery令牌输入: http://loopj.com/jquery-tokeninput/遵循铁路广播的指南: http://railscasts.com/episodes/258-token-字段

Im trying to use the jquery tokeninput found here: http://loopj.com/jquery-tokeninput/ Following the guide from railcasts: http://railscasts.com/episodes/258-token-fields

默认情况下,它在表/json上需要一个名称"列.

By default, it required a 'name' column on the table/json..

有什么自定义方式可以将其更改为其他名称(在我的情况下为"account_number")?

Any way to customize to change it to something else (in my case, 'account_number')?

(在重播中,他说,如果您没有名称"列,则需要进行额外的自定义)

(in the reailcast, he says if you dont have a 'name' column, youll require extra customization)

推荐答案

神奇的线条是

<%= f.text_field :author_tokens, "data-pre" => @book.authors.map(&:attributes).to_json %>

format.json { render :json => @authors.map(&:attributes) }

这些行将从表中读取的数据转换为jquery-tokeninput可以理解的json.它将所有数据从模型传递到jquery-tokeninput中,但这不是必需的.令牌输入,仅需要两个字段

These lines convert the data read from table into the json which jquery-tokeninput can understand. It passes on all the data from the model into, jquery-tokeninput, but it is not necessary. Tokeninput, only needs two fields,

    每个选定令牌的
  1. id ->,与表单一起发布
  2. 名称->用作令牌的标签
  1. id -> for each selected token, this is posted along with form
  2. name -> used as label of the token

如果您不想在模型中使用 name 字段,并希望使用 account_number 作为标签,则可以执行以下操作:

If you don't want to have a name field in your model, and want to use account_number as the label, you can do it like following:

<%= f.text_field :author_tokens, "data-pre" => @book.authors.collect {|author| {:id => author.id, :name => author.account_number } } %>

format.json { render :json => @authors.collect {|author| {:id => author.id, :name => author.account_number } }

基本上,更改传递给tokeninput的json.通过 accoun_number 作为 name .

Basically, change the json passed to tokeninput. Pass accoun_number as name.

更新:

将此行更改为更适合您的内容:

Change this line to something which better suits you:

@authors = Author.where("name like ?", "%#{params[:q]}%")

一个建议可能是:

@authors = Author.where("name like ?", "#{params[:q]}%")

删除第一个,但实际上取决于您的数据类型和全部.

Remove the first %, but really depends on your data type and all.

这篇关于使用没有默认&amp;#39; name&amp;#39;的jquery TokenInput的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆