⚠ 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

Simple dependant dropdown extension



ricardomduarte

ricardomduarte
  • profile picture
  • Member

Posted 10 April 2013 - 10:34 AM

Hi all,

 

First of all thanks the GC guys for this magnific piece of software. I love it's simplicity and neatness.

 

I'd like to share with you a short extension I built to allow dependant dropboxes to be declared easily and in the same fashion that we declare the grocery CRUD model:

 

Dependant drop downs:

 

Say we have an address table that has both country and state fields and we want to enforce that the state dropdown only shows states from the selected country.

 

Using the ajax_grocery_CRUD.php library (copy the attached file [attachment=511:ajax_grocery_crud.php] to the application\libraries folder), we would be writting the following php code:

 

      
       
function addresses_management()
{
            $this->load->library('grocery_CRUD');
            $this->load->library('ajax_grocery_CRUD');

//create ajax_grocery_CRUD instead of grocery_CRUD. This extends the functionality with the set_relation_dependency method keeping all original functionality as well
            $crud = new ajax_grocery_CRUD();

//this is the default grocery CRUD model declaration
            $crud->set_table('address');
            $crud->set_relation('ad_country_id','country','c_name');
            $crud->set_relation('ad_state_id','state','s_name');

//this is the specific line that specifies the relation.
// 'ad_state_id' is the field (drop down) that depends on the field 'ad_country_id' (also drop down).
// 's_country_id' is the foreign key field on the state table that specifies state's country
            $crud->set_relation_dependency('ad_state_id','ad_country_id','s_country_id');

            $output = $crud->render();
            $this->_example_output($output);
}

 

 

The SQL table definition for this example would be:

 

CREATE TABLE country ( 
     c_id INT PRIMARY KEY,
     c_name VARCHAR(50)
);

CREATE TABLE state ( 
     s_id INT PRIMARY KEY,
     s_name VARCHAR(50),
     s_country_id INT,
     FOREIGN KEY (`s_country_id`) REFERENCES `country` (`c_id`)
);

CREATE TABLE address ( 
     ad_id INT PRIMARY KEY,
     ad_country_id INT,
     ad_state_id INT,
     FOREIGN KEY (`ad_country_id`) REFERENCES `country` (`c_id`),
     FOREIGN KEY (`ad_state_id`) REFERENCES `state` (`s_id`)
);

 

 

 

Dependant date filtering:

 

The source filed that provides the value to filter the dependent field can also be a date. I use it for of an agenda where I have a large set of 'performances' (musical shows) which I want to select on another table ('highlights') based on an input date. 
 
function highlights_management()
{
	$crud = new ajax_grocery_CRUD();

	$crud->set_table('highlight');
	$crud->columns('date','performance_id');


	$crud->set_relation('performance_id','performance','name');

	$crud->set_relation('image_id','image','name','category_id IN (2,3,4)');

//'eventDate' is the field on the performance table that indicates the date of the performance
// The drop down will list only the performances that happen on the selected date
	$crud->set_relation_dependency('performance_id','date','eventDate');

	$output = $crud->render();

	$this->_example_output($output);

}

 

Table highlights has both date (date) and performance_id (foreign key for performance table) fields.

Table performance table has a date field that is used to filter the performances to show on the dropdown.

 

 

I hope this may be useful for anyone else.

Keep up the good work GC people!


victor

victor
  • profile picture
  • Member

Posted 10 April 2013 - 13:26 PM

Thank for your library )) It's an interesting solution!


garaa

garaa
  • profile picture
  • Member

Posted 10 April 2013 - 19:47 PM

awesome :)


davidoster

davidoster
  • profile picture
  • Member

Posted 10 April 2013 - 22:18 PM

I'll test it and get back to you. Thank you very much for your contribution!


garaa

garaa
  • profile picture
  • Member

Posted 11 April 2013 - 17:40 PM

@icardomduarte i've been testing your library, thats cool and very simple to used :).

but i think this library has a bug. when i use the  set_relation_dependency function, and I tried to upload a photo, an error like the following picture

[attachment=506:error.png]

and image preview not worked . but when i was check in upload folder, the file successfully uploads. 


ricardomduarte

ricardomduarte
  • profile picture
  • Member

Posted 12 April 2013 - 10:46 AM

Hi @garaa,

 

That's strange. I didn't change anything related to file uploads whatsoever, so it could be unrelated. 

I suppose the image field is separate from the relations you are defining.

Does the error happen only when you include the set_relation_dependency and stops when you comment the set_relation_dependency function call?


ricardomduarte

ricardomduarte
  • profile picture
  • Member

Posted 12 April 2013 - 14:24 PM

I was able to reproduce and fix the problem.

Thanks for your feedback.

 

Here's the corrected version in attachment to this post.

 


victor

victor
  • profile picture
  • Member

Posted 12 April 2013 - 20:00 PM

Make support of multiple dependents sections like this: 

[sharedmedia=core:attachments:379].

ricardomduarte

ricardomduarte
  • profile picture
  • Member

Posted 15 April 2013 - 14:14 PM

Multiple dependent dropdowns are supported.

For your example you would be needing something like this:

 

 


                $crud->set_relation('category_id','category','category_name');
                $crud->set_relation('subcategory_id','subcategory','subcategory_name');
                $crud->set_relation_dependency('subcategory_id','category_id','category_id');

                $crud->set_relation('zone_id','zone','zone_name');
                $crud->set_relation('subzone_id','subzone','subzone_name');
                $crud->set_relation_dependency('subzone_id','zone_id','zone_id');
 

 


victor

victor
  • profile picture
  • Member

Posted 15 April 2013 - 14:48 PM

Thank. it's fine :) Your library is very useful!

I want to ask last question (I'm sorry if I bothered you):

does your library supports unlimited levels?

for example:

country->state->city->street->home? I often need to create a similar structure.


ricardomduarte

ricardomduarte
  • profile picture
  • Member

Posted 15 April 2013 - 16:57 PM

I made some little changes to support that correctly.

 

Here it goes:

 

[attachment=510:ajax_grocery_crud.php]

 

For an exeample with place->space->room related fields code will be something like:

 

                $crud->set_relation('country_id','country','name');
                $crud->set_relation('state_id','state','name');
                $crud->set_relation_dependency('state_id','country_id','country_id');
                $crud->set_relation('city_id','city','name');
                $crud->set_relation_dependency('city_id','state_id','state_id');

 


kenshicu

kenshicu
  • profile picture
  • Member

Posted 18 April 2013 - 19:06 PM

I got this error:

Fatal error: Class 'grocery_CRUD' not found in U:\htdocs\application\libraries\ajax_grocery_crud.php on line 3

apparently not found:

class ajax_grocery_CRUD extends grocery_CRUD {

I'm using: GC 1.3.3


ricardomduarte

ricardomduarte
  • profile picture
  • Member

Posted 19 April 2013 - 09:17 AM

You still need to load the grocery crud library before you load ajax_grocery_CRUD like this:

 

 

        $this->load->library('grocery_CRUD');
        $this->load->library('ajax_grocery_CRUD');
 

Does it fix your problem?


kenshicu

kenshicu
  • profile picture
  • Member

Posted 19 April 2013 - 13:51 PM

yes, very well, thank you.


your library supports this case: ?

/topic/1489-how-to-add-two-more-fields-when-i-edit-a-record-and-update-another-table-also/#entry6359


ricardomduarte

ricardomduarte
  • profile picture
  • Member

Posted 22 April 2013 - 09:27 AM

No, I think your case is a bit more complex because you want to work with fields that don't exist on the underlying table.

 

I wonder if you can make it work using callback_field and field_type functions to add the fields that don't exist on your new model... but that would be something much more complicated, I guess.


vladbutterfly

vladbutterfly
  • profile picture
  • Member

Posted 28 April 2013 - 21:08 PM

Hi Recardomduarte

I need to know if your library could help me to display (Crud) for the project fields and also custmer name (which I could display it with the set_relation) and I want to have also the name of the customer group

here the database model

project (id,#customer_id,resource_id),customer(id,#customer_group_id),customer_group(id,#text_id),resource(id,#resource_group)

And thank you for helping me


sapophp

sapophp
  • profile picture
  • Member

Posted 07 May 2013 - 16:22 PM

Hi Ricardo,

Man it's a really great feature, congratulations. I started using it today, really easy and friendly.

Just something that I noticed is that the dependent dropdown doesn't start filtered when editing the record. Using your data example, it starts with all states in DB, and when you choose a different country, then it filter the data and shows just the states related to that country. Not a big deal, I'll try to fix it using an edit_field_callback...

 

Nice job !

 


ricardomduarte

ricardomduarte
  • profile picture
  • Member

Posted 17 May 2013 - 09:46 AM

Hi Recardomduarte

I need to know if your library could help me to display (Crud) for the project fields and also custmer name (which I could display it with the set_relation) and I want to have also the name of the customer group

here the database model

project (id,#customer_id,resource_id),customer(id,#customer_group_id),customer_group(id,#text_id),resource(id,#resource_group)

And thank you for helping me

 

I'm not sure I understand the issue but I think you could use this library to set the dependency between the customer and customer group, but you would also need to add the customer_group_id column to the project table in order to add the customer group dropdown to the form.

 

 

                $crud->set_relation('customer_id','customer','name');
                $crud->set_relation('customer_group_id','customer_group','name');
                $crud->set_relation_dependency('customer_id','customer_group_id','customer_group_id');

ricardomduarte

ricardomduarte
  • profile picture
  • Member

Posted 17 May 2013 - 09:49 AM

Hi Ricardo,

Man it's a really great feature, congratulations. I started using it today, really easy and friendly.

Just something that I noticed is that the dependent dropdown doesn't start filtered when editing the record. Using your data example, it starts with all states in DB, and when you choose a different country, then it filter the data and shows just the states related to that country. Not a big deal, I'll try to fix it using an edit_field_callback...

 

Nice job !

 

Thanx sapophp for your feedback.

I'll try to fix that when I change the lib again, but currently I'm working on a different project and have no time just now.

If you found a solution or workaround, please post!


Assem Abdul-Mahmood

Assem Abdul-Mahmood
  • profile picture
  • Member

Posted 09 August 2013 - 23:58 PM

Thank you so much Mr ricardomduarte for you awesome library.

I tried to use your library with Grocory CURD 1.4.0 but I got this error message

Fatal error: Access level to ajax_grocery_CRUD::$state_code must be protected (as in class Grocery_CRUD) or weaker in C:\wamp\www\cpuwebsite\application\libraries\ajax_grocery_crud.php on line 255

please any idea about it, let me know i will be waiting