⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

[updated 24/11/2012] Dependent dropdown (library)



victor
  • profile picture
  • Member

Posted 16 November 2012 - 15:35 PM

Hi all! This is my second library.
If you have a few dependent dropdowns on any page this library for you.
When you made selection in a "Parent" dropdown the library refreshes a "child" dropdown.

Advantage of this library:

  • integration is easily
  • it is not required to change a template
  • it is not required to create a callback function

The library works perfectly with GC v 1.5.x and CI 3.x
_______________________________________

Download here:

v 1.5.1 [attachment=1127:Gc_dependent_select.php]

__________________________________________

v 1.2.0 [attachment=378:gc_dependent_select.php]
_______________________________________
v 1.1.1 [attachment=364:gc_dependent_select.php]
_______________________________________
v 1.1.0 [attachment=360:gc_dependent_select.php]
The library v.1.1.0 has a bug!
http://www.grocerycr...t__20#entry4344
_______________________________________

How to use (v. 1.2.0.):
[attachment=359:cc.jpg]
[attachment=358:c.jpg]
 


$crud = new grocery_CRUD();
$crud->set_table('dd_goods');
$crud->set_relation('goods_country', 'dd_country', 'country_title');
$crud->set_relation('goods_state', 'dd_state', 'state_title');
$crud->set_relation('goods_city', 'dd_city', 'city_title');

$this->load->library('gc_dependent_select');
// settings

$fields = array(

// first field:
'goods_country' => array( // first dropdown name
'table_name' => 'dd_country', // table of country
'title' => 'country_title', // country title
'relate' => null // the first dropdown hasn't a relation
),
// second field
'goods_state' => array( // second dropdown name
'table_name' => 'dd_state', // table of state
'title' => 'state_title', // state title
'id_field' => 'state_id', // table of state: primary key
'relate' => 'country_ids', // table of state:
'data-placeholder' => 'select state' //dropdown's data-placeholder:

),
// third field. same settings
'goods_city' => array(
'table_name' => 'dd_city',
'where' =>"post_code>'167'",  // string. It's an optional parameter.
'order_by'=>"state_title DESC",  // string. It's an optional parameter.
'title' => 'id: {city_id} / city : {city_title}',  // now you can use this format )))
'id_field' => 'city_id',
'relate' => 'state_ids',
'data-placeholder' => 'select city'
)
);

$config = array(
'main_table' => 'dd_goods',
'main_table_primary' => 'goods_id',
"url" => base_url() . __CLASS__ . '/' . __FUNCTION__ . '/', path to method
'ajax_loader' => base_url() . 'ajax-loader.gif' // path to ajax-loader image. It's an optional parameter
'segment_name' =>'Your_segment_name' // It's an optional parameter. by default "get_items"
);
$categories = new gc_dependent_select($crud, $fields, $config);

// first method:
//$output = $categories->render();

// the second method:
$js = $categories->get_js();
$output = $crud->render();
$output->output.= $js;
$this->_example_output($output);

Note:

You can't use the callback_edit_field and callback_add_field for this field. You can use callback_edit_field and callback_add_field only for the first field

example 2:
[attachment=379:schema.jpg]


function test()
{
$crud = new grocery_CRUD();
$crud->set_table('zone_allocate_subzone');
$crud->set_relation('category_id', 'category', 'category_name');
$crud->set_relation('subcategory_id', 'sub_category', 'subcategory_name');
$crud->set_relation('zone_id', 'zone', 'zone_name');
$crud->set_relation('subzone_id', 'sub_zone', 'subzone_name');
$this->load->library('gc_dependent_select');

$fields = array(
'category_id' => array(// first dropdown name
'table_name' => 'category', // table of country
'title' => 'country_title', // country title
'relate' => null // the first dropdown hasn't a relation
),
'subcategory_id' => array(// second dropdown name
'table_name' => 'sub_category', // table of state
'title' => 'subcategory_name', // state title
'id_field' => 'id', // table of state: primary key
'relate' => 'category_id', // table of state:
'data-placeholder' => 'Select Subcategory' //dropdown's data-placeholder:
)
);
$config = array(
'main_table' => 'dd_goods',
'main_table_primary' => 'goods_id',
"url" => base_url() . __CLASS__ . '/' . __FUNCTION__ . '/',
);
$categories = new gc_dependent_select($crud, $fields, $config);
$js = $categories->get_js();

$fields2 = array(
'zone_id' => array(// first dropdown name
'table_name' => 'zone', // table of country
'title' => 'country_title', // country title
'relate' => null // the first dropdown hasn't a relation
),
'subzone_id' => array(// second dropdown name
'table_name' => 'sub_zone', // table of state
'title' => 'subzone_name', // state title
'id_field' => 'id', // table of state: primary key
'relate' => 'zone_id', // table of state:
'data-placeholder' => 'Select Subzone' //dropdown's data-placeholder:
)
);
// change the "segment_name"!!!
$config2 = array(
'main_table' => 'dd_goods',
'main_table_primary' => 'goods_id',
"url" => base_url() . __CLASS__ . '/' . __FUNCTION__ . '/',
'segment_name' => "other_name"
);
$categories2 = new gc_dependent_select($crud, $fields2, $config2);
$js2 = $categories2->get_js();

$output = $crud->render();
$output->output.= $js . $js2;

$this->_example_output($output);
}

---------------------------------------------------
P.S. Thanks to web-johnny, thanks to KaBaDaBrA and to me /public/style_emoticons/#EMO_DIR#/smile.png .
P.S.S. Code can contain some bugs . If you find a bug - report, please.
Sorry, my English is poor.


tlc033
  • profile picture
  • Member

Posted 16 November 2012 - 16:07 PM

+1 GC magician

victor
  • profile picture
  • Member

Posted 16 November 2012 - 16:59 PM

Write about your tests, please.
I anywhere didn't use this library yet.

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 18 November 2012 - 10:51 AM

Hello [member='victor'],

your library seems really cool. By the way I changed your Member title to "grocery CRUD Hero" . I hope you like it :D

victor
  • profile picture
  • Member

Posted 19 November 2012 - 07:29 AM

Thanks! :D

tofayelahmed
  • profile picture
  • Member

Posted 19 November 2012 - 10:46 AM

Hi victor!
Its create an error.
Pls check my code.


function zonal_stock_received()
{
$crud = new grocery_CRUD();
$this->grocery_crud->set_table('zone_received_stock');
$this->grocery_crud->required_fields("season_id","category_id","subcategory_id","zone_id","seed_type_id","allocate","entry_date");
$this->grocery_crud->set_relation('season_id','season_set','season_name');
$this->grocery_crud->set_relation('category_id','category','category_name');
$this->grocery_crud->set_relation('subcategory_id','sub_category','subcategory_name');
$this->grocery_crud->set_relation('zone_id','zone','zone_name');
$this->grocery_crud->set_relation('seed_type_id','seed_type','seed_type_name');
$this->grocery_crud->display_as('season_id','সালের নাম')->display_as('category_id','ক্যাটাগরির নাম')->display_as('subcategory_id','সাবক্যাটাগরির নাম')->display_as('zone_id','জোনের নাম')->display_as('seed_type_id','বীজের প্রকৃতি')->display_as('allocate','বরাদ্দ')->display_as('entry_date','এন্ট্রি তারিখ');
$this->load->library('gc_dependent_select');

$fields = array(

// first field:
'category_id' => array( // first dropdown name
'table_name' => 'category', // table of country
'title' => 'country_title', // country title
'relate' => null // the first dropdown hasn't a relation
),
// second field
'subcategory_id' => array( // second dropdown name
'table_name' => 'sub_category', // table of state
'title' => 'state_title', // state title
'id_field' => 'id', // table of state: primary key
'relate' => 'category_id ', // table of state:
'data-placeholder' => 'select state' //dropdown's data-placeholder:

)
);

$config = array(
'main_table' => 'zone_received_stock',
'main_table_primary' => 'id',
"url" => base_url() . __CLASS__ . '/' . __FUNCTION__ . '/',
'ajax_loader' => base_url() . 'ajax-loader.gif' // path to ajax-loader image. It's an optional parameter
);
$categories = new gc_dependent_select($crud, $fields, $config);
$js = $categories->get_js();
$output = $crud->render();
$output->output.= $js;
$this->badc_output($output);
}



category table: field--- 1.id, 2. category_name

Subcategory table: field--1.id, 2. category_id 3.subcategory_name

Error:

[b] A Database Error Occurred[/b]

Error Number: 1103
Incorrect table name ''
SHOW COLUMNS FROM ``
Filename: H:\project\xampp\htdocs\sds\system\database\DB_driver.php
Line Number: 330

victor
  • profile picture
  • Member

Posted 19 November 2012 - 11:25 AM

Hi! Do you have this error without my library?

tofayelahmed
  • profile picture
  • Member

Posted 19 November 2012 - 11:53 AM

Thanx victor.I fix this.
but another error in firebug:

"NetworkError: 404 Not Found - http://localhost/sds/Zonal_office/zonal_stock_received/get_items/subcategory_id/2"

victor
  • profile picture
  • Member

Posted 19 November 2012 - 12:19 PM

show please your real url to controller

tofayelahmed
  • profile picture
  • Member

Posted 19 November 2012 - 12:29 PM


controller url : http://localhost/sds/index.php/zonal_office/zonal_stock_received

"NetworkError: 404 Not Found - http://localhost/sds/Zonal_office/zonal_stock_received/get_items/subcategory_id/2"

victor
  • profile picture
  • Member

Posted 19 November 2012 - 13:04 PM

'url'=>base_url().'index.php/'.__CLASS__.'/'.__METHOD__.'/'

victor
  • profile picture
  • Member

Posted 19 November 2012 - 13:06 PM

what result do you have now?

kenshicu
  • profile picture
  • Member

Posted 19 November 2012 - 16:30 PM

Hi Victor, I tried yesterday's KaBaDaBrA example and worked perfectly, now with your library, it is much better, try it now and tell you.

What I did not like about the code of KaBaDaBrA is that it hides Dependent dropdown, I would prefer to leave them DISABLED, as an aesthetic detail.

regards and congratulations to the [background=rgb(235, 239, 249)]"grocery CRUD Hero"[/background]

victor
  • profile picture
  • Member

Posted 19 November 2012 - 17:52 PM

Hi kenshicu and welcome to the forum.
I'll try to make this functionality in the next version of the library

kenshicu
  • profile picture
  • Member

Posted 19 November 2012 - 22:12 PM

well, I'll be waiting ...

tu libreria soporta 3 o mas Dependent dropdown?, funciona con uno solo?
disculpa la pregunta es que no he tenido tiempo de revisar tu libreria.

tofayelahmed
  • profile picture
  • Member

Posted 20 November 2012 - 04:27 AM

Hi victor! You are right and thanx for your continious help.

The previous problem is solved by your instructions.
But a error:


<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Notice</p>
<p>Message: Undefined index: sub_category_title</p>
<p>Filename: libraries/gc_dependent_select.php</p>
<p>Line Number: 199</p> </div>[{"value":"3","property":null}]



Pls describe why we need to use title?

victor
  • profile picture
  • Member

Posted 20 November 2012 - 07:00 AM

The title is field name.
for example:
table "subcategories":

subcategory_id | subcategory_name | parent_category_id

in this case:
"title"=>"subcategory_name"

davidoster
  • profile picture
  • Member

Posted 20 November 2012 - 07:31 AM

Victor, does this library work if a relation is declared like this , e.g.

$this->grocery_crud->set_relation('activities_id','activities','{year} {description}',null,'activities.id ASC');

KaBaDaBrA
  • profile picture
  • Member

Posted 20 November 2012 - 07:36 AM

Really really awesome Victor - been a bit dormant the last few months, just saw this now!! EPIC!!!!

:ph34r: GCRUD Ninja!!!! :D

victor
  • profile picture
  • Member

Posted 20 November 2012 - 08:01 AM

[quote name='David Olive Tree Oster' timestamp='1353396702' post='4336']
Victor, does this library work if a relation is declared like this , e.g.

$this->grocery_crud->set_relation('activities_id','activities','{year} {description}',null,'activities.id ASC');
[/quote]

for difficult cases use KaBaDaBrA's code.
Maybe in the next version I'll try make this functionality.