Change table color on Laravel Blade

Asked

Viewed 374 times

1

Hello. I want to change the background of a table according to the type in DB. I am using Laravel 5.5. Can you help me?

  • Same logic https://answall.com/a/281717/99718 . If you can’t, post your code as this will help.

2 answers

0

To solve, I made a validation.

    @if( $financa->banco == 1)
      @php $cor = 'red'  @endphp
    @elseif( $financa->banco == 2)
      @php $cor = 'green'  @endphp
    @else
      @php $cor = 'pink'  @endphp
    @endif

0

First, what you should keep in mind is that it is not the Laravel that makes the change in specific, but rather a possible class that you define in the tr table as in the example below:

.mytable tr td, .mytable tr, .mytable {
  padding:0; margin:0; border:none;
}
.mytable tr.red td { background: red; }
.mytable tr.blue td { background: blue; }
.mytable tr.green td { background: green; }
.mytable tr.yellow td { background: yellow; }
.mytable tr.pink td { background: pink; }
<table class="mytable" cellspacing="0" cellpadding="0" border="0">
  <tr><th>nome</th> <th>tipo</th></tr>
  <tr class="red"><td>nome</td> <td>tipo</td></tr>
  <tr class="blue"><td>nome</td> <td>tipo</td></tr>
  <tr class="green"><td>nome</td> <td>tipo</td></tr>
  <tr class="yellow"><td>nome</td> <td>tipo</td></tr>
  <tr class="pink"><td>nome</td> <td>tipo</td></tr>
</table>

So in order for your problem to be solved you must do something like this:

<!-- forçando uma lista manualmente para o exemplo -->
<?php
    $lista = [
        ['nome' => 'Foo', 'tipo' => 'red'],
        ['nome' => 'Bar', 'tipo' => 'blue']
        ['nome' => 'Bin', 'tipo' => 'green']
        ['nome' => 'Ids', 'tipo' => 'yellow']
        ['nome' => 'Asd', 'tipo' => 'pink']
    ];
?>

<table class="mytable" cellspacing="0" cellpadding="0" border="0">
    <tr>
        <th>nome</th>
        <th>tipo</th>
    </tr>
    @for ($i = 0; $i < count($lista); $i++)
        <tr class="{{ $lista[$i]['tipo'] }}">
            <td>{{ $lista[$i]['nome'] }}</td>
            <td>{{ $lista[$i]['tipo'] }}</td>
        </tr>
    @endfor
</table>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.