admin管理员组文章数量:1406951
I've tried to make some reusable code an put it into a file. In this file there is Wire:Model.live to generate a search and a dropdown to make a pagination. If i put the code direct into the blade file it works as expected. But if I load it from a file indide the blade the search and the pagination has no effect. Have I to do something after loading the file ?
The file livewire:basedata_wines.head-table
<div>
<table class="table table-striped table-hover table-bordered border-success border-2">
<thead>
<tr>
<th class="col-2 align-middle h3" scope="col">{{ session('Head_Table') }}</th>
<th class="col-3 align-middle h3" scope="col">
<div>
<input type="text" wire:model.live.debounce.1000ms="q" class="form-control"
placeholder="Search....">
</div>
</th>
<th class="col-3 text-center" scope="col">
<div class="form-floating">
<select wire:model.live="pagination" class="form-select" id="paginateselect"
aria-label="page select">
<option value="10">10</option>
<option value="20">20</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
<label for="paginateselect">Results per Page</label>
</div>
</th>
<th class="col-12" scope="col">
<div class="col-12 d-flex align-items-center justify-content-evenly">
<button @click="$dispatch('new-mode')" type="button" class="btn btn-success"
data-bs-toggle="modal" data-bs-target="#TerroirProcessingModal">
{{ session('Button_Table') }}
</button>
</div>
</th>
</tr>
</thead>
</table>
The blade where all the files are loaded:
<div>
<livewire:basedata-wines.base-data />
<div class="col-12">
<livewire:basedata-wines.terroirprocessing />
<div class="border 2px border-success p-2">
<livewire:basedata_wines.head-table />
<table class="table table-striped table-hover table-bordered border-success border-2">
<div>
{{ $this->countries->links() }}
</div>
<thead>
<tr class="h5">
<th class="col-10" scope="col">Country Name</th>
<th class="col-2 text-center" scope="col">Actions</th>
</tr>
</thead>
<tbody>
@foreach ($this->countries as $country)
<tr>
<td class="h5 align-middle col-7">{{ $country->name }}</td>
<td class="text-center btn-group col-12">
<div class="col-6 mt-1">
<button @click="$dispatch('edit-mode',{id:{{ $country->id }}})" type="button"
class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#editgrapeModal">
Edit
</button>
</div>
<div class="col-6 mt-1">
<button @click="$dispatch('delete-mode',{id:{{ $country->id }}})" type="button"
class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#editgrapeModal">
Delete
</button>
</div>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
<livewire:script.fade-alert-script />
<livewire:basedata_wines.head-table />
is to load the file.
The part
<button @click="$dispatch('delete-mode',{id:{{ $country->id }}})" type="button"
works in both options (inside the file and direct in the blade).
Thx for the help, Stephan (still Learning)
I've tried to make some reusable code an put it into a file. In this file there is Wire:Model.live to generate a search and a dropdown to make a pagination. If i put the code direct into the blade file it works as expected. But if I load it from a file indide the blade the search and the pagination has no effect. Have I to do something after loading the file ?
The file livewire:basedata_wines.head-table
<div>
<table class="table table-striped table-hover table-bordered border-success border-2">
<thead>
<tr>
<th class="col-2 align-middle h3" scope="col">{{ session('Head_Table') }}</th>
<th class="col-3 align-middle h3" scope="col">
<div>
<input type="text" wire:model.live.debounce.1000ms="q" class="form-control"
placeholder="Search....">
</div>
</th>
<th class="col-3 text-center" scope="col">
<div class="form-floating">
<select wire:model.live="pagination" class="form-select" id="paginateselect"
aria-label="page select">
<option value="10">10</option>
<option value="20">20</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
<label for="paginateselect">Results per Page</label>
</div>
</th>
<th class="col-12" scope="col">
<div class="col-12 d-flex align-items-center justify-content-evenly">
<button @click="$dispatch('new-mode')" type="button" class="btn btn-success"
data-bs-toggle="modal" data-bs-target="#TerroirProcessingModal">
{{ session('Button_Table') }}
</button>
</div>
</th>
</tr>
</thead>
</table>
The blade where all the files are loaded:
<div>
<livewire:basedata-wines.base-data />
<div class="col-12">
<livewire:basedata-wines.terroirprocessing />
<div class="border 2px border-success p-2">
<livewire:basedata_wines.head-table />
<table class="table table-striped table-hover table-bordered border-success border-2">
<div>
{{ $this->countries->links() }}
</div>
<thead>
<tr class="h5">
<th class="col-10" scope="col">Country Name</th>
<th class="col-2 text-center" scope="col">Actions</th>
</tr>
</thead>
<tbody>
@foreach ($this->countries as $country)
<tr>
<td class="h5 align-middle col-7">{{ $country->name }}</td>
<td class="text-center btn-group col-12">
<div class="col-6 mt-1">
<button @click="$dispatch('edit-mode',{id:{{ $country->id }}})" type="button"
class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#editgrapeModal">
Edit
</button>
</div>
<div class="col-6 mt-1">
<button @click="$dispatch('delete-mode',{id:{{ $country->id }}})" type="button"
class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#editgrapeModal">
Delete
</button>
</div>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
<livewire:script.fade-alert-script />
<livewire:basedata_wines.head-table />
is to load the file.
The part
<button @click="$dispatch('delete-mode',{id:{{ $country->id }}})" type="button"
works in both options (inside the file and direct in the blade).
Thx for the help, Stephan (still Learning)
Share asked Mar 7 at 19:38 Stephan ApenbrinkStephan Apenbrink 255 bronze badges 1- Have you created a specific class that handles the basedata_wines.head-table view? – TUPKAP Commented Mar 7 at 19:47
1 Answer
Reset to default 0It looks like your issue is due to how Livewire components work when included as separate files.
When you include <livewire:basedata_wines.head-table />
, it treats head-table
as an independent Livewire component. This means that the properties q
(search input) and pagination
(dropdown value) are isolated within the head-table
component and are not affecting the parent component where your table ($this->countries
) resides.
Possible Fixes:
1. Move the Code Inside the Main Component (Recommended)
Since the search and pagination are affecting $this->countries
, they should be inside the same Livewire component managing the countries list. Instead of a separate Livewire component, put the search input and dropdown directly inside your main component's view.
2. Use $this->emit()
to Sync the Values
If you must keep head-table
as a separate component, you need to emit events so the parent component can listen for changes.
Modify head-table
Component (head-table.php
) Inside the updated()
method, emit the values to the parent:
public function updatedQ()
{
$this->emit('updateSearch', $this->q);
}
public function updatedPagination()
{
$this->emit('updatePagination', $this->pagination);
}
Modify Parent Livewire Component (BaseDataWines.php
) In the mount()
method, listen for emitted events and update the properties:
protected $listeners = ['updateSearch' => 'setSearch', 'updatePagination' => 'setPagination'];
public function setSearch($q)
{
$this->q = $q;
}
public function setPagination($pagination)
{
$this->pagination = $pagination;
}
3. Ensure Livewire is Correctly Loading the Component
Make sure your Livewire component names follow kebab-case, not snake_case. Instead of:
<livewire:basedata_wines.head-table />
Use:
<livewire:basedata-wines.head-table />
or
@livewire('basedata-wines.head-table')
Best Fix: Move search and pagination inside the main component.
Alternative: Use
$this->emit()
to communicate betweenhead-table
and the parent component.Check Component Name: Ensure correct Livewire syntax.
本文标签: laravel livewireWireModellive not working if its loaded from a fileStack Overflow
版权声明:本文标题:laravel livewire - Wire:Model.live not working if its loaded from a file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744911810a2631962.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论