Test Coder
Test Coder

Reputation: 37

Laravel table cell color with cell value

This is my blade loop.

@for ($coun=0;$coun< $count_2; $coun++)
<table id="table2" border="1" style="display: inline-block;clear: both;">
        <tbody>
          <tr><td><?php print_r( $array_mem['id'][$coun]); ?></td></tr>
          <tr><td><?php print_r( $array_mem['Name'][$coun]); ?></td></tr>
          <tr><td><?php print_r( $array_mem['Mark'][$coun]); ?></td></tr>
          <tr><td><?php print_r( $array_mem['Sub_1'][$coun]); ?></td></tr>
          <tr><td><?php print_r( $array_mem['Sub_2'][$coun]); ?></td></tr>
          <tr><td><?php print_r( $array_mem['Sub_3'][$coun]); ?></td></tr>
        </tbody>
</table> @endfor

I Have set of conditions to check the values in each column. and also change the cell colour depends.

+-------------------+-------------+
| Condition         | Cell Colour |
+-------------------+-------------+
| If Id < 10        | Red         |
+-------------------+-------------+
| If Id>10 && Id<20 | Green       |
+-------------------+-------------+
| If Mark <20       | Red         |
+-------------------+-------------+
| If Mark >20       | Green       |
+-------------------+-------------+
| If Sub_1 < 20     | Red         |
+-------------------+-------------+

Is there any way to check and make the cell in specific colour.

Am looking something except using if else condition, that means using function or this.

Upvotes: 1

Views: 847

Answers (2)

Joseph
Joseph

Reputation: 6269

You can make a mutator from your model class and add all of your logic in

public function getCellColorAttribute(){
  $cellColor = "";
  if($this->id > 10 || $this->mark < 20 || $this->sub_1 < 20 ) 
        $cellColor = 'red';
  else 
        $cellColor = 'green';

  return $cellColor;
}

now when you iterate this model in your view you can access this new attribute with cell_color

you can read more about Accessors & Mutators here

Upvotes: 1

The Alpha
The Alpha

Reputation: 146239

You should extract the logic somewhere else instead of doing if/else in the view and you can do it in many ways. One approach could be creating a helper file and declaring a helper function and use it in view. So, to do that, you can create a helper file in app/Helpers/Helper.php, then in your composer.json file you can add an entry in files section like this:

"autoload": {
    "classmap": [
        ...
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files": [
        "app/Helpers/Helpers.php"
    ]
},

Then in your "app/Helpers/Helpers.php" file add a function, for example:

if (!function_exists('getCellColor')) {
    function getCellColor($arg) {
        $cellColor = '';

        // Implement your login verifying the $arg...

        return $cellColor;
    }
}

Then you should run composer dump-autoload from your terminal/command prompt. Now you can use that function in the place where you need the formatting. For example:

<tr><td style="color: {{ getCellColor($array_mem) }}"></td></tr>

In this case, you can return something like this from the function:

// ...

$cellColor = '';

// Verify and set the color...

$cellColor = 'red';

return $cellColor;

Alternatively, you can use/return a class like:

$cellColorClass = 'red';

// assign the right color class...

return $cellColorClass;

You may use it like:

<tr><td class="{{ getCellColor($array_mem) }}"></td></tr>

In that case, you need to create some css rules in a new or existing css file like:

.red {
    color: red;
}

.green {
    color: green;
}

That's it. This is one approach among many, so take the idea and use what you think is better for you but this one is easy, IMO.

Upvotes: 1

Related Questions