categories and subcategories
Posted 25 March 2012 - 13:42 PM
Posted 25 March 2012 - 15:42 PM
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!!!
Posted 25 March 2012 - 22:33 PM
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 ?
Posted 25 March 2012 - 23:56 PM
[member='KaBaDaBrA'] great job
Posted 26 March 2012 - 05:50 AM
[size=4]And thanks web johnny!!!![/size] [size=4] [/size]
Posted 27 March 2012 - 11:44 AM
Regards, Cèsar.
Posted 27 March 2012 - 17:30 PM
Posted 29 March 2012 - 22:22 PM
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 ?
Posted 30 March 2012 - 06:36 AM
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?
Posted 30 March 2012 - 06:54 AM
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 ) 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.
Posted 30 March 2012 - 07:03 AM
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
}
Posted 02 April 2012 - 13:08 PM
$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
Posted 03 April 2012 - 08:18 AM
Posted 05 April 2012 - 13:30 PM
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
Posted 03 May 2012 - 14:26 PM
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.
Posted 23 May 2012 - 16:39 PM
Posted 13 June 2012 - 15:17 PM
json calls are ok, they return data.
EDIT: was a problem with relation_n Thanks!!!
Posted 24 July 2012 - 09:56 AM
It works perfect!! I was looking for something like that!
btw, there is any posibility to let selected the first dependent subcategory??
Posted 25 July 2012 - 07:37 AM
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...
Posted 25 July 2012 - 15:41 PM