Laravel 5.1将数据从视图传递到模态 [英] Laravel 5.1 pass data from view to modal
问题描述
如何将数据从刀片视图传递到模式对话框: 例如 : 我从控制器传递了$ user来查看:
How can I pass data from blade view to modal dialog : For example : I pass $user from controller to view :
$user = User::findOrFail($id);
return view('user.show')->withUser($user);
接下来,我想通过这样的按钮将此数据$ user传递给此视图中包括的模式:
Next, I want to pass this data $user to a modal included in this view via a button like this :
@include('user.edit',$user)
;
在模态中,我可以设置$ user的值(例如:{!! $ user-> lastname !!})来编辑它们.
and there in the modal I can set $user's values (like this : {!! $user->lastname !!} ) to edit them for example.
请帮助我:)
推荐答案
尝试这种方式.我正在使用a
标记,但是该解决方案也应该可以通过按钮使用.
Try out this way. I am using a
tag, but the solution should work for you as well with button.
<a
href="#"
data-target="yourModalId"
data-toggle="modal"
data-email="{{ $user->email }}"
data-username="{{ $user->username }}"
>
Edit
</a>
jQuery代码:
$('#yourModalId').on('show', function(e) {
var link = e.relatedTarget(),
modal = $(this),
username = link.data("username"),
email = link.data("email");
modal.find("#email").val(email);
modal.find("#username").val(username);
});
使用find
方法中传递的id
在模式窗口内创建输入字段.
Create the input fields inside the modal window with the id
that are passed in find
method.
这会将传递的值放在模式窗口内的输入字段中.
That will put the values passed in the input fields inside modal window..
这篇关于Laravel 5.1将数据从视图传递到模态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!