HOW TO: dependent dropdownlists strategies

Talk and info about common issues

Moderator: alexandrleonenko

lambro
Master
Master
Posts: 102
Joined: Sep 13th, '10, 14:31
Location: Metz, France

HOW TO: dependent dropdownlists strategies

Postby lambro » Apr 13th, '11, 14:38

Hi ! I've spent a good half hour searching the forum for dependent dropdownlists strategies, and I've found two very good already :
Based on database queries : MONO/MULTI table(s)
hugomp 's solution at http://www.apphp.net/forum/viewtopic.php?f=20&t=173.

Based on dg class and one extra form : MONO/MULTI table(s)
wonoz's solution in the thread at http://www.apphp.net/forum/viewtopic.php?f=20&t=2362.


Since I got my own to add, I think it's a good idea to link the other solutions as well so that users can select the most appropriate for themselves.

Based on dg class only (using "condition"=> item)
At the top of your dg add :

Code: Select all

// On prépare les valeur de retour
$afftypea =(isset($_POST['com_ff_ta_product_type_a']))? $_POST['com_ff_ta_product_type_a'] : "";
$afftypeb =(isset($_POST['com_ff_ta_product_type_b']))? $_POST['com_ff_ta_product_type_b'] : "";
$afftypec =(isset($_POST['com_ff_ta_product_type_c']))? $_POST['com_ff_ta_product_type_c'] : "";
$afftyped =(isset($_POST['com_ff_ta_product_type_d']))? $_POST['com_ff_ta_product_type_d'] : "";
$afftypee =(isset($_POST['com_ff_ta_product_type_e']))? $_POST['com_ff_ta_product_type_e'] : "";

'$afftype*' is your local value inside the datagrid code template.
'$_POST' corresponds to your $postback_method. Use $_GET or $_REQUEST otherwise.
'com' is your $unique_prefix.
'ff' stands for filtering field, I guess.
'ta_product' is the name of the queried DATABASE used in the filter field.
'type_*' is the name of the queried FIELD used in the filter field.

Now to the Filtering fields:
°1 We define the filtering option settings.

Code: Select all

##  *** set filtering option: true or false(default)
$filtering_option = true;
$show_search_type = ($_SESSION['atype']==2)? false:true;
$dgrid->AllowFiltering($filtering_option, $show_search_type);

°2 We prepare the "condition=>" statement text

Code: Select all

$conda = ($afftypea !="" )? "type_a = '".$afftypea."'":"";
$condb = ($afftypeb !="" )? "type_b = '".$afftypeb."'":"";
$condc = ($afftypec !="" )? "type_c = '".$afftypec."'":"";
$condd = ($afftyped !="" )? "type_d = '".$afftyped."'":"";
$conde = ($afftypee !="" )? "type_e = '".$afftypee."'":"";
$Arrcond = array($conda,$condb,$condc,$condd,$conde);
$textcondition = "1 = 1 ";
foreach ($Arrcond as $val){
if (!empty ($val)){
$textcondition .= " AND ".$val;
}}

This way the statement text will read either 1 =1, or 1=1 AND type_a = '+value' AND type_d = '+value', etc...
°3 We define our filtering fields as dropdownlists with the reloading javascript function included in the datagrid class.

Code: Select all

$filtering_fields[$bycateg]= array("type"=>"dropdownlist", "table"=>"ta_product", "field"=>"type_a", "field_view"=>"type_a", "show_count"=>false, "filter_condition"=>"", "order"=>"ASC", "source"=>"self", "condition"=>$textcondition, "show_operator"=>"false", "default_operator"=>"=", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>$ddlwidth, "multiple"=>"false", "multiple_size"=>"4", "on_js_event"=>"onchange='document.getElementById(\"com_ff_onSUBMIT_FILTER\").click();'");
$filtering_fields[$byfam]  = array("type"=>"dropdownlist", "table"=>"ta_product", "field"=>"type_b", "field_view"=>"type_b", "show_count"=>false, "filter_condition"=>"", "order"=>"ASC", "source"=>"self", "condition"=>$textcondition, "show_operator"=>"false", "default_operator"=>"=", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>$ddlwidth, "multiple"=>"false", "multiple_size"=>"4", "on_js_event"=>"onchange='document.getElementById(\"com_ff_onSUBMIT_FILTER\").click();'");
$filtering_fields[$bylite] = array("type"=>"dropdownlist", "table"=>"ta_product", "field"=>"type_c", "field_view"=>"type_c", "show_count"=>false, "filter_condition"=>"", "order"=>"ASC", "source"=>"self", "condition"=>$textcondition, "show_operator"=>"false", "default_operator"=>"=", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>$ddlwidth, "multiple"=>"false", "multiple_size"=>"4", "on_js_event"=>"onchange='document.getElementById(\"com_ff_onSUBMIT_FILTER\").click();'");
$filtering_fields[$bytype] = array("type"=>"dropdownlist", "table"=>"ta_product", "field"=>"type_d", "field_view"=>"type_d", "show_count"=>false, "filter_condition"=>"", "order"=>"ASC", "source"=>"self", "condition"=>$textcondition, "show_operator"=>"false", "default_operator"=>"=", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>$ddlwidth, "multiple"=>"false", "multiple_size"=>"4", "on_js_event"=>"onchange='document.getElementById(\"com_ff_onSUBMIT_FILTER\").click();'");

$dgrid->SetFieldsFiltering($filtering_fields);
$dgrid->SetFilteringTabularLayoutColumns("2");


So now, the dropdownlists will be filled only with possible matches in accordance with previous choices.
You can go even further and adding extra limitation in the statement text.
Before filtering (view mode) :
Image
Image
After filtering :
Image
Image
@+ !

Return to “ApPHP DataGrid {HOW TO}”