How do I color a row based on the condition

Discuss everything related to ApPHP DataGrid class. All versions. Any questions related to the ApPHP DataGrid developing or using should be posted to this forum.
Please, check out Knowledge Base first before you post a question.

Moderator: alexandrleonenko

Forum rules
Posts are to be made in the relevant forum. Users are asked to read the forum descriptions before posting.
wellsantos
Newbie
Newbie
Posts: 8
Joined: Dec 8th, '13, 16:25

How do I color a row based on the condition

Postby wellsantos » Dec 30th, '13, 22:49

How do I color a row based on the condition of some data in the database?
mode VIEW
ex:
id active product
----------------------
1 1 xxxxxxxx
2 0 xxxxxxxxx - this line in red ???
1 1 xxxxxxxxx
4 1 xxxxxxxxx


wellsantos
Newbie
Newbie
Posts: 8
Joined: Dec 8th, '13, 16:25

Re: How do I color a row based on the condition

Postby wellsantos » Jan 1st, '14, 18:25

$curr_value = str_replace(",", '', $fp_on_item_created($row[$c]));

this line not exist´s in my datagrid.php, my verion is lasted.

you already have the file ready where I can be testing or downloading?

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

Re: How do I color a row based on the condition

Postby administrator » Jan 5th, '14, 06:52

Try this one:
$curr_value = str_replace(',', '', $fp_on_item_created($row_c));

Does it exist?

cphil123
Senior
Senior
Posts: 26
Joined: Mar 28th, '11, 11:23
Location: Belgique

Re: How do I color a row based on the condition

Postby cphil123 » Jan 14th, '17, 23:10

Hi,

I would like to make sure to put the lines in color on the fly, when it is active or not active

I tested the following solution: http://www.apphp.net/forum/viewtopic.php?f=33&t=3295

I change the function:

Code: Select all

function SetColors($val, $r){
    $unique_prefix = "TACH_";
    if($val == "Oui"){
        echo '<script>document.getElementById("'.$unique_prefix.'row_"+'.$r.').style.backgroundColor = "#38c101";</script>';
    }elseif($val == "Non"){
       echo '<script>document.getElementById("'.$unique_prefix.'row_"+'.$r.').style.backgroundColor = "#ff0000";</script>';    
    }
    return $val;       
}

Could you provide a datagrid file as an example ?
all my tests fail ...

thank you for your support
Phil

cphil123
Senior
Senior
Posts: 26
Joined: Mar 28th, '11, 11:23
Location: Belgique

Re: How do I color a row based on the condition

Postby cphil123 » Jan 26th, '17, 20:33

hi,

is there a solution ? :roll:

Phil

madmucho
Newbie
Newbie
Posts: 2
Joined: Dec 29th, '17, 17:39

Re: How do I color a row based on the condition

Postby madmucho » Dec 3rd, '18, 22:52

Hi,
solving same goal using refered change, but little bit nicely.

change row in phpdatagrid class as refered in WAY #2 it is because $row data in not here normaly we need it.

Code: Select all

// customized work with field value
 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);
}


In your field in view, refer to function which is called after row is created on_item_created

Code: Select all

$vm_columns = array(
    'name' => array('header' => "name", 'type' => 'textbox', 'req_type' => 'ryy', 'width' => '210px', 'title' => '', 'readonly' => 'true', 'maxlength' => '65', 'default' => '', 'unique' => 'false', 'unique_condition' => '', 'visible' => 'true', 'on_js_event' => '','on_item_created' => 'SetColors'),
....


And my php function, define it in upper side of your php code.
in this function is array variable $row which contains all fields from SQL select, so you can assign dynamic tresholds or condition based on row values from sql, and you dont need to display that values just use them for compute, that is nice.
This function print at row level of datagrid small <span></span> which have class defined as orange or red, this class you must catch later and collor with Jquery, so i dont find there row number then i used class in selector.

Code: Select all

function SetColors($field_value, $ind, $row){
    if($row['hourdiff'] >= $row['warning'] AND $row['hourdiff'] <= $row['error']){
        echo '<span class="orange"></span>';
    }elseif($row['hourdiff'] > $row['error']){
        echo '<span class="red"></span>';
    }
   
    return $field_value;
}


Jquery part on page to handle on mouse over row and on mouse out, and colorize row.
Put this code before <body> of your html, and have included jquery libs, hope you have them from datagrid itself.

Code: Select all

<script type="text/javascript">
        $(document).ready( function() {
            $(".red").closest( "tr" )
                    .css( "background-color", "red" )
                    .removeAttr("onmouseover")
                    .removeAttr("onmouseout");
            $(".orange").closest( "tr" )
                    .css( "background-color", "orange" )
                    .removeAttr("onmouseover")
                    .removeAttr("onmouseout");
        });
</script>


Translation of jquery code: find all class .red or .orange, at this place find closes tag tr and change its background to red or orange, and remove onmouseover and onmouseout efect on this row to not change our color

And done. Using like this and is sufficient.
If works, You owe me beer

cphil123
Senior
Senior
Posts: 26
Joined: Mar 28th, '11, 11:23
Location: Belgique

Re: How do I color a row based on the condition

Postby cphil123 » Jan 31st, '19, 08:52

:ugeek:
Impeccable, it works.

When you want for a beer...

Phil

madmucho
Newbie
Newbie
Posts: 2
Joined: Dec 29th, '17, 17:39

Re: How do I color a row based on the condition

Postby madmucho » Feb 3rd, '19, 21:41

cphil123 wrote::ugeek:
Impeccable, it works.

When you want for a beer...

Phil

Glad that helps,
beer beging > https://giveabeer.me/madmucho


Return to “ApPHP DataGrid”