⚠ In case you've missed it we are migrating 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 forum is read-only and soon will be archived. ⚠


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

Dependent dropdown categories ajax dropdown Categories and sub-categories

  • Please log in to reply
347 replies to this topic

#1 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts

Posted 16 November 2012 - 03: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 Attached File  Gc_dependent_select.php   11.36K   3759 downloads

__________________________________________

v 1.2.0 Attached File  gc_dependent_select.php   9.14K   5784 downloads
_______________________________________
v 1.1.1 Attached File  gc_dependent_select.php   6.64K   1760 downloads
_______________________________________
v 1.1.0 Attached File  gc_dependent_select.php   6.34K   1801 downloads
The library v.1.1.0 has a bug!
http://www.grocerycr...t__20#entry4344
_______________________________________

How to use (v. 1.2.0.):
cc.jpg
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:
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 http://www.grocerycr..._DIR#/smile.png .
P.S.S. Code can contain some bugs . If you find a bug - report, please.
Sorry, my English is poor.


Edited by victor, 29 June 2016 - 05:06 PM.


#2 tlc033

tlc033

    Advanced Member

  • Members
  • PipPipPip
  • 59 posts

Posted 16 November 2012 - 04:07 PM

+1 GC magician

#3 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts

Posted 16 November 2012 - 04:59 PM

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

#4 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,166 posts

Posted 18 November 2012 - 10:51 AM

Hello victor,

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

#5 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts

Posted 19 November 2012 - 07:29 AM

Thanks! :D

#6 tofayelahmed

tofayelahmed

    Advanced Member

  • Members
  • PipPipPip
  • 63 posts

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:

A Database Error Occurred

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

#7 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts

Posted 19 November 2012 - 11:25 AM

Hi! Do you have this error without my library?

#8 tofayelahmed

tofayelahmed

    Advanced Member

  • Members
  • PipPipPip
  • 63 posts

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"


#9 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts

Posted 19 November 2012 - 12:19 PM

show please your real url to controller

#10 tofayelahmed

tofayelahmed

    Advanced Member

  • Members
  • PipPipPip
  • 63 posts

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"



#11 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts

Posted 19 November 2012 - 01:04 PM

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

#12 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts

Posted 19 November 2012 - 01:06 PM

what result do you have now?

#13 kenshicu

kenshicu

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 19 November 2012 - 04: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 "grocery CRUD Hero"

#14 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts

Posted 19 November 2012 - 05:52 PM

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

#15 kenshicu

kenshicu

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 19 November 2012 - 10: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.

#16 tofayelahmed

tofayelahmed

    Advanced Member

  • Members
  • PipPipPip
  • 63 posts

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?

#17 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts

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"

#18 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts

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');

#19 KaBaDaBrA

KaBaDaBrA

    grocery CRUD magician

  • Members
  • PipPipPip
  • 57 posts

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

#20 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts

Posted 20 November 2012 - 08:01 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');


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





Also tagged with one or more of these keywords: Dependent dropdown, categories, ajax, dropdown, Categories and sub-categories

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users