This module solve "country/state/city" problem. If you have similar problem like "group/user/account" or "project/task/activity" ... and need a unique solution, here you have that solution =).
Requirements ¶
- Yii 1.1 or above)
Usage ¶
$this->widget('MSensorarioDropdown.extensions.ESensorarioDropdown', array(
'configOption' => 'default'
));
Install with git ¶
Move in your /protected/modules folder. If not exists, create it. And now clone the repository on your project:
$ git clone git@github.com:sensorario/MSensorarioDropdown
Sample schema and data ¶
CREATE TABLE IF NOT EXISTS `city` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`state_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `state_id` (`state_id`)
);
CREATE TABLE IF NOT EXISTS `state` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`country_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `country_id` (`country_id`)
);
CREATE TABLE IF NOT EXISTS `country` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
);
ALTER TABLE `city`
ADD CONSTRAINT `city_ibfk_1`
FOREIGN KEY (`state_id`)
REFERENCES `state` (`id`);
ALTER TABLE `state`
ADD CONSTRAINT `state_ibfk_1`
FOREIGN KEY (`country_id`)
REFERENCES `country` (`id`);
INSERT INTO `country` (`id`, `name`) VALUES
(1, 'Italy'),
(2, 'Spain'),
(3, 'Germany'),
(4, 'England'),
(5, 'Swiss'),
(6, 'Norway');
INSERT INTO `state` (`id`, `name`, `country_id`) VALUES
(1, 'Emilia-Romagna', 1),
(2, 'Molise', 1),
(3, 'Lazio', 1);
INSERT INTO `city` (`id`, `name`, `state_id`) VALUES
(1, 'Cesena', 1),
(2, 'Bologna', 1),
(3, 'Parma', 1);
Normal Install ¶
Download archive and put it on /protected/modules folder. If not exists, create it.
And add it into your module list
'modules' => array(
...
'MSensorarioDropdown',
...
),
Configure ¶
To configure this module, you need to update this config file:
/protected/modules/MSensorarioDropdown/config/maing.php
here the content:
<?php
return array(
'Country' => array(
'id' => 'stati',
'name' => 'Stati',
'model' => 'Stato::getStati();',
),
'State' => array(
'id' => 'regioni',
'name' => 'Regioni',
'model' => 'Regione::getRegione($id);',
'message' => 'Questa regione non ha comuni',
),
'City' => array(
'id' => 'comuni',
'name' => 'Comuni',
'model' => 'Comune::getComune($id);',
'message' => 'Questa regione non ha comuni',
),
);
The widget use these vars generating this html. The most important thing to change is model and message. Message will appear when there are no city once selected a state. Or there are no state once selected a country*.
<div class="box">
<span id="' . $this->config['Country']['id'] . '"></span>
<span id="' . $this->config['State']['id'] . '"></span>
<span id="' . $this->config['City']['id'] . '"></span>
</div>';
Resources ¶
1.2 (Jun 27, 2012) ¶
Bugfixes:
7: Label configurable in config file ¶
5: Changing Country, city is not resetted ¶
11: Fixed README.md ¶
Enhancement:
1.1 (Jun 23, 2012) ¶
Bugfixes:
1 Enable also non "path" style url ¶
great works ;)
thanks sensorario !! Great widget
dynamic
yeah, it's a great idea, thanks!
do you plan to make the dropdowns dynamic?
dynamic dropwdown?
@maxxer: Do you mean, a modal window to insert new values?
@sensoorario
I mean the models not to be hardcoded, but as widget options
Milestone 1.2
I've already implemented your feature (if you download from github this module you'll found it). But I want to add some others stuff. Version 1.2 (next milestone) will improve this module. For example I want to add a modal view to add items. And fix some minor bug i have found here.
Retrieve Sub-categories Through Ajax?
Hello,
Thank you for this nice widget.
I have a relatively high number of cities, which is leading the widget to break on loading..
Can this widget be edited so that it retrieves sub-lists from DB using Ajax? This way only the needed sublists are retrieved as requested.
This widget is open source
This widget can be edited by anyone. You can forkit and make a pull request or just open a new issue on github.
great work
Hi,
what if i have this scenario:
and if i choose car i should get for the next dropdowns car make, car model
and if i choose motorcycle i should get for the next dropdowns motorcycle make, motorcycle model
how do i do it in this case ?
Thanks
Congratulation! This istructions are completed and fully Works!
This is the only one completed dropdown example that I have found!
Thera are istruction to how configure it,
the istruction for generate table in DB,
but the most important thing is that at the end this really Work with no problem!!
Thank you very mutch!
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.