Javascript Tokeninput将新标记保存到数据库
我试图保存在数据库中不存在的输入字段中输入的新标记,并希望将创建的标记保存到表单提交中。这是我的控制器,它正在将自动完成列表发送到tokeninput字段:Javascript Tokeninput将新标记保存到数据库,javascript,php,jquery,grails,jquery-tokeninput,Javascript,Php,Jquery,Grails,Jquery Tokeninput,我试图保存在数据库中不存在的输入字段中输入的新标记,并希望将创建的标记保存到表单提交中。这是我的控制器,它正在将自动完成列表发送到tokeninput字段: def tags = { def foundTags = Tag.findAllByTagnameIlike("${params.q}%") def output = [] foundTags.each { output.add([id: it.id, name: it
def tags = {
def foundTags = Tag.findAllByTagnameIlike("${params.q}%")
def output = []
foundTags.each {
output.add([id: it.id, name: it.tagname]) // assumes Tag has an id field exposed
}
if(output.size()==0){
def c = Tag.createCriteria()
def maxId = c.get {
projections {
max('id')
}
}
output.add([id:(maxId+1),name:params.q])
}
render output as JSON
}
我的jQuery脚本是:
<script type="text/javascript">
$(document).ready(function () {
$("#my-text-input").tokenInput("${createLink(controller: 'product', action: 'tags')}",{theme: "facebook",allowFreeTagging:"true"});
});
</script>
$(文档).ready(函数(){
$(“#我的文本输入”).tokenInput(${createLink(controller:'product',action:'tags')},{theme:'facebook',allowfreetaging:'true});
});
现在,当我提交表单时,我得到params.tags作为输入字段中新输入标记的id。但实际上,这些id并不存在,而是由output.add创建的([id:(maxId+1),name:params.q])
仅仅是因为tokeninput需要它。
那么,如何在params.tags中获得标记名而不是ID?事实上,我需要这样的映射
[“id1”:“tagname1”,“id2”:“tagname2”…]
。那么,如何在服务器端操作中获取实际标记名而不是id字段,该操作将表单参数持久化?在设置过程中使用tokenValue
参数
e、 g.在jQuery中
<script type="text/javascript">
$(document).ready(function () {
$("#my-text-input").tokenInput("${createLink(controller: 'product', action: 'tags')}",{
theme: "facebook",
allowFreeTagging:"true",
tokenValue:"name"
});
});
</script>
$(文档).ready(函数(){
$(“#我的文本输入”).tokenInput(${createLink(控制器:'product',操作:'tags')}){
主题:“facebook”,
AllowFreetaging:“true”,
标记值:“名称”
});
});
这将提交一个
名称数组
,而不是ID。如果您同时需要name和id,那么最好通过onAdd
参数为每个令牌添加一个自定义属性,然后将其设置为tokenValue
@aksu这通常是jquery的一个问题。如果有人能在php中实现这一点,我相信我可以在grails中应用这一点。我浏览了过去的tokeninput问题,发现php程序员已经回答了大部分问题。params.tags
来自哪里(它是由tokeninput
以某种方式生成的)?你的问题中没有足够的信息来了解发生了什么。我猜您是在用户输入了一些标记之后提交页面的,如果是,那么发送的表单数据是什么(粘贴一个示例)。在tokenInput
示例中,我看不到在用户选择或添加新标记时设置任何表单输入的任何地方,因此我不确定id
如何在params.tags
(在tokenInput
源代码中根本没有对tags
的引用。@nickdos因为tokenInput页面上没有示例,所以我在这里问。表单submsion上的params.tags给出了一个字符串(例如“3,5,7”),其中的数字表示标记的ID。