将数据传递给 Laravel 中的引导模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26411186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pass data to a bootstrap modal in laravel
提问by Lucas Campos
I want to create a new register in my Database using modal, is just 5 fields, and now works fine just for the first register, when I use the button to create for other register doenst work, I just want to take the cod('id') from my equipment and pass to the modal hidden field, and this works but like I said only for the first
我想使用模式在我的数据库中创建一个新的寄存器,只有 5 个字段,现在仅适用于第一个寄存器,当我使用按钮为其他寄存器创建工作时,我只想获取 cod(' id') 从我的设备传递到模态隐藏字段,这有效,但就像我说的只是第一个
I think my problem is on my Include modal inside of foreach in the view but I didnt found the problem
我认为我的问题出在视图中 foreach 的 Include modal 上,但我没有发现问题
My store function:
我的店铺功能:
public function store()
{
$input = array_except(Input::all(), '_token');
$validation = Validator::make($input, Localizacao::$rules);
if ($validation->passes())
{
$this->localizacao->create($input);
return Redirect::route('equipamentos.index');
}
return Redirect::route('equipamentos.index')
->withInput()
->withErrors($validation);
}
My table in index view:
我在索引视图中的表:
@foreach ($equipamentos as $equipamento)
<tr>
<td>{{{ $equipamento->descricao_uso }}}</td>
<td>{{{ $equipamento->data_compra->format('d/m/Y') }}}</td>
<td>{{{ $equipamento->nota_fiscal }}}</td>
<td>{{{ $equipamento->valor_compra }}}</td>
<td>{{{ $equipamento->fornecedor }}}</td>
<td>{{{ $equipamento->taxa_depreciacao }}}</td>
<td>{{ $item }}</td>
<td> - </td>
<td>
<a href="{{ route('equipamentos.edit', array($equipamento->cod)) }}" data-toggle="tooltip" title="Editar" data-placement="top" class="legenda">
<i class="btn btn-info glyphicon glyphicon glyphicon-pencil" style="width: 41px; height:34px;"></i></a>
</td>
<td>
<button type="button" data-target="#myModal" data-toggle="modal" data-tooltip="tooltip" class="btn btn-success glyphicon glyphicon-transfer" style="width: 41px; height:34px;" title="Alterar Localiza??o"></button>
@include('localizacao.partials.form', array($equipamento, $itens_contabil))
</td>
</td>
</tr>
@endforeach
This is my modal code:
这是我的模态代码:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Fechar</span></button>
<h4 class="modal-title" id="myModalLabel">Alterar Localiza??o: {{ $equipamento->descricao_equipamento }}</h4>
</div>
<div class="modal-body" align="center">
{{ Form::open(array('route' => 'localizacao.store', 'class'=>'form-inline')) }}
{{ Form::hidden('created_by', Auth::user()->cod); }}
{{ Form::hidden('cod_equipamento', $equipamento->cod); }}
{{ Form::label('data_movimentacao', 'Data Movimenta??o:') }}
{{ Form::text('data_movimentacao', null, array('class'=>'form-control datepicker', 'style' => 'width:100%')) }}
{{ Form::label('local_atual', 'Local:') }}
{{ Form::select('local_atual', $itens_contabil, null ,array('class' => 'chosen-select')) }}
<div class="clear"><br></div>
{{ Form::label('projeto_atual', 'Projeto:') }}
{{ Form::text('projeto_atual', null, array('class'=>'form-control', 'style' => 'width:100%')) }}
{{ Form::label('funcionario_responsavel', 'Funcionário responsável:') }}
{{ Form::text('funcionario_responsavel', null, array('class'=>'form-control', 'style' => 'width:100%')) }}
{{ Form::label('motivo', 'Motivo:') }}
{{ Form::text('motivo', null, array('class'=>'form-control', 'style' => 'width:100%')) }}
<br>
<br>
<br>
</div>
<div class="modal-footer" align="center">
{{ Form::submit('Alterar Localiza??o', array('class' => 'btn btn-success')) }}
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
{{ Form::close() }}
</div>
</div>
</div>
</div>
Thx for the help!
谢谢你的帮助!
回答by NULL
Although, you are including the modal multiple time the button will call the first one because of the id
虽然,您多次包含模态按钮将调用第一个,因为 id
To solve your problem you have to use javascript or jQuery. i have used jQuery.
要解决您的问题,您必须使用 javascript 或 jQuery。我用过 jQuery。
My table in index view:
我在索引视图中的表:
@foreach ($equipamentos as $equipamento)
<tr>
<td>{{{ $equipamento->descricao_uso }}}</td>
<td>{{{ $equipamento->data_compra->format('d/m/Y') }}}</td>
<td>{{{ $equipamento->nota_fiscal }}}</td>
<td>{{{ $equipamento->valor_compra }}}</td>
<td>{{{ $equipamento->fornecedor }}}</td>
<td>{{{ $equipamento->taxa_depreciacao }}}</td>
<td>{{ $item }}</td>
<td> - </td>
<td>
<a href="{{ route('equipamentos.edit', array($equipamento->cod)) }}" data-toggle="tooltip" title="Editar" data-placement="top" class="legenda">
<i class="btn btn-info glyphicon glyphicon glyphicon-pencil" style="width: 41px; height:34px;"></i></a>
</td>
<td>
<button type="button" data-target="#myModal" data-toggle="modal" data-tooltip="tooltip" class="btn btn-success glyphicon glyphicon-transfer registerBtn" style="width: 41px; height:34px;" title="Alterar Localiza??o" data_value="{{ $equipamento->cod }}"></button>
</td>
</tr>
@endforeach
@include('localizacao.partials.form', array($itens_contabil))
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$('.registerBtn').click(function() {
$("#cod_equipamento").val($(this).attr('data_value'));
});
});
</script>
This is my modal code:
这是我的模态代码:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Fechar</span></button>
<h4 class="modal-title" id="myModalLabel">Alterar Localiza??o: {{ $equipamento->descricao_equipamento }}</h4>
</div>
<div class="modal-body" align="center">
{{ Form::open(array('route' => 'localizacao.store', 'class'=>'form-inline')) }}
{{ Form::hidden('created_by', Auth::user()->cod); }}
{{ Form::hidden('cod_equipamento', '', array('id' => 'cod_equipamento')); }}
{{ Form::label('data_movimentacao', 'Data Movimenta??o:') }}
{{ Form::text('data_movimentacao', null, array('class'=>'form-control datepicker', 'style' => 'width:100%')) }}
{{ Form::label('local_atual', 'Local:') }}
{{ Form::select('local_atual', $itens_contabil, null ,array('class' => 'chosen-select')) }}
<div class="clear"><br></div>
{{ Form::label('projeto_atual', 'Projeto:') }}
{{ Form::text('projeto_atual', null, array('class'=>'form-control', 'style' => 'width:100%')) }}
{{ Form::label('funcionario_responsavel', 'Funcionário responsável:') }}
{{ Form::text('funcionario_responsavel', null, array('class'=>'form-control', 'style' => 'width:100%')) }}
{{ Form::label('motivo', 'Motivo:') }}
{{ Form::text('motivo', null, array('class'=>'form-control', 'style' => 'width:100%')) }}
<br>
<br>
<br>
</div>
<div class="modal-footer" align="center">
{{ Form::submit('Alterar Localiza??o', array('class' => 'btn btn-success')) }}
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
{{ Form::close() }}
</div>
</div>
</div>
</div>
回答by Mahbub
You can re-use one modal in whole project to add or edit an item. You can find a sample project on Githubwhere two modals are used. One for delete which is used as a confirmation modal and another for edit or update an item.
您可以在整个项目中重复使用一个模态来添加或编辑项目。您可以在Github上找到一个示例项目,其中使用了两个模态。一个用于删除,用作确认模式,另一个用于编辑或更新项目。