laravel Livewire 更新多个下拉列表的特定类别
laravel Livewire updating specific category on multiple dropdowns
我正在尝试使用下拉菜单更新 table 字段。我已经让它只处理一行数据,但我无法让 livewire 跟随这些行。我认为这是我应该寻找的地方,但我没有关注该做什么。 https://laravel-livewire.com/docs/2.x/nesting-components
[前三行的屏幕截图][1]
[1]: https://i.stack.imgur.com/znG96.png
第一个下拉菜单基本上是 class 个类别,第二个下拉菜单是我要分配给交易的特定类别。
主视图
@livewire('transaction-category', ['types'=>$types], key($types->type))
分量
<div>
<td class="border" >
<select name="types" wire:model="types" class="inv_select ">
<option value=''>Choose Category Class</option>
@foreach($types as $t)
<option value={{ $t->type }}>{{ $t->type }}</option>
@endforeach
</select>
</td>
//@if(count($subclass>0)
<td>
<select name="subclass" wire:model="subclass" class="inv_select">
<option value=''>Choose Category</option>
@foreach($subclass as $s)
<option value={{ $s->type }}>{{ $s->type }}</option>
@endforeach
</select>
</td>
//@endif
</div>
Livewire 型号
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Category;
class TransactionCategory extends Component
{
public $types;
public $subclass=[];
public function mount(){
$this->types=Category::select('type')->groupby('type')->get();
}
public function updatedTypes(){
dd($this->subclass =Category::subs($this->types));
$this->subclass =Category::subs($this->types);
}
public function render()
{
return view('livewire.transaction-category');
}
}
您需要更好地理解 livewire 关于 public 属性的含义以及 collections 之间的区别。我不清楚你的 table 循环的模型是什么,所以我假设是关于交易模型。
我的方法首先是
@livewire('transaction-category', ['transaction' => $transaction], key($transaction->id))
public Transaction $transaction;
public $selectedType, $selectedSub;
public $types, $subclass;
public function mount(){
// I'm going to improve with your code
$this->types = Category::all();
$this->subclass = Collection()->empty();
}
public function updatedSelectedType($value)
{
if($value) {
$this->subclass = Category::subs($this->types)->get();
} else {
$this->subclass = Collection()->empty();
}
}
和 blade 元素
<div>
<td class="border" >
<select name="types" wire:model="selectedType" class="inv_select ">
<option value=''>Choose Category Class</option>
@foreach($types as $t)
<option value={{ $t->type }}>{{ $t->type }}</option>
@endforeach
</select>
</td>
@if(count($subclass))
<td>
<select name="subclass" wire:model="selectedSub" class="inv_select">
<option value=''>Choose Category</option>
@foreach($subclass as $s)
<option value={{ $s->type }}>{{ $s->type }}</option>
@endforeach
</select>
</td>
@endif
</div>
我正在尝试使用下拉菜单更新 table 字段。我已经让它只处理一行数据,但我无法让 livewire 跟随这些行。我认为这是我应该寻找的地方,但我没有关注该做什么。 https://laravel-livewire.com/docs/2.x/nesting-components
[前三行的屏幕截图][1] [1]: https://i.stack.imgur.com/znG96.png 第一个下拉菜单基本上是 class 个类别,第二个下拉菜单是我要分配给交易的特定类别。
主视图
@livewire('transaction-category', ['types'=>$types], key($types->type))
分量
<div>
<td class="border" >
<select name="types" wire:model="types" class="inv_select ">
<option value=''>Choose Category Class</option>
@foreach($types as $t)
<option value={{ $t->type }}>{{ $t->type }}</option>
@endforeach
</select>
</td>
//@if(count($subclass>0)
<td>
<select name="subclass" wire:model="subclass" class="inv_select">
<option value=''>Choose Category</option>
@foreach($subclass as $s)
<option value={{ $s->type }}>{{ $s->type }}</option>
@endforeach
</select>
</td>
//@endif
</div>
Livewire 型号
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Category;
class TransactionCategory extends Component
{
public $types;
public $subclass=[];
public function mount(){
$this->types=Category::select('type')->groupby('type')->get();
}
public function updatedTypes(){
dd($this->subclass =Category::subs($this->types));
$this->subclass =Category::subs($this->types);
}
public function render()
{
return view('livewire.transaction-category');
}
}
您需要更好地理解 livewire 关于 public 属性的含义以及 collections 之间的区别。我不清楚你的 table 循环的模型是什么,所以我假设是关于交易模型。
我的方法首先是
@livewire('transaction-category', ['transaction' => $transaction], key($transaction->id))
public Transaction $transaction;
public $selectedType, $selectedSub;
public $types, $subclass;
public function mount(){
// I'm going to improve with your code
$this->types = Category::all();
$this->subclass = Collection()->empty();
}
public function updatedSelectedType($value)
{
if($value) {
$this->subclass = Category::subs($this->types)->get();
} else {
$this->subclass = Collection()->empty();
}
}
和 blade 元素
<div>
<td class="border" >
<select name="types" wire:model="selectedType" class="inv_select ">
<option value=''>Choose Category Class</option>
@foreach($types as $t)
<option value={{ $t->type }}>{{ $t->type }}</option>
@endforeach
</select>
</td>
@if(count($subclass))
<td>
<select name="subclass" wire:model="selectedSub" class="inv_select">
<option value=''>Choose Category</option>
@foreach($subclass as $s)
<option value={{ $s->type }}>{{ $s->type }}</option>
@endforeach
</select>
</td>
@endif
</div>