使用控制器的LiveWire组件编辑用户数据用户数据

发布于 2025-02-04 03:31:39 字数 1355 浏览 5 评论 0原文

我想将ID从标准刀片视图传递给livewire组件视图

我在Blade文件中有一个编辑用户按钮,因此当用户单击按钮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,我可以成功打开一个使用数据进行编辑的模态。但是我想打开模态作为LiveWire组件。

LiveWire View

<div class="modal fade" id="editUserModal" tabindex="-1" role="dialog" aria-hidden="true">
    //blahh
</div>

LiveWire组件

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组件用于某些地方,因为它的简单和较少的代码

I want to pass the id from the standard blade view to the livewire component view.

I have an edit user button in the blade file, so when the user clicks on the button bootstrap modal (which is a livewire component view) with id will open.

//some ajax to render the table with an edit button

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);
)};

if I stick to jquery I can successfully open a modal with data to edit. but I want to open the modal as a livewire component.

livewire view

<div class="modal fade" id="editUserModal" tabindex="-1" role="dialog" aria-hidden="true">
    //blahh
</div>

livewire component

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');
}

any suggestion will be helpful.
I don't want to port the whole project to livewire I just want to use the livewire component for some places due to its simplicity and lesser code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南薇 2025-02-11 03:31:39

您可以发出事件从表中从表中单击使用$ 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');
}

You can emit an event from the table click using $emit or 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);
)};

And listen on your modal:

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');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文