【发布时间】: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!很高兴知道。这意味着:
<input class="branchCheckbox" type="checkbox" wire:model="propertyIds.{{$property->id}}" value="{{$property->id}}">没有条件,其余的由 livewire 完成。