我正在关注DevsBrasil'Laravel tutorial,尝试裁剪用户形象。
和模态总是显示..
这是一个控制器:
public function editUsers($userID) {
$user = Sentry::findUserById($userID);
$data = array();
$data['image'] = Session::get('img');
$data['modal'] = (Session::get('modal') == null ? 'false' : 'true');
$group_p = $user->getGroups();
$groups_p = [];
foreach (Sentry::findAllGroups() as $group) {
$groups_p [$group->id] = $group->name;
}
return View::make('dashboard.users.edit')
->with('user',$user)
->with('group_p',$group_p)
->with('groups_p',$groups_p)
->with('data',$data);
}
public function updateUsers($userID) {
$user = Sentry::findUserById($userID);
return View::make('dashboard.users.edit')
->with('user',$user);
}
public function UploadImg($userID)
{
if (Input::hasFile('image')) {
$user = Sentry::findUserById($userID);
$image_name = $image->getClientOriginalName();
$image = Input::file('image');
$filename = $user->username . '.' . $image->getClientOriginalExtension();
$path = public_path('img/users/' . $filename);
$int_image = Image::make($image->getRealPath());
$int_image->resize(568,null, function($constraint){
$constraint->aspectRatio();
});
$int_image->save($path);
$user->image = 'img/users/'.$filename;
Session::put('modal','ture');
}else{
}
return Redirect::back();
}
public function CropImg()
{
return Redirect::back();
}
查看:
<!-- Tab content -->
<div id="tabImage" class="tab-pane innerAll">
<h4 class="innerTB">
Update {{ $user->name }} image.
</h4>
<div class="row">
<div class="col-md-12 ">
<div class="bootbox modal fade in" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: block;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Crop Image</h4>
</div>
<div class="modal-body">
{{Form::open(array('route' => 'crop_user_image_post', 'onsubmit' => 'return checkCoords' ,'class'=>'form-horizontal'))}}
{{HTML::image($data['image'], '' , ['id' => 'crop'] )}}
<input type="hidden" id="src" name="src" value="{{ $data['image'] }}" >
<input type="hidden" id="x" name="x" />
<input type="hidden" id="y" name="y" />
<input type="hidden" id="w" name="w" />
<input type="hidden" id="h" name="h" />
</div>
<div class="modal-footer">
<button data-bb-handler="Done" type="button" class="btn btn-success">Success!</button>
</div>
{{ Form::close() }}
</div>
</div>
</div>
{{Form::open(array('route' => 'update_user_image_post', 'method' => 'post','files'=>'true','class'=>'form-horizontal') , $user->id)}}
{{ HTML::image($user->img, $user->username, array('class' => 'img-circle' )) }}
{{Form::file('image')}}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">Upload Image <i class="fa fa-check"></i></button>
</div>
</div>
{{ Form::close() }}
<input type="hidden" id="modal" value="{{$data['modal']}}" />
</div>
</div>
</div>
<!-- // Tab content END -->
JS代码:
我使用来自控制器的id(#modal)值隐藏输入
$data['modal'] = (Session::get('modal') == null ? 'false' : 'true');
<script>
var modal;
if ($('#modal').val() == 'true' ) {
modal = true;
}else{
modal = false;
}
$(document).ready(function(){
$('#crop').Jcrop({
aspectRatio: 1,
onSelect: atualizaCoordenadas
});
$('#myModal').modal({show: modal});
});
function atualizaCoordenadas(c){
$('#x').val(c.x);
$('#y').val(c.y);
$('#w').val(c.w);
$('#h').val(c.h);
}
function checkCoords(){
if (parseInt($('#w').val())) return true;
alert('Error');
return false;
}
</script>
答案 0 :(得分:0)
以下是模态不断显示的原因。
您在模态div中设置了style="display:block"
。内联样式优先于其他css样式。我没有看过Bootstrap源代码,但是有可能使用css类而不是内联样式设置模态可见性。
见这里(滚动到最右端):
<div class="bootbox modal fade in" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: block;">
您需要从div中删除style="display:block"
,如下所示:
<div class="bootbox modal fade in" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
这是div属性的一个更易读的版本供您比较:
版本不正确
<div
class="bootbox modal fade in"
id="myModal"
tabindex="-1"
role="dialog"
aria-labelledby="myModalLabel"
aria-hidden="true"
style="display: block;"> <!--The issue is this `style` attribute-->
正确版本
<div
class="bootbox modal fade in"
id="myModal"
tabindex="-1"
role="dialog"
aria-labelledby="myModalLabel"
aria-hidden="true">