Monday, October 15, 2007

List, JQuery & CakePHP

In my work I will need a online change droplist.

So. I have list of countries, and of course, every country have a lot of cities. And we don't need to put ALL tons of cities in our html code. What we must do?

I don't work with prototype, I love jquery, and I wrote small code in jquery & cakephp.

First, user select country, then script AJAX-working upload list of cities, and user can select needing city.

How? Look my add.thtml (ctp):

For country select:

selectTag('Realty/country_id', $country_list,null,null,null,true)?>

And city select:


selectTag('Realty/city_id', $city_list,null,null,null,true)?>



Ok?

In top of add.thtml I wrote:

$(document).ready(function() {
$("#attention").hide();
$("#RealtyCountryId").change( function () {
$("#sel_city").html("Please wait...");
$.get("/realties/sel_city", {id: $("#RealtyCountryId").val()}, function (data) { $("#sel_city").html(data)} );
$("#sel_city").show();
});

});

And, of course, in realties_controller I have sel_city function:

function sel_city()
{
$this->layout = null;
$id = intval($this->params['url']['id']);
if (! $id) exit;
$this->City->unbindModel(array('hasMany' => array('Realty'), 'belongsTo' => array('Country')));
$city_list = $this->City->generateList('country_id = ' . $id,'name ASC');
$this->set('city_list',$city_list);
$this->render();
}

Yep! ;-) Any questions? ;-)

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home