Jump to content


Simple dependant dropdown extension

dropdown select dependancy relation date filetring ajax

  • Please log in to reply
85 replies to this topic

#1 ricardomduarte

ricardomduarte

    Member

  • Members
  • PipPip
  • 11 posts

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 Attached File  ajax_grocery_crud.php   6.76K   2821 downloads 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!



#2 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts
  • LocationMinsk

Posted 10 April 2013 - 01:26 PM

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



#3 garaa

garaa

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 10 April 2013 - 07:47 PM

awesome :)



#4 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

Posted 10 April 2013 - 10:18 PM

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


____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#5 garaa

garaa

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 11 April 2013 - 05: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

error.png

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



#6 ricardomduarte

ricardomduarte

    Member

  • Members
  • PipPip
  • 11 posts

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?



#7 ricardomduarte

ricardomduarte

    Member

  • Members
  • PipPip
  • 11 posts

Posted 12 April 2013 - 02: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.

 

Attached Files



#8 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts
  • LocationMinsk

Posted 12 April 2013 - 08:00 PM

Make support of multiple dependents sections like this: 

schema.jpg .


#9 ricardomduarte

ricardomduarte

    Member

  • Members
  • PipPip
  • 11 posts

Posted 15 April 2013 - 02: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');
 

 



#10 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts
  • LocationMinsk

Posted 15 April 2013 - 02: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.



#11 ricardomduarte

ricardomduarte

    Member

  • Members
  • PipPip
  • 11 posts

Posted 15 April 2013 - 04:57 PM

I made some little changes to support that correctly.

 

Here it goes:

 

Attached File  ajax_grocery_crud.php   6.76K   1523 downloads

 

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

 



#12 kenshicu

kenshicu

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts
  • LocationHolguín, Cuba

Posted 18 April 2013 - 07: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



#13 ricardomduarte

ricardomduarte

    Member

  • Members
  • PipPip
  • 11 posts

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?



#14 kenshicu

kenshicu

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts
  • LocationHolguín, Cuba

Posted 19 April 2013 - 01:51 PM

yes, very well, thank you.


your library supports this case: ?

http://www.grocerycrud.com/forums/topic/1489-how-to-add-two-more-fields-when-i-edit-a-record-and-update-another-table-also/#entry6359



#15 ricardomduarte

ricardomduarte

    Member

  • Members
  • PipPip
  • 11 posts

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.



#16 vladbutterfly

vladbutterfly

    Member

  • Members
  • PipPip
  • 16 posts

Posted 28 April 2013 - 09: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



#17 sapophp

sapophp

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 07 May 2013 - 04: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 !

 



#18 ricardomduarte

ricardomduarte

    Member

  • Members
  • PipPip
  • 11 posts

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


#19 ricardomduarte

ricardomduarte

    Member

  • Members
  • PipPip
  • 11 posts

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!



#20 Assem Abdul-Mahmood

Assem Abdul-Mahmood

    Member

  • Members
  • PipPip
  • 20 posts

Posted 09 August 2013 - 11: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







Also tagged with one or more of these keywords: dropdown, select, dependancy, relation, date filetring, ajax

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users