如何在表单Symfony2中添加自动填充字段? [英] How to add an autocomplete field in forms Symfony2?

查看:225
本文介绍了如何在表单Symfony2中添加自动填充字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我可以在编辑任务中将任务分配给用户。编辑任务时,我会在系统中显示所有用户的下拉列表。现在,我将能够为用户显示带有自动完成功能的文本输入,并且能够在用户不存在时添加用户。



如何做到这一点? 您需要做两件事: / p>

1)创建一个数据转换器

这样可以让您使用输入字段而不是默认的下拉菜单链接的实体。
$ b

Data Transformer基本上将您的输入(在您的情况下是一个用户名字符串)转换为您可以与您的实体一起存储的实际用户对象。它可以让你定义一个输入字段,而不是链接实体的默认下拉菜单。查看文档,这非常简单。



2)创建Ajax自动完成



这可以在客户端使用任何需要的库(jQuery,Prototype等)来完成,服务器端在Controller动作中处理此事件,并使用JSON或渲染模板进行回复。那里有很多教程。如果没有找到,Ajax结果可能包含创建新用户的链接。



数据转换器是您需要关注的内容,我会得到这个工作之后在UI上工作。


Actually, I can assign a task to a user in the edition task. I display a dropdown list of all user in the system when I edit a task. Now, I would to be able to display a text input with autocomplete for user, and be able to add user if is not exist.

How to do that?

Thanks in advance.

解决方案

Two things you need to do:

1) Create a Data Transformer

hich will let you use an input field instead of the default dropdown to the linked entity.

The Data Transformer basically transforms your input (in your case a username string) to actual user object that you can store with your entity. It will let you define an input field instead of the default dropdown to the linked entity. Have a look at the documentation, it's pretty straight forward.

2) Create the Ajax autocomplete

This can be done using any library you want (jQuery, Prototype etc.) on the client side, and server side you handle this in your Controller action and replies with either JSON or a rendered template. There are plenty of tutorials for this out there. The Ajax result could include a link to create a new user, if none is found.

The Data Transformer is what you need to focus on, and I would get that working before working on the UI.

这篇关于如何在表单Symfony2中添加自动填充字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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