Loop through a single <td> element - Laravel

Viewed 829

i'm new to Laravel. I used @foreach to make a table that gets data from database. I want to make the database value 1 & 0 display as words like Active & Inactive. However, the javascript I made to do it only display the first column(as shown in the output image below). Is there a way to make the script apply to the whole table? Thank you!

<tbody>
    @foreach ($data as $row)
    <tr>
        <td> {{ $row->id }}</td>
        <td> {{ $row->name }}</td>
        <td> {{ $row->description }}</td>
        <td id=statust> {{ $row->status }}</td>
        <td id="defaulttt"> {{ $row->default }}</td>
        <td><a href="{{action('DetailTestController@edit',$row->id)}}" class="btn btn-warning">EDIT</a></td>
        <td>
            <form method="post" class="delete_form" action="{{action('DetailTestController@destroy', $row['id'])}}">
                {{csrf_field()}}
                <input type="hidden" name="_method" value="DELETE"/>
                <button type="submit" class="btn btn-danger">Delete</button>
            </form>
        </td>
    </tr>
    @endforeach
</tbody>

Javascript

function checkshow() {
    if (document.getElementById("statust").innerHTML == 1) {
        document.getElementById("statust").innerHTML = "Active";
    } else {
        document.getElementById("statust").innerHTML = "Inactive";
    }
    return;
}

Output

5 Answers

I'm not sure why you'd involve a js snippet for this purpose. In fact, you can simply just use a ternary in this case.

Blade templates will understand this shorthand if else (ternary operator):

<tbody>
    @foreach ($data as $row)
    <tr>
    <td> {{ $row->id }} </td>
    <td> {{ $row->name }} </td>
    <td> {{ $row->description }} </td>
    <td> {{ ($row->status == 1) ? 'Active' : 'Inactive' }} </td>
    <td id = "defaulttt"> {{ $row->default }} </td>
    <td><a href="{{action('DetailTestController@edit',$row->id)}}" class="btn btn-warning">EDIT</a></td>
    <td>
    <form method="post" class="delete_form" action="{{action('DetailTestController@destroy', $row['id'])}}">
      {{csrf_field()}}
      <input type="hidden" name="_method" value="DELETE" />
      <button type="submit" class="btn btn-danger">Delete</button>
    </form>
    </td>
    </tr>

    @endforeach
</tbody>

Sidenote for JS: If you're trying to apply multiple instances of actions, classes are better suited with this task not ids

put this condition to know your status

@foreach ($data as $row)
 @php
   $status = "";
   @if($row->status == '1')
     $status = "Active";
   @else
     $status = "Inactive";
   @elseif
 @endphp

<td>{{ $status }} </td>
@endforeach

Or

<td> {{ ($row->status == 1) ? 'Active' : 'Inactive' }} </td>

You can do like that

<tbody>
       @foreach ($data as $row)
        <tr>
        <td> {{ $row->id }} </td>
        <td> {{ $row->name }} </td>
        <td> {{ $row->description }} </td>
        <td id = statust> @if($row->status == 1) Active @else Inactive @endif </td>
        <td id = "defaulttt"> {{ $row->default }} </td>
        <td><a href="{{action('DetailTestController@edit',$row->id)}}" class="btn btn-warning">EDIT</a></td>
        <td>
        <form method="post" class="delete_form" action="{{action('DetailTestController@destroy', $row['id'])}}">
          {{csrf_field()}}
          <input type="hidden" name="_method" value="DELETE" />
          <button type="submit" class="btn btn-danger">Delete</button>
        </form>
        </td>
        </tr>

        @endforeach
  </tbody>
<tbody>
   @foreach ($data as $row)
     @php
       $status = "";
       @if($row->status == '1')
         $status = "Active"
       @else
         $status = "Inactive"
       @elseif
     @endphp
    <tr>
    <td> {{ $row->id }} </td>
    <td> {{ $row->name }} </td>
    <td> {{ $row->description }} </td>
    <td>{{ $status }} </td>
    <td id = "defaulttt"> {{ $row->default }} </td>
    <td><a href="{{action('DetailTestController@edit',$row->id)}}" class="btn btn-warning">EDIT</a></td>
    <td>
    <form method="post" class="delete_form" action="{{action('DetailTestController@destroy', $row['id'])}}">
      {{csrf_field()}}
      <input type="hidden" name="_method" value="DELETE" />
      <button type="submit" class="btn btn-danger">Delete</button>
    </form>
    </td>
    </tr>
    @endforeach

The reason why your code only affects on the first row of the table is because you are calling your element through an id instead of class as selector. Id selector is for the element that is unique. Change it to class and on javascript loop through class and apply your logic like so.

If you are using jQuery:

$('.statust').each(function() {
   if($(this).html() ==1){
      $(this).html("Active");
   }else{
      $(this).html("Inactive");
   }  
})

If you are using javascript core:

document.getElementsByClassName("statust").forEach(function(e) {
   if(e.innerHTML ==1){
      e.innerHTML = "Active";
   }else{
      e.innerHTML = "Inactive";
   }  
})
Related