Laravel Blade:每次增量变化1?

时间:2015-06-18 15:12:29

标签: php laravel laravel-4 blade

使用Laravel刀片模板,有没有办法在foreach中包含变量并增加每次更新或更好的方法?

例如:

@foreach($categories as $category)
  <li><a href="#tab_c1" role="tab" data-toggle="tab">{{$category->name}}</a></li>
@endforeach

foreach块中,#tab_c1的值需要增加。例如:#tab_c1, #tab_c2, #tab_c3

7 个答案:

答案 0 :(得分:19)

将迭代器添加到@foreach

@foreach($categories as $key => $category)
  <li @if ($key === 0) class="active" @endif>
    <a href="#tab_c{{$key+1}}" role="tab" data-toggle="tab">
      {{$category->name}}
    </a>
  </li>
@endforeach
在我的例子中

{{$key+1}},因为在PHP中,迭代器从0开始。

答案 1 :(得分:13)

在Laravel 5.3中,您可以使用循环变量,$ loop-&gt;迭代来了解具体情况。 https://laravel.com/docs/5.3/blade#the-loop-variable

Exapmle:

@foreach ($questions as $question)
    <tr>
        <th scope="row">{{ $loop->iteration }}</th>
        <td>{{ $question->question }}</td>
        <td>{{ $question->category_id }}</td>
    </tr>
@endforeach

答案 2 :(得分:4)

只需使用键值即可。对于大多数只是0的数组。

@foreach($categories as $i => $category)
  <li{{ $i == 0 ? ' class="active"' : '' }}><a href="#tab_c{{ $i }}" role="tab" data-toggle="tab">{{$category->name}}</a></li>
@endforeach

答案 3 :(得分:2)

在foreach循环中添加键值

@foreach($questions as $key => $question)
<tr>
    <th scope="row">{{ ++$key }}</th>
    <td>{{ $question->question }}</td>
    <td>{{ $question->category_id }}</td>
</tr>
@endforeach

答案 4 :(得分:2)

只需使用{{ $loop->iteration }}从1进行迭代即可限制

@foreach($categories as $category)
  <li><a href="#tab_c{{ $loop->iteration }}" role="tab" data-toggle="tab">{{$category->name}}</a></li>
@endforeach

答案 5 :(得分:0)

您可以尝试以下方法:

<div className={`form-control valbox1 dvPercValue`} disabled = {isDisabled ? true : false } onClick={event=>this.focusPerc(event, rowIndex)}>
    <Textbox
      onTextboxChange={e => this.onTextboxChange(e, rowIndex)}
      name="percentage"
      isDisabled={isDisabled}
      value={(value) } 
      maxLength={4}
      refData={(input) => {
          this.myAllocat[rowIndex] = input
      }}
      className = "percValue"
      onInputKeyPress= {event=> this.setWidth(event, rowIndex) }
      onInputkeyUp={ event=> this.setInputValue(event, rowIndex)}
      textBoxWidth= { textBoxWidth }
    />
    { value && <span className="percSign ">%</span> }
</div> 

const textBoxWidth = { 
      width: (value && parseInt(value,10) < 101) ? ((value.length ) * 8) : 0
    };

focusPerc =(e, rowIndex) => {
        if (rowIndex >=0) {
          let searchInput= null;
          const allBox = document.querySelectorAll(".dvPercValue");
          for (let i=0;i<allBox.length;i++) { 
            allBox[i].classList.remove("active");
          }
          if (e.target && e.target.tagName) {
            if (e.target.tagName.toLowerCase() === 'span' || e.target.tagName.toLowerCase() === 'input') {
              e.target.parent.classList.add("active");
              searchInput= e.target.parent.children[0];
            } else {
              e.target.classList.add("active");
              searchInput = e.target.children[0]
            }

          }
          if (searchInput) {
            const strLength = searchInput.value.length * 2;
            searchInput.focus();
            searchInput.setSelectionRange(strLength, strLength);

          }
        }

      }

 setInputValue = (e, rowIndex) => {
    const inputvalue=e.target.value.trim();
    if (e.keyCode ===8 && rowIndex >=0) {
      if (parseInt(inputvalue,10) > 100) {
        return;
      }
      e.target.style.width = ((inputvalue.length ) * 8) + 'px';
    }
  }
  setWidth =(e, rowIndex) => {
    const inputval=e.target.value.trim();
    if (parseInt(inputval,10) > 100) {
      return;
    }
    e.target.style.width = ((inputval.length + 1) * 8) + 'px';
  }

const TextboxComponent = (props) => {
  return (
    <input type="text
      value= {props.value}
      name= {props.name}
      maxLength= {props.maxLength}
      className={props.className}
      disabled= {props.isDisabled}
      placeholder= {props.placeHolder}
      onChange={props.onTextboxChange}
      onKeyPress = {props.onInputKeyPress}
      onBlur = {props.onTextBoxBlur}
      ref={props.refData}
      autoComplete="off"
      onKeyUp = {props.onInputkeyUp}
      style = { props.textBoxWidth } 
    />
  )
}

答案 6 :(得分:0)

这应该可以解决问题 @php与laravel中的php打开和关闭标签相同

<?php $count=0; ?>
 @php($count=0)

<table>
<th>#</th>
<th>Category Name</th>
<tbody>
@php($count=0)
@foreach($categories as $category)
@php($count++)
<tr>
<td>{{$count}}</td>
<td>{{$category->name}}</td>
</tr>
@endforeach
</tbody>
</table>