1

我使用 Symfony 1.4。我有三个相互关联的表,如下所示。

表格1:

Conflictos1:
  connection: doctrine
  tableName: conflictos_1
  columns:
    id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: true

    id_sector_actividad:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false

    id_subsector_actividad:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false

  relations:

    SectorActividadCiuTa7:
      local: id_sector_actividad
      foreign: id
      type: one
    SubsectorActividadTa8:
      local: id_subsector_actividad
      foreign: id
      type: one

表 2 和表 3:

SectorActividadCiuTa7:
  connection: doctrine
  tableName: sector_actividad_ciu_ta7
  columns:
    id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: true
    sector_actividad:
      type: string(255)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
  relations:
    Conflictos1:
      local: id
      foreign: id_sector_actividad
      type: many
    SubsectorActividadTa8:
      local: id
      foreign: id_sector
      type: many
SubsectorActividadTa8:
  connection: doctrine
  tableName: subsector_actividad_ta8
  columns:
    id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: true
    id_sector:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
    descripcion:
      type: string(255)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
  relations:
    SectorActividadCiuTa7:
      local: id_sector
      foreign: id
      type: one
    Conflictos1:
      local: id
      foreign: id_subsector_actividad
      type: many

我用 Symfony 生成了一个名为“conflictos”的模块。在这个模块中,我有一个部分 _form.php。这个部分包含一个 AJAX 函数。这里的代码:

<?php use_stylesheets_for_form($form) ?>
<?php use_javascripts_for_form($form) ?>
<?php use_helper('Date') ?>

<!--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>
<form action="<?php echo url_for('conflictos/'.($form->getObject()->isNew() ? 'create' : 'update').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : '')) ?>" method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
    <?php if (!$form->getObject()->isNew()): ?>
        <input type="hidden" name="sf_method" value="put" />
    <?php endif; ?>
    <table border="0" width="96%">
        <tfoot>
            <tr>
                <td align="center" colspan="4">
                    <?php echo $form->renderHiddenFields(false) ?>
                    &nbsp;<a href="<?php echo url_for('conflictos/index') ?>"><button class="btn btn-success" type="button">Listado</button></a>
                    <?php if (!$form->getObject()->isNew()): ?>
                        &nbsp;<?php echo link_to('<button class="btn btn-danger" type="button">Borrar</button>', 'conflictos/delete?id='.$form->getObject()->getId(), array('method' => 'delete', 'confirm' => '¿Está seguro?')) ?>
                    <?php endif; ?>
                    <!--<input type="submit" value="Save" />-->
                    <button class="btn btn-primary" type="submit">Grabar</button>
                </td>
            </tr>
        </tfoot>
        <tbody>
            <?php echo $form->renderGlobalErrors() ?>
            <tr>
                <td  width="24%"><?php echo "<b>Sector Actividad <font color='red'> (*)</b></font><br>"  ?>
                    <?php echo $form['id_sector_actividad']->renderError() ?>
                    <?php echo $form['id_sector_actividad'] ?>
                </td>
                <td  width="24%"><?php echo "<b>Subsector Actividad</b><br> "  ?>
                    <?php echo $form['id_subsector_actividad']->renderError() ?>
                    <select name="conflictos1[id_subsector_actividad]" id="conflictos1_id_subsector_actividad">
                        <option value="" selected="selected">Seleccione sub-sector</option>
                </td>
        </tbody>
    </table>
</form>

AJAX 功能在我添加新记录时工作正常,但是当我要编辑记录时,与 select id_subsector_actividad 对应的字段显示为空。

我的问题是:当我使用 Symfony 调用函数 executeEdit 时,我应该在 AJAX 函数中进行哪些更改以显示字段值“id_subsector_actividad”?

4

1 回答 1

0

我解决了这个问题。该函数应如下所示:

<script type="text/javascript">
$(document).ready(function()
{
    var sector = document.getElementById("conflictos1_id_sector_actividad");

    var id_sub = sector.options[sector.selectedIndex].value;

    {
      $.ajax
      ({
         type: "POST",
         url: '<?php echo url_for('conflictos/subsector'); ?>'+ '?id=' + id_sub,
         cache: false,
                 data: "id_sub="+ id_sub,

success: function(data)
                    {
//                          alert(data);// its show my returned value

                         $("#conflictos1_id_subsector_actividad").html(data);// but it does not select the value of dropdown list.
                   }  
      });
     }

  $("#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>

现在在加载文档时执行相同的功能。现在我在这里有了更好的答案:Symfony 1.4: How I can retrieve selected value with AJAX's function in selectdependent?

于 2013-10-09T23:05:26.683 回答