Yii 1.1: m-sensorario-dropdown

Dependents dropdown Country/State/City like
11 followers

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:

  • #3: Modal window for adding new items
  • #8: Convert data samples en english
  • #2: Dynamic models

1.1 (Jun 23, 2012)

Bugfixes: - #1 Enable also non "path" style url

Total 8 comments

#13656 report it
Bibi40k at 2013/06/14 08:34am
great work

Hi, what if i have this scenario:

  • vehicle type ( car / motorcycle) 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

#11798 report it
sensorario at 2013/02/04 01:06pm
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.

#11796 report it
beesho at 2013/02/04 12:06pm
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.

#8768 report it
sensorario at 2012/06/25 10:47am
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.

#8762 report it
Maxxer at 2012/06/25 08:04am
@sensoorario

I mean the models not to be hardcoded, but as widget options

#8761 report it
sensorario at 2012/06/25 07:59am
dynamic dropwdown?

@maxxer: Do you mean, a modal window to insert new values?

#8756 report it
Maxxer at 2012/06/25 02:23am
dynamic

yeah, it's a great idea, thanks! do you plan to make the dropdowns dynamic?

#8751 report it
paskuale at 2012/06/24 09:51am
great works ;)

thanks sensorario !! Great widget

Leave a comment

Please to leave your comment.

Create extension