Symfony2:实体字段(html SELECT)设置所选项目
我正在使用表单(filterForm)来过滤树枝视图上的实体。
“filterForm”只有一个“entity”类型的字段。在视图上它显示一个 HTML-SELECT-OPTIONs 标签。
当用户更改选择时,将调用相同的控制器来执行必要的操作来过滤实体列表。
一切正常,但我需要显示带有过滤列表的值的 SELECT 字段。这就是问题所在,我不知道该怎么做。
来自index.html.twig的字段代码:
{{ form_widget(personalFilterForm.personaFiltrarMail,
{ 'empty_value': 'Todos',
'attr': {'selected': personaFiltrarMail,
'onChange': 'javascript:document.filtrado.submit()' }
}
)
}}
该代码正在生成此html代码:
<select name="test_onebundle_type[personaFiltrarMail]" id="test_onebundle_type_personaFiltrarMail"
onchange="javascript:document.filtrado.submit()"
required="required" selected="[email protected]">
<option value="">Todos</option>
<option [email protected]">Name One</option>
<option [email protected]">Name Two</option>
<option [email protected]">Name three</option>
这里真正的问题(我认为)是知道如何访问 OPTIONS 子元素以设置 de SELECTED 属性具体的选项。
谢谢。
=== 控制器 ===
这里的“控制器”...
所有四个编号的“回声”给我邮件:“[email protected]' 但 SELECT html 标记始终位于第一个选项标签。
class HorasController extends Controller
{
/**
* Lists all Horas entities.
*
* @Route("/", name="horas")
* @Template()
*/
public function indexAction()
{
$em = $this->getDoctrine()->getEntityManager();
$personas = $em->getRepository('PtGhorgaBundle:Personal')->findAll();
$personalFilterForm = $this->createForm(new PersonalFilterType(), $personas);
$request = $this->getRequest();
$formVars = $request->request->get('pt_ghorgabundle_type');
$personaFiltrarMail = $formVars['personaFiltrarMail'];
//echo "1.- [".$personaFiltrarMail."]<br />";
if (!isset($personaFiltrarMail) || $personaFiltrarMail=="") {
$entities = $em->getRepository('PtGhorgaBundle:Horas')->findAll();
} else {
$criterio = array('persona' => $personaFiltrarMail,);
$entities = $em->getRepository('PtGhorgaBundle:Horas')->findBy($criterio);
$criterio = array('mail' => $personaFiltrarMail,);
$personaFiltrarMail = $em->getRepository('PtGhorgaBundle:Personal')->find($criterio)->getMail();
echo "2.- [".$personaFiltrarMail."]<br />";
$personalFilterForm->personaFiltrarMail = $personaFiltrarMail;
echo "3.- [".$personaFiltrarMail."]<br />";
echo "4.- [".$personalFilterForm->personaFiltrarMail."]<br />";
}
return array('entities' => $entities,
'personas' => $personas,
'personalFilterForm' => $personalFilterForm->createView(),
'personaFiltrarMail' => $personaFiltrarMail,
);
}
I am using a form (filterForm) to filter entities on a twig view.
The 'filterForm' only has a field of 'entity' type. On the view it shows a HTML-SELECT-OPTIONs tag.
When the user changes the selection, the same controller is called doing the neccesary stuff to filter the entities-list.
All is working fine but I need to show the SELECT-field with the value that is filtering the list. And here is the problem, I don't know how to do it.
A bit of code of the field from the index.html.twig:
{{ form_widget(personalFilterForm.personaFiltrarMail,
{ 'empty_value': 'Todos',
'attr': {'selected': personaFiltrarMail,
'onChange': 'javascript:document.filtrado.submit()' }
}
)
}}
That code is generating this html code:
<select name="test_onebundle_type[personaFiltrarMail]" id="test_onebundle_type_personaFiltrarMail"
onchange="javascript:document.filtrado.submit()"
required="required" selected="[email protected]">
<option value="">Todos</option>
<option [email protected]">Name One</option>
<option [email protected]">Name Two</option>
<option [email protected]">Name three</option>
The real problem here (I think) is knowing how can I get access to the OPTIONS sub-element to set de SELECTED attribute on a concrete OPTION item.
Thanks.
=== Controller ===
Here the 'Controller'...
All four numbered 'echoes' gives me the mail: '[email protected]' But the SELECT html TAG is always located on the first one OPTION tag.
class HorasController extends Controller
{
/**
* Lists all Horas entities.
*
* @Route("/", name="horas")
* @Template()
*/
public function indexAction()
{
$em = $this->getDoctrine()->getEntityManager();
$personas = $em->getRepository('PtGhorgaBundle:Personal')->findAll();
$personalFilterForm = $this->createForm(new PersonalFilterType(), $personas);
$request = $this->getRequest();
$formVars = $request->request->get('pt_ghorgabundle_type');
$personaFiltrarMail = $formVars['personaFiltrarMail'];
//echo "1.- [".$personaFiltrarMail."]<br />";
if (!isset($personaFiltrarMail) || $personaFiltrarMail=="") {
$entities = $em->getRepository('PtGhorgaBundle:Horas')->findAll();
} else {
$criterio = array('persona' => $personaFiltrarMail,);
$entities = $em->getRepository('PtGhorgaBundle:Horas')->findBy($criterio);
$criterio = array('mail' => $personaFiltrarMail,);
$personaFiltrarMail = $em->getRepository('PtGhorgaBundle:Personal')->find($criterio)->getMail();
echo "2.- [".$personaFiltrarMail."]<br />";
$personalFilterForm->personaFiltrarMail = $personaFiltrarMail;
echo "3.- [".$personaFiltrarMail."]<br />";
echo "4.- [".$personalFilterForm->personaFiltrarMail."]<br />";
}
return array('entities' => $entities,
'personas' => $personas,
'personalFilterForm' => $personalFilterForm->createView(),
'personaFiltrarMail' => $personaFiltrarMail,
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的数据中,您可以将属性
personaFiltrarMail
设置为相应的值。例如在您的控制器中:
然后渲染您的模板。
In your data you can set the property
personaFiltrarMail
to the according value.For example in your controller :
Then render your template.
我找到了:
就在下面
回显“4.....”行
问候。
I have found it:
just belown
echo "4....." line
Regards.