【问题标题】:Laravel Livewire checkbox checked on edit page在编辑页面上选中 Laravel Livewire 复选框
【发布时间】:2021-12-17 14:05:42
【问题描述】:

我在使用 Livewire 检查编辑页面上的复选框时遇到问题。我尝试了很多方法,但仍然无法检查旧的选定值

查看代码:

<table id="branches" class="table table-bordered table-sm" width="100%" cellspacing="0">
  <thead>
      <tr>
          <th width="10%">

          </th>
          <th>Branch ID</th>
      </tr>
  </thead>
  <tbody>
      @foreach ($propertiesOptions as $key => $property)
          <tr>
              <td>
                  <input class="branchCheckbox" 
                  type="checkbox" 
                  wire:model="propertyIds.{{$property->id}}" 
                  value="{{$property->id}}" @if(in_array($property->id,$propertyIds)) checked @endif>
              </td>
            <td>{{$property->id}}</td>
         </tr>
     @endforeach
 </tbody>
</table>

后端代码:

public $propertyIds, $propertiesOptions;
public function mount($record)
    {
        $this->propertiesOptions = Properties::where('merchant_id',$record->id)->get()
        $this->propertyIds = $record->properties->pluck('property_id')->toArray();

    }```

【问题讨论】:

  • 我更新了我的回答。看看吧。
  • 嘿,谢谢你回答这个问题。但是我尝试了这种方式,但没有成功
  • 但是我的问题通过阅读这个答案得到了解决laracasts.com/discuss/channels/livewire/…
  • 嘿@Humza Faqi!很高兴知道。这意味着:&lt;input class="branchCheckbox" type="checkbox" wire:model="propertyIds.{{$property-&gt;id}}" value="{{$property-&gt;id}}"&gt; 没有条件,其余的由 livewire 完成。

标签: laravel laravel-livewire


【解决方案1】:
`<table id="branches" class="table table-bordered table-sm" width="100%" cellspacing="0">
  <thead>
      <tr>
          <th width="10%">

          </th>
          <th>Branch ID</th>
      </tr>
  </thead>
  <tbody>
      @foreach ($propertiesOptions as $key => $property)
          <tr>
              <td>
                  <input class="branchCheckbox" 
                  type="checkbox" 
                  wire:model="propertyIds" 
                  value="{{$property->id}}" >
              </td>
            <td>{{$property->id}}</td>
         </tr>
     @endforeach
 </tbody>
</table>`

后台

public $propertyIds, $propertiesOptions;
public function mount($record)
    {
        $this->propertiesOptions = Properties::where('merchant_id',$record->id)->get()
        $this->propertyIds = $record->properties->pluck('property_id')->toArray();

    }

【讨论】:

    【解决方案2】:

    更新 你能检查一下这是否正确吗? wire:model="propertyIds.{{$property-&gt;id}}"?尝试删除wire:model。

    <input class="branchCheckbox" type="checkbox" 
      value="{{$property->id}}" 
      @if(in_array($property->id,$propertyIds)) checked @endif>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多