我正在开发应用程序,它需要引导模式中的表单,并且还动态加载表单。我面临的问题是所有页面都再次以模式加载。这里有人为此提供任何示例吗?
// Controller
public function loadJsModalForm() {
return View::make('frmModals.frmProject');
}
//index.blade
@section('Modal')
<!-- Modal -->
<div class="modal fade" id="ProjectModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content" id="frmAddProject"></div>
</div>
</div>
@stop
//形成模态
{{ Form::open(array('url'=>'projects/jsModal', 'class'=>'frmCreateProject')) }}
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Create Project</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-lg-12">
<div class="form-group">
{{ Form::label('Project Title') }}
{{ Form::text('project_title', null, array('class'=>'form-control', 'placeholder'=>'Project title')) }}
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
{{ Form::submit('Submit', array('class'=>'btn btn-primary')) }}
</div>
{{ Form::close() }}
//main.js
function openMdoal(link, div){
$(link).on('click', function(e){
e.preventDefault();
$('#'+div).html('<div style="text-align:center;"><img src="../img/spinner.gif"/><br/><br/><h3 class="text-primary">Loading Form...</h3></div>');
$(div).load(
$(this).attr('href'),
function(response, status, xhr) {
if (status === 'error') {
//console.log('got here');
$(div).html('<p>Sorry, but there was an error:' + xhr.status + ' ' + xhr.statusText+ '</p>');
}
return this;
}
);
});
}
$(document).ready(function(){
openMdoal('.addProjectModal', '#frmAddProject');
});
//路线
Route::get('projects/jsModal', 'ProjectsController@loadJsModalForm');
谢谢
最佳答案
这是一个迟到的答案,但我想动态加载所有模式,而不是将它们放在标记中,所以我为每个模式创建了 View - 用于删除、编辑等,只是内部部分(内容)
在index.blade.php上我有一个模态框架:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content load_modal"></div>
</div>
</div>
用于不同操作的和按钮:
<button type="button" class="btn btn-danger" data-toggle="modal" data-id="{{ $id }}" data-post="data-php" data-action="delete">Delete </button>
然后调用不同模态的按钮单击内容,为每个模态提供单独的 View ,例如 modal_delete.blade.php 如下所示:
<div class="modal-body">
<em>Are you sure that you want to delete</em>?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-danger photo-delete" data-slug="">Delete</button>
</div>
在 Controller 中,我为所有操作创建了一个方法:
/**
* load modals dynamically - for edit, delete,create
* GET /folder/{id}/load-{action}
*
* @param string $action
* @param int $id
* @return Response
*/
public function loadModal($id, $action)
{
//return view for specified action
//if action is delete, call this view, etc...
return View::make('admin.photos.modal_delete')->render();
}
在 route :
Route::get('photos/{id}/load-{action}','YourController@loadModal');
最后用 JS 加载内容:
$('button').on('click', function(){
var this_id = $(this).attr('data-id');
var this_action = $(this).attr('data-action');
if(this_action == 'edit'){
$.get( base_url + this_id + '/load-' + this_action, function( data ) {
$('#myModal').modal();
$('#myModal').on('shown.bs.modal', function(){
$('#myModal .load_modal').html(data);
});
$('#myModal').on('hidden.bs.modal', function(){
$('#myModal .modal-body').data('');
});
});
}
});
这样我的 html 就更干净了。我希望这也会对某人有所帮助 - 我愿意接受建议:)
关于jquery - 使用 Laravel 在 Bootstrap 模式中动态加载表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25241802/