Welcome Guest, Not a member yet? Register   Sign In
Customized table generation with conditional cell styling
#1

[eluser]LeonardoGaiero[/eluser]
Greetings, all. I have been working on a personalized table generation library, using CI 1.7.2, to create a table which changes layout depending on values pulled from a database. It's currently structured like this:

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Table extends CI_Table {
    
    function generate($table_data = NULL)
    {
        // (truncated for length, up until here it's the same as the default lib)
        // Build the table rows (start of edits)

        if (count($this->rows) > 0)
        {
            $i = 1;
            
            foreach($this->rows as $row)
            {

                if ( ! is_array($row))
                {
                    break;
                }
                if(array_key_exists('tic_id', $row)) {
                    $row['tic_id']='<span class="bold">'.$row['tic_id'].'</span>';
                }
                if(array_key_exists('tic_subject', $row)) {
                    $row['tic_subject']='<span class="bold">'.$row['tic_subject'].'</span>';
                }
                if(array_key_exists('tic_status', $row)) {
                    $row['tic_status']=$ci->helpdesk->getReadableStatus($row['tic_status']);
                }
                if(array_key_exists('tic_type', $row)) {
                    $row['tic_type']=$ci->helpdesk->getReadableType($row['tic_type']);
                }
                
                // We use modulus to alternate the row colors
                $name = (fmod($i++, 2)) ? '' : 'alt_';
            
                $out .= $this->template['row_'.$name.'start'];
                $out .= $this->newline;        
                if(array_key_exists('tic_priority', $row)) {
                    $row['tic_priority']=$ci->helpdesk->getReadablePriority($row['tic_priority']);
                    switch ($row['tic_priority']) {
                        case "bassa":
                            $row['tic_priority']='<div class="lopri logview">'.$row['tic_priority'].$row.'</div>';
                            break;
                        case "media":
                            $row['tic_priority']='<div class="medpri logview">'.$row['tic_priority'].$row.'</div>';
                            break;
                        case "alta":
                            $row['tic_priority']='<div class="hipri logview">'.$row['tic_priority'].$row.'</div>';
                            break;
                        default:
                            echo ('<div></div>');
                    }
                }
    
                foreach($row as $cell)
                {
                    $out .= $this->template['cell_'.$name.'start'];
                    
                    if ($cell === "")
                    {
                        $out .= $this->empty_cells;
                    }
                    else
                    {
                        $out .= $cell;
                    }
                    
                    $out .= $this->template['cell_'.$name.'end'];
                }
    
                $out .= $this->template['row_'.$name.'end'];
                $out .= $this->newline;    
            }
        }

        $out .= $this->template['table_close'];
    
        return $out;
    }
}

What's going on here is that the new code intercepts the specified rows and modifies their values, calling a getReadable function which simply assigns a human-readable text to the retrieved value.

My problem, however, as a lot of people will no doubt notice from the less-than-elegant styling to the tic_priority cell, is that I don't have a clean way to conditionally style the cells directly. What this code does is to open a div within the cell (in place of the query results, actually) and use a class to set the background color, and another one to set the div height manually, since if I don't only the div text ends up with a recolored background. Of course this is a very crude hack, and I'm looking for a better solution. Rather than winding up with this code:

Code:
<td><div class="lopri logview">bassa</div></td>

I would like to have something like this instead:

Code:
<td class="lopri">bassa</td>

My problem is, however, that I don't have the foggiest on how to intercept the proper cell number, and style that one. I can find out if the tic_priority key exists already, but how would I go to assign it to a numeric value showing the code which cell to process? Code reusability is not a strict priority, I will be using this function for three views where I already know which values I should expect. Many thanks in advance for your advice, I hope I’ve provided adequate information on my problem.
#2

[eluser]LeonardoGaiero[/eluser]
Sorry for the double post, but I believe I solved my problem and thought I'd share:

Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Table extends CI_Table {
    
        // (what I did was use a variable to keep the desired field index)        
        $j = 0;
        
        foreach($this->rows[0] as $key => $pos)
        {
            // search for the proper key; if not found, advance the counter, if found break
            if ($key == 'tic_priority') {
                break 1;
            }
            $j++;
        }

        
        // Build the table rows
        if (count($this->rows) > 0)
        {
            $i = 1;
            
            foreach($this->rows as $row)
            {

                if ( ! is_array($row))
                {
                    break;
                }
                if(array_key_exists('tic_id', $row)) {
                    $row['tic_id']='<span class="bold">'.$row['tic_id'].'</span>';
                }
                if(array_key_exists('tic_subject', $row)) {
                    $row['tic_subject']='<span class="bold">'.$row['tic_subject'].'</span>';
                }
                if(array_key_exists('tic_status', $row)) {
                    $row['tic_status']=$ci->helpdesk->getReadableStatus($row['tic_status']);
                }
                if(array_key_exists('tic_type', $row)) {
                    $row['tic_type']=$ci->helpdesk->getReadableType($row['tic_type']);
                }
                
                // We use modulus to alternate the row colors
                $name = (fmod($i++, 2)) ? '' : 'alt_';
            
                $out .= $this->template['row_'.$name.'start'];
                $out .= $this->newline;        

                // checks if the required key exists; if it does, it sets a boolean and substitutes the number with the human-readable value
                if(array_key_exists('tic_priority', $row)) {
                    $prioExists = true;
                    $row['tic_priority']=$ci->helpdesk->getReadablePriority($row['tic_priority']);
                }
                $k=0;
                foreach($row as $cell)
                {
                    // checks if the tic_priority record exists through the boolean value, and if the current index is the proper one
                    if ($prioExists && $k == $j) {
                        // applies the required conditional CSS
                            case "bassa":
                                $out .= '<td class="lopri">';
                                break;
                            case "media":
                                $out .= '<td class="medpri">';
                                break;
                            case "alta":
                                $out .= '<td class="hipri">';
                                break;
                            default:
                            $out .= $this->template['cell_'.$name.'start'];
                        }
                    } else {
                    $out .= $this->template['cell_'.$name.'start'];
                    }
                    if ($cell === "")
                    {
                        $out .= $this->empty_cells;
                    }
                    else
                    {
                        $out .= $cell;
                    }
                    
                    $out .= $this->template['cell_'.$name.'end'];
                    $k++;
                }
                
                
                $out .= $this->template['row_'.$name.'end'];
                $out .= $this->newline;    
            }
        }

        $out .= $this->template['table_close'];
    
        return $out;
    }
}

I'm not sure if it's bug-free, but I'll have to test this either way in a couple of different scenarios. Hope this post wasn't completely pointless Smile I suppose I'll do the remaining fields in the same fashion next, provided that I don't get lazy.




Theme © iAndrew 2016 - Forum software by © MyBB