简体   繁体   English

循环一个<td>元素 - Laravel

[英]Loop through a single <td> element - Laravel

i'm new to Laravel.我是 Laravel 的新手。 I used @foreach to make a table that gets data from database.我使用@foreach 制作了一个从数据库中获取数据的表。 I want to make the database value 1 & 0 display as words like Active & Inactive.我想让数据库值 1 & 0 显示为 Active & Inactive 之类的词。 However, the javascript I made to do it only display the first column(as shown in the output image below).但是,我做的 javascript 只显示第一列(如下面的输出图像所示)。 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 Javascript

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

Output输出

I'm not sure why you'd involve a js snippet for this purpose.我不确定您为什么要为此目的使用 js 片段。 In fact, you can simply just use a ternary in this case.事实上,在这种情况下,您可以简单地使用三元。

Blade templates will understand this shorthand if else (ternary operator):如果 else(三元运算符),Blade 模板将理解此速记:

<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, class es are better suited with this task not id s JS 旁注:如果您尝试应用多个操作实例,则class es 更适合此任务而不是id s

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 而不是类作为选择器来调用您的元素。 Id selector is for the element that is unique. Id 选择器用于唯一的元素。 Change it to class and on javascript loop through class and apply your logic like so.将其更改为 class 并在 javascript 循环中通过 class 并像这样应用您的逻辑。

If you are using jQuery:如果您使用 jQuery:

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

If you are using javascript core:如果您使用的是 javascript 核心:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM