⚠ 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

categories and subcategories



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

Posted 25 March 2012 - 13:42 PM

Perhaps an easy callback_add_field and callback_edit_field with returning an empty select for the cities will solve the problem. What do you think?

KaBaDaBrA
  • profile picture
  • Member

Posted 25 March 2012 - 15:42 PM

fleep!! Found a solution using the callback_add_field and callback_edit_field as per what web-johnny recommended too. At first I couldn't get the fancy dropdown boxes working but I managed to get it working!! :D

The callbacks:



$crud->callback_add_field('stateID', array($this, 'empty_state_dropdown_select'));
$crud->callback_edit_field('stateID', array($this, 'empty_state_dropdown_select'));
$crud->callback_add_field('cityID', array($this, 'empty_city_dropdown_select'));
$crud->callback_edit_field('cityID', array($this, 'empty_city_dropdown_select'));


The callback functions:



function empty_state_dropdown_select()
{
//CREATE THE EMPTY SELECT STRING
$empty_select = '<select name="stateID" class="chosen-select" data-placeholder="Select State/Province" style="width: 300px; display: none;">';
$empty_select_closed = '</select>';
//GET THE ID OF THE LISTING USING URI
$listingID = $this->uri->segment(4);

//LOAD GCRUD AND GET THE STATE
$crud = new grocery_CRUD();
$state = $crud->getState();

//CHECK FOR A URI VALUE AND MAKE SURE ITS ON THE EDIT STATE
if(isset($listingID) && $state == "edit") {
//GET THE STORED STATE ID
$this->db->select('countryID, stateID')
->from('customers')
->where('customerNumber', $listingID);
$db = $this->db->get();
$row = $db->row(0);
$countryID = $row->countryID;
$stateID = $row->stateID;

//GET THE STATES PER COUNTRY ID
$this->db->select('*')
->from('state')
->where('countryID', $countryID);
$db = $this->db->get();

//APPEND THE OPTION FIELDS WITH VALUES FROM THE STATES PER THE COUNTRY ID
foreach($db->result() as $row):
if($row->state_id == $stateID) {
$empty_select .= '<option value="'.$row->state_id.'" selected="selected">'.$row->state_title.'</option>';
} else {
$empty_select .= '<option value="'.$row->state_id.'">'.$row->state_title.'</option>';
}
endforeach;

//RETURN SELECTION COMBO
return $empty_select.$empty_select_closed;
} else {
//RETURN SELECTION COMBO
return $empty_select.$empty_select_closed;
}
}

function empty_city_dropdown_select()
{
//CREATE THE EMPTY SELECT STRING
$empty_select = '<select name="cityID" class="chosen-select" data-placeholder="Select City/Town" style="width: 300px; display: none;">';
$empty_select_closed = '</select>';
//GET THE ID OF THE LISTING USING URI
$listingID = $this->uri->segment(4);

//LOAD GCRUD AND GET THE STATE
$crud = new grocery_CRUD();
$state = $crud->getState();

//CHECK FOR A URI VALUE AND MAKE SURE ITS ON THE EDIT STATE
if(isset($listingID) && $state == "edit") {
//GET THE STORED STATE ID
$this->db->select('stateID, cityID')
->from('customers')
->where('customerNumber', $listingID);
$db = $this->db->get();
$row = $db->row(0);
$stateID = $row->stateID;
$cityID = $row->cityID;

//GET THE CITIES PER STATE ID
$this->db->select('*')
->from('city')
->where('stateID', $stateID);
$db = $this->db->get();

//APPEND THE OPTION FIELDS WITH VALUES FROM THE STATES PER THE COUNTRY ID
foreach($db->result() as $row):
if($row->city_id == $cityID) {
$empty_select .= '<option value="'.$row->city_id.'" selected="selected">'.$row->city_title.'</option>';
} else {
$empty_select .= '<option value="'.$row->city_id.'">'.$row->city_title.'</option>';
}
endforeach;

//RETURN SELECTION COMBO
return $empty_select.$empty_select_closed;
} else {
//RETURN SELECTION COMBO
return $empty_select.$empty_select_closed;
}
}


If you have a big list like fleep does, the above code with force the two child dropdowns to load as an empty select instead of populating them from the start making the loading process muuuuch faster!!!

fleep
  • profile picture
  • Member

Posted 25 March 2012 - 22:33 PM

Awesome ! You're amazing KaBaDaBrA !
It's working so fast, it looks like it's instant, eventhough there's hundreds of entries !

Thank you so much, you did a great job ;)

You can give it a try and see how cool it is : http://www.veloccasion.net/index.php/examples/customers_management/add

I had another question, can you maybe give me a hint ?
When I finally choose the city, in the 3rd dropdown, I would like to populate a hidden field with the postal code value (this value is in my "city" table)
I don't know if I should use a [i][b]callback_after_insert[/b][/i] and [b][i]callback_after_update[/i][/b] or if I should populate a 4th field using your script and a relation ?

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

Posted 25 March 2012 - 23:56 PM

[member='fleep'] a callback_after_insert and callback_after_update it is a better idea for this.

[member='KaBaDaBrA'] great job :)

KaBaDaBrA
  • profile picture
  • Member

Posted 26 March 2012 - 05:50 AM

[size=4]Pleasure fleep, glad it's all working so great now!!!![/size]

[size=4]And thanks web johnny!!!![/size] [size=4] :D[/size]

Cèsar Martí
  • profile picture
  • Member

Posted 27 March 2012 - 11:44 AM

Thanks KaBaDaBrA and Johnny. My depenent dropdown select is working perfect!! If one day you come to Valencia (in east coast of Spain), I'll invite you a paella (a real one).

Regards, Cèsar.

KaBaDaBrA
  • profile picture
  • Member

Posted 27 March 2012 - 17:30 PM

Gracias [color=#282828][font=helvetica, arial, sans-serif]Cèsar!!! If you're ever in Durban, South Africa, come over for a braai [/font][/color][color=#282828][font=helvetica, arial, sans-serif] :D[/font][/color]

fleep
  • profile picture
  • Member

Posted 29 March 2012 - 22:22 PM

Hey, it's me again :)

I'd like to make every "countries", "regions" and "cities" linkable.
I mean adding for example a "[b][i]<a href="cities/mycity.html">[/i][/b]" on every listed cities

I tried to use the callback_column , using the example you provide. It gives smthg like :


$crud->callback_column('cityID', array($this, 'city_link'));

function city_link($value, $row)
{
return "<a href=cities/".$row->citySlug.">".$value."</a>";
}


Is it a good idea to use callback_column to add a link on every rows of this specified column ? Or is there a better solution ?
Is it a good idea to use a "cityslug" data from the sql table, or can I create a simpler link using the "city title" (formatted with no uppercase, no accent or special caracters)

[b]EDIT [/b]:
I just discovered the add_action control, it's perfect for what I want, but I had prefer that the cities, countries, and states had been directly clickable, instead of having the links on the "actions" column. Can someone helps ?

KaBaDaBrA
  • profile picture
  • Member

Posted 30 March 2012 - 06:36 AM

Hey fleep! Seems like someone has a similar issue on here (no solution yet): /topic/245-relation-n-callback-column/page__hl__callback_column__fromsearch__1

I also tried using the callback_column but doesn't seem to want to work on relation_n fields but works fine on the normal fields.

Johnny any ideas? :)

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

Posted 30 March 2012 - 06:54 AM

[member='fleep'] I think your way is fine. And don't forget that you can always had as columns names as you like WITHOUT having them in your database for example 'test1','test2' or whatever name you like and you can add a callback there by using the second parameter, for example:


function product_scale_callback($value, $row)
{
return "Scale: <b>".$row->productScale."</b>";
}


[member='KaBaDaBrA'], actually there is something that let's say "breaks" the callback_column if you use the set_relation. A temporary solution for this (I didn't know it either :P ) is http://www.grocerycr...p__982#entry982 . Now for the relation_n_n I didn't test it actually so I still don't know when I have time I will have a look for this one.

Hope it helps.

KaBaDaBrA
  • profile picture
  • Member

Posted 30 March 2012 - 07:03 AM

Thanks Johnny and blz4! :D

Tested on relation_n and works great!!! fleep, try this out...


$crud->callback_column($this->unique_field_name('cityID'),array($this,'city_link'));

function city_link($value, $row)
{
return '<a href="cities/'.$row->citySlug.'">'.$value.'</a>';
}
function unique_field_name($field_name) {
return 's'.substr(md5($field_name),0,8); //This s is because is better for a string to begin with a letter and not with a number
}

xxaxxo
  • profile picture
  • Member

Posted 02 April 2012 - 13:08 PM

Is there any easy way to mark a dropdown 'selected' before add - here is the thing... i have 2 related dbs (category, subcategory) - i've added a custom action button in the category crud to add a subcategory.

$crud->add_action('Add subcat', base_url('img/icons/wired.png'), 'crud/subcategory/add');


I'd like the dropdown of category to be selected when the user presses the custom action button.

Thank's

KaBaDaBrA
  • profile picture
  • Member

Posted 03 April 2012 - 08:18 AM

Hey xxaxxo, you got an example online I can see just to get a better idea of how your button etc works?

xxaxxo
  • profile picture
  • Member

Posted 05 April 2012 - 13:30 PM

I'm sorry i dont have an online example yet, but it's a normal add_action
here is the code of the action button

$crud->add_action('add subcategory', base_url('img/icons/16x16/wired.png'), 'crud/subcategory/add');

my main admin controler is named crud
the above code is in the function category
and there is a function named subcategory
(by default when you are adding a thing the link from the groceryCRUD is function_name/add ... so by the link just points you to the add page of the subcategry. The thing i wanted to do is mark the 'relation dropdown' that appears based on the row

Cèsar Martí
  • profile picture
  • Member

Posted 03 May 2012 - 14:26 PM

Hi KaBaDabrA!

I realised a small issue in your code for categories and subcategories. Imagine we have something like States and Cities.

At the beginning the States dorpdown list is full with the options with the default option selected, it's supossed to be something like "Select States". And in the Cities dropdown is empty with, for example, the default selected option "Select cities", waiting you choose something in States. Ok.

Now, if you choose something in States the Cities list will charge the cities of this state. Imagine you choose wathever. In this moment you have an option selected in each and there will be that "X" in the right of the option selected of each dropdown list. Everything is ok at the moment.

If you click the "X" of the "States", the "Cities" dropdown will be empty, but this (the "Cities" dropdown) will keep that "X". Of course, if you click it it won't do anything.

Could you fix it, please?

I hope you will understand.

Thanks again.

jcasanova
  • profile picture
  • Member

Posted 23 May 2012 - 16:39 PM

great work!

jcasanova
  • profile picture
  • Member

Posted 13 June 2012 - 15:17 PM

I still have a problem... I get the first dropdown working good, but when I select, the second field appears but just like an input with no data
json calls are ok, they return data.


EDIT: was a problem with relation_n Thanks!!!

kesing
  • profile picture
  • Member

Posted 24 July 2012 - 09:56 AM

So awesome work KaBaDaBrA!!!

It works perfect!! I was looking for something like that! :D

btw, there is any posibility to let selected the first dependent subcategory??

angga777
  • profile picture
  • Member

Posted 25 July 2012 - 07:37 AM

[color=#282828][font=helvetica, arial, sans-serif]Hi KaBaDabrA..[/font][/color]

Good Work...!!

I had trouble with use set_relation_n_n into your code. may I ask for directions?


$crud->set_subject('Info Pulau');

$crud->set_table('pulau');
$crud->required_fields('nama_pulau','id_prop','id_kab','kecamatan_pulau');
$crud->display_as('id_prop','Propinsi');
$crud->display_as('id_kab','Kabupaten');

$crud->columns('nama_pulau', 'id_prop', 'id_kab' , 'kecamatan_pulau');
$crud->fields('nama_pulau', 'id_prop', 'id_kab' , 'kecamatan_pulau' );

$crud->set_relation('id_prop','t_propinsi','propinsi');
$crud->set_relation('id_kab','t_kabupaten','kabupaten');
$crud->set_relation_n_n('kecamatan_pulau', 'kec_pulau', 't_kecamatan', 'id_pulau','id_kec', 'kecamatan');


Pulau = Island
Propinsi = State
Kabupaten = City
Kecamatan = sub-district

explanation: 1 Island have have more than 1 sub-district



[img]http://img708.imageshack.us/img708/4700/failedm.jpg[/img]


sorry bad English...

jrowe
  • profile picture
  • Member

Posted 25 July 2012 - 15:41 PM

BTW KaBa did you know this doesn't work in IE7? When I select Country it shows animation but doesn't show the next state/province drop down box..