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 =).
$this->widget('MSensorarioDropdown.extensions.ESensorarioDropdown', array( 'configOption' => 'default' ));
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
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);
Download archive and put it on /protected/modules folder. If not exists, create it.
And add it into your module list
'modules' => array(
...
'MSensorarioDropdown',
...
),
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>';
Bugfixes:
Enhancement:
Bugfixes: - #1 Enable also non "path" style url
Total 7 comments
This widget can be edited by anyone. You can forkit and make a pull request or just open a new issue on github.
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.
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.
I mean the models not to be hardcoded, but as widget options
@maxxer: Do you mean, a modal window to insert new values?
yeah, it's a great idea, thanks! do you plan to make the dropdowns dynamic?
thanks sensorario !! Great widget
Leave a comment
Please login to leave your comment.