Symfony 1.4:如何在选择依赖项中使用AJAX函数检索选择的值? [英] Symfony 1.4: How I can retrieve the selected value with AJAX's function in select dependent?
问题描述
在我的数据库中,我有两个相关字段.第二个字段取决于在第一个字段中选择的值.关系是:
In my database I have two related fields. The second field depends on the value selected in the first. The relations are:
我以表"conflictos_1"的形式使用的函数是:
The function I use in the form of table "conflictos_1" is:
<!--Aquí el javascript para select dependientes-->
<script type="text/javascript">
$(document).ready(function()
{
$("#conflictos1_id_sector_actividad").change(function()
{
var id_sub = $(this).val();
if(id_sub != '')
{
$.ajax
({
type: "POST",
url: '<?php echo url_for('conflictos/subsector'); ?>'+ '?id=' + id_sub,
cache: false,
data: "id_sub="+ id_sub,
success: function(data)
{
$("#conflictos1_id_subsector_actividad").html(data); // but it does not select the value of dropdown list.
}
});
}
else
{
$("#conflictos1_id_subsector_actividad").html("<option value=''>-- No se ha seleccionado subsector --</option>");
}
return false;
});
});
</script>
当我添加新记录时,一切正常.
但是,当我编辑一条记录时,select依赖项不会显示"selected"值.
在编辑模式下,当我查看字段"id_subsector_actividad"时,所选值应为例如<option value="37 " selected="selected">
:这是在检查使用AJAX函数创建的元素时在表单上看到的内容:
When I add a new record, everything works fine.
But when I edit a record, the select dependent, does not show "selected" value.
In edit mode, when I look at the field "id_subsector_actividad", the selected value should be, for example, <option value="37 " selected="selected">
: This is what I see on my form when I inspect the element created with AJAX's function:
<select name="conflictos1[id_subsector_actividad]" id="conflictos1_id_subsector_actividad">
<option value="29 ">14.1 Meretrices</option>
<option value="30 ">Preparación de alimentos y comedor</option>
<option value="31 ">Seguridad</option>
<option value="37 ">redes sanitarias</option>
</select>
这是我想看到的:
<select name="conflictos1[id_subsector_actividad]" id="conflictos1_id_subsector_actividad">
<option value="29 ">14.1 Meretrices</option>
<option value="30 ">Preparación de alimentos y comedor</option>
<option value="31 ">Seguridad</option>
<option value="37 " selected="selected">redes sanitarias</option>
</select>
我使用此功能过滤表"Subsector_actividad_ta8"中的记录(我使用Symfony 1.4和Doctrine):
I use this function to filter records in the table "Subsector_actividad_ta8"(I work with Symfony 1.4 and Doctrine):
public function executeSubsector()
{ $id_sub = $_POST['id_sub'];
$this->subsec= Doctrine_Core::getTable('SubsectorActividadTa8') ->createQuery('a')
->where('a.id_sector = ?', $id_sub)
->execute();
}
我的问题是:编辑现有记录时,我应该在AJAX的功能中进行哪些更改,以便在第二个字段中显示选定"值?
My question is: What should I change in the AJAX's function, to display the "selected" value in the second field, when I am editing an existing record?
推荐答案
这是我遇到的解决方案:我将问题分为两部分.
Here the solution I encountered: I've separated the problem in two.
案例1:新记录
我使用上面问题中显示的相同的JQuery函数.具有相关功能,也如上所示.这个案子不是我的问题
I use the same JQuery function shown above in the question. With the associated function, also shown above. This case was not part of my problem
案例2:编辑现有记录(这是我的问题)
我在窗口小部件中为'id_subsector_actividad'
添加了与功能public function Subsector()
关联的属性'table_method'=>'Subsector'
(请参见下面的代码).
I added for 'id_subsector_actividad'
in the widget the property 'table_method'=>'Subsector'
associated with the function public function Subsector()
(see below code).
在conflictos1的部分_form.php中,我为字段id_subsector_actividad
编写了以下代码:
In the partial _form.php of conflictos1, I wrote the following code for the field id_subsector_actividad
:
<?php if (!$form->getObject()->isNew()): ?>
<?php echo $form['id_subsector_actividad'] ?>
<?php endif; ?>
<?php if ($form->getObject()->isNew()): ?>
<select name="conflictos1[id_subsector_actividad]" id="conflictos1_id_subsector_actividad">
<option value="" selected="selected">Seleccione sub-sector</option>
<?php endif; ?>
在conflictos1的action.class.php中,我修改了函数executeEdit,并添加了变量$elsector
:
In the action.class.php in the conflictos1, I modified the function executeEdit, with the addition of the variable $elsector
:
public function executeEdit(sfWebRequest $request)
{
global $elsector;
$this->forward404Unless($conflictos1 = Doctrine_Core::getTable('Conflictos1')->find(array($request->getParameter('id'))), sprintf('Object conflictos1 does not exist (%s).', $request->getParameter('id')));
$elsector= $conflictos1->getIdSectorActividad();
$this->form = new Conflictos1Form($conflictos1);
}
我在SubsectorActividadTa8Table.class.php
public function Subsector()
{
global $elsector;
if (!is_null($elsector)){
$id_sub=$elsector;
$query= Doctrine_Query::create()
->select('a.id')
->from('SubsectorActividadTa8 a')
->innerJoin('a.Conflictos1 c')
->where('a.id_sector = ?', $id_sub);
return $query->execute();
}
}
因此,Symfony显示在窗体上,以前未显示过名为IdSubsectorActividad的字段的值.
Thus, Symfony displayed on the form, the value of field named IdSubsectorActividad, not previously exhibited.
换句话说,现在当我在表conflictos1
中编辑记录时,名为IdSubsectorActividad的字段显示正确的值,而在表单之前则不显示任何值.
In other words, now when I'm editing a record in the table conflictos1
the field named IdSubsectorActividad shows the correct value, whereas before the form does not show any value.
现在一切正常!.
这篇关于Symfony 1.4:如何在选择依赖项中使用AJAX函数检索选择的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!