我想将standard blade view
的id传递给livewire component view
。
我有一个编辑用户按钮在刀片文件,所以当用户点击按钮bootstrap modal
(这是一个livewire component view
)与id将打开。
//一些ajax来呈现带有编辑按钮的表
success: function(response) {
$('tbody').html("");
$.each(response, function(key, item) {
str += '<tr>';
//blahh...
str += '<a href="#" id="' + item.id + '" wire:click="edit({{ '+item.id+' }})" class="text-info mx-1 editIcon" data-toggle="modal" data-target="#editUserModal"></a>';
//blahh...
str += '</tr>';
})
$('tbody').append(str);
)};
如果我坚持使用jquery,我可以成功地打开一个模态与数据进行编辑。但是我想把modal作为livewire组件打开。
<<p>livewire视图/strong><div class="modal fade" id="editUserModal" tabindex="-1" role="dialog" aria-hidden="true">
//blahh
</div>
<<p>livewire组件/strong>public function render()
{
//some logic here to open modal with id clicked, so I can do something as below
$user = User::where('id', $id)->first();
$this->user_id = $id;
$this->first_name = $student->student_first_name;
return view('livewire.user.edit');
}
任何建议都会有帮助的。我不想把整个项目移植到livewire,我只是想在某些地方使用livewire组件,因为它简单,代码少
您可以使用$emit
或Livewire.emit
从表单击发出事件。
success: function(response) {
$('tbody').html("");
$.each(response, function(key, item) {
str += '<tr>';
//blahh...
str += '<a href="#" id="' + item.id + '" wire:click="$emit({{ openmodal'+item.id+' }})" class="text-info mx-1 editIcon" data-toggle="modal" data-target="#editUserModal"></a>';
//blahh...
str += '</tr>';
})
$('tbody').append(str);
)};
听你的模态:
public bool $open = false;
public ?int $userId = null;
protected $listeners = ['openmodal' => 'openModal'];
public openModal(int $userId)
{
$this->userId = $userId;
// you can load your user data here
$user = User::findOrFail($userId);
}
public function render()
{
return view('livewire.user.edit');
}