HOW TO change the rows color "on fly"?

Talk and info about common issues

Moderator: alexandrleonenko

administrator
Site Admin
Site Admin
Posts: 6004
Joined: Jan 7th, '09, 23:18
Contact:

HOW TO change the rows color "on fly"?

Postby administrator » Feb 29th, '12, 11:04

ApPHP DataGrid supports an "on_item_created" feature for columns,
so using this feature and some others tricks you can change a color of rows,
according to some column values.

Remember: this is only an example of HOW TO change the color of rows. Of course, you may change and improve this method.

WAY #1 INSTRUCTIONS:
1. Open datagrid.class.php file and find there following line of code:

Code: Select all

$curr_value = str_replace(',', '', $fp_on_item_created($row_c));

and change it with

Code: Select all

$curr_value = str_replace(',', '', $fp_on_item_created($row_c), $r);

Now your trigger "on_item_created" method may accept an id of the row.

2. Create(add) to appropriate View Mode field "on_item_created"=>"SetColors" property.

3. Create "SetColors" functions at the bottom of your page in a following way:

Code: Select all

function SetColors($val, $r){
    $unique_prefix = "er_";
    if($val < 100){
        echo '<script>document.getElementById("'.$unique_prefix.'row_"+'.$r.').style.backgroundColor = "#ff0000";</script>';
    }
    return $val;
}


WAY #2 INSTRUCTIONS:

1. Change following lines of code (in datagrid.class.php)

Code: Select all

if(function_exists($fp_on_item_created) && $fp_type != "money"){
   //ini_set("allow_call_time_pass_reference", true);
   $field_value = $fp_on_item_created($field_value);
}


with this:

Code: Select all

if(function_exists($fp_on_item_created) && $fp_type != "money"){
   //ini_set("allow_call_time_pass_reference", true);
   $field_value = $fp_on_item_created($field_value, $ind, $row);
}


Define for needed field on_item_created function, something like this:

Code: Select all

.., 'on_item_created'=>'my_function'


define at top of your page:

Code: Select all

 $global_code = '';


and also define this function at the bottom of your page

Code: Select all

function my_function($field_value, $ind, $row){
    global $global_code;
    if($field_value == 2){
        $global_code .= '<script>document.getElementById("er_row_".$ind).style.backgroundColor="#ff0000";</script>';
        return 'two';
    }
    return $field_value;
}

echo $global_code;

Return to “ApPHP DataGrid {HOW TO}”