tengxuanp
tengxuanp

Reputation: 179

Loop through a single <td> element - Laravel

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

Upvotes: 3

Views: 1722

Answers (5)

Kevin
Kevin

Reputation: 41893

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

Upvotes: 4

VIKAS KATARIYA
VIKAS KATARIYA

Reputation: 6005

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>

Upvotes: 1

Qonvex620
Qonvex620

Reputation: 3972

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";
   }  
})

Upvotes: 0

Chaminda Chanaka
Chaminda Chanaka

Reputation: 190

<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

Upvotes: 0

Lalit Kumar
Lalit Kumar

Reputation: 360

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>

Upvotes: 0

Related Questions