En un formulario de alta, tengo 3 campos, "Nombre"," Country" y
"Province", country y province son selects dependientes que los
actualizo con un script Jquery:
//webroot/js/list_provinces.js
$(document).ready(function(){
$('#country').live('change', function() {
alert($(this).val());
if($(this).val().length != 0) {
$.getJSON('obtener_provincias',
{
countryId: $(this).val()
},
function(provinces) {
if(provinces !== null) {
populateProvinceList(provinces);
}
});
}
});
});
function populateProvinceList(provinces) {
var options = '';
$.each(provinces, function(index, province) {
options += '<option value="' + index + '">' + province + '</
option>';
});
$('#provincia').html(options);
$('#provincias').show();
Y esto me funciona a la perfeccion.
El problema viene cuando quiero aplicar los mismos selects
dependientes en la edicion del formulario, he mirado con el firebug y
no me devuelve ningun valor JONSON, no se donde puede estar el error,
Si podeis echar un vistazo al codigo, os dejo el controlador y las
vistas de add y edit Cities
views/cities/add
<div class="cities form">
<?php echo $this->Html->script('list_provinces'); ?>
<?php echo $this->Form->create('City'); ?>
<fieldset>
<legend><?php __('Add City'); ?></legend>
<?php
echo $this->Form->input('nombre');
echo $this->Form->input('country_id', array('options' =>
$countries, 'empty' =>
'-- Seleccione Pais --', 'id' => 'country'));
?>
<div id="provincias" style="display: none;">
<?php
echo $this->Form->input('province_id', array('options' =>
$provinces, 'empty' =>
'-- Seleciones Provincia --', 'id' => 'provincia'));
?>
</div>
<div id="loading"
<img src="../>img/loadin.gif" border="0"/>
</fieldset>
<?php echo $this->Form->end(__('Submit', true)); ?>
</div>
views/cities/edit
<div class="cities form">
<?php echo $this->Html->script('list_provinces'); ?>
<?php echo $this->Form->create('City');?>
<fieldset>
<legend><?php __('Edit City'); ?></legend>
<?php
echo $this->Form->input('id');
echo $this->Form->input('nombre');
echo $this->Form->input('country_id', array('options' =>
$countries, 'empty' =>
'-- Seleccione Pais --', 'id' => 'country'));
?>
<div id="provincias" >
<?php
echo $this->Form->input('province_id', array('options' =>
$provinces, 'empty' =>
'-- Seleciones Provincia --', 'id' => 'provincia'));
?>
</div>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
<div class="actions">
<h3><?php __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('Delete', true),
array('action'
=> 'delete', $this->Form->value('City.id')), null, sprintf(__('Are you
sure you want to delete # %s?', true), $this->Form-
>value('City.id'))); ?></li>
</ul>
</div>
controller/cities_controller
<?php
class CitiesController extends AppController {
var $name = 'Cities';
var $components = array('RequestHandler');
public function obtener_provincias() {
if($this->RequestHandler->isAjax()) {
$this->set('provinces', $this->City->Province->find('list',
array('conditions' =>
array('province.country_id' =>
$this->params['url']['countryId']),
'recursive' => -1)));
pr($this->data);
if(isset($provinces)) {
echo $this->Js->object($provinces);
}
}
}
function index() {
$this->City->recursive = 0;
$this->set('cities', $this->paginate());
}
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid city', true));
$this->redirect(array('action' => 'index'));
}
$this->set('city', $this->City->read(null, $id));
}
function add() {
if (!empty($this->data)) {
$this->City->create();
if ($this->City->save($this->data)) {
//$this->flashSuccess('The city has been
saved','index');
$this->Session->setFlash(__('The city has been
saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The city could not
be saved. Please,
try again.', true));
}
}
$countries = $this->City->Country->find('list');
$provinces = $this->City->Province->find('list');
$this->set(compact('countries', 'provinces'));
}
function edit($id = null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid city', true));
$this->redirect(array('action' => 'index'));
}
if (!empty($this->data)) {
if ($this->City->save($this->data)) {
$this->Session->setFlash(__('The city has been
saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The city could not
be saved. Please,
try again.', true));
}
}
if (empty($this->data)) {
$this->data = $this->City->read(null, $id);
}
// pr($this->data['Province']['id']);
$countries = $this->City->Country->find('list');
$provinces =
$this->City->Province->find('list',array('conditions'
=>
array('province.country_id' =>
($this->data['Country']['id']))));
$this->set(compact('countries', 'provinces'));
}
function delete($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid id for city',
true));
$this->redirect(array('action'=>'index'));
}
if ($this->City->delete($id)) {
$this->Session->setFlash(__('City deleted', true));
$this->redirect(array('action'=>'index'));
}
$this->Session->setFlash(__('City was not deleted', true));
$this->redirect(array('action' => 'index'));
}
}
?>
Gracias por la ayuda que me podais ofrecer
--
Has recibido este mensaje porque estás suscrito al grupo "CakePHP-es" de Grupos
de Google.
Para publicar una entrada en este grupo, envía un correo electrónico a
[email protected].
Para anular tu suscripción a este grupo, envía un correo electrónico a
[email protected]
Para tener acceso a más opciones, visita el grupo en
http://groups.google.com/group/cakephp-es?hl=es.