Question

In my database I have two related fields. The second field depends on the value selected in the first. The relations are:

enter image description here

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>

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>

This is what I WANT to see:

<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>

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(); 
    } 

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?

Was it helpful?

Solution 2

Here the solution I encountered: I've separated the problem in two.

Case 1: New Record

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

Case 2: Edit an existing record (here was my problem)

I added for 'id_subsector_actividad' in the widget the property 'table_method'=>'Subsector' associated with the function public function Subsector() (see below code).

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; ?>  

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);


  }

I use $elsector in the following function created in 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();  
    }
    }

Thus, Symfony displayed on the form, the value of field named IdSubsectorActividad, not previously exhibited.

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.

Now everything works fine!.

OTHER TIPS

Maybe with something like:

$("option[value='37']").attr('selected', 'selected');

You have a similar question on: Set selected option of select box

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top