Php 如何从一行中获取数据并移动到一个模式?拉威尔5.4
我有一个表,其中包含来自我的数据库的listo产品,其中包含ID、名称、描述以及其他数据类型。我创建了一个按钮,该按钮将调用一个模式来显示有关产品的更多详细信息,但是该模式始终显示表中第一个产品的详细信息,而不是与之相关的ID 我的桌子: 我的表格代码:Php 如何从一行中获取数据并移动到一个模式?拉威尔5.4,php,laravel,laravel-5,Php,Laravel,Laravel 5,我有一个表,其中包含来自我的数据库的listo产品,其中包含ID、名称、描述以及其他数据类型。我创建了一个按钮,该按钮将调用一个模式来显示有关产品的更多详细信息,但是该模式始终显示表中第一个产品的详细信息,而不是与之相关的ID 我的桌子: 我的表格代码: <table class="table table-striped table-bordered" id="table1" class="table table-bordered">
<table class="table table-striped table-bordered" id="table1" class="table table-bordered">
<thread>
<th><center>Imagem</center></th>
<th>SKU</th>
<th><center>Produto</center></th>
<th>Custo</th>
<th>Preço</th>
<th>Atualização</th>
<th>Status</th>
<th>Estoque</th>
<th>Distruibuidor</th>
<th>Categoria</th>
<th id="acoes">Ações</th>
</thread>
@foreach($product as $prod)
<thread>
<tbody>
<td><img src="{{$prod->image->erp_image}}" style="width: 50px; height: 50px;" alt="" id="ProdImage"></td>
<td>{{$prod->erp_model}}</td>
<td>{{$prod->description->erp_name}}</td>
<td>R$ {{$prod->erp_cost}}</td>
<td>R$ {{$prod->erp_price}}</td>
<td>{{ $prod->erp_modifieddate}}</td>
<td style="max-width: 45px">
@if($prod->status == 1)
<span class="label label-success">Ativo</span>
@else
<span class="label label-danger">Inativo</span>
@endif
</td>
<td>{{ $prod->erp_quantity}}</td>
<td>@if($prod->erp_distributor == 'A')
<span class="label label-default">Aldo</span>
@elseif($prod->erp_distributor == 'AN')
<span class="label label-default">All Nations</span>
@elseif($prod->erp_distributor == 'H')
<span class="label label-default">Hayamax</span>
@elseif($prod->erp_distributor == 'O')
<span class="label label-default">Oderço</span>
@elseif($prod->erp_distributor == 'R')
<span class="label label-default">Rico Peças</span>
@endif
</td>
<td>
@foreach($prod->category as $category)
{{$category->erp_name}}
@endforeach
</td>
<td>
<button href="#" data-toggle="modal" data-target="#view" class="btn btn-default"><i class="glyphicon glyphicon-eye-open"></i></button>
<div class="modal fade" id="view" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Detalhes</h4>
</div>
<div class="modal-body">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Informações do produto</h3>
</div>
<div class="panel-body">
<h5>Nome</h5>
<input class="form-control" type="text" placeholder="{{$prod->description->erp_name}}" readonly>
<h5>SKU</h5>
<input class="form-control" type="text" placeholder="{{$prod->erp_model}}" readonly>
<h5>EAN</h5>
<input class="form-control" type="text" placeholder="{{$prod->erp_ean}}" readonly>
<h5>NCM</h5>
<input class="form-control" type="text" placeholder="{{$prod->erp_ncm}}" readonly>
<h5>CST</h5>
<input class="form-control" type="text" placeholder="{{$prod->erp_cstid}}" readonly>
<h5>Marca</h5>
<input class="form-control" type="text" placeholder="{{$prod->brand->erp_name}}" readonly>
<h5>Categoria</h5>
@foreach($prod->category as $category)
<input class="form-control" type="text" placeholder="{{$category->erp_name}}" readonly>
@endforeach
<h5>Distribuidor</h5>
<input class="form-control" type="text" placeholder="{{ App\Helpers\ProductDistributorHelper::$distributor[$prod->erp_distributor]}}" readonly>
</div>
</div>
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">Informações dos preços</h3>
</div>
<div class="panel-body">
<h5>Preço custo</h5>
<input class="form-control" type="text" placeholder="R$ {{$prod->erp_cost}}" readonly>
<h5>Preço venda</h5>
<input class="form-control" type="text" placeholder="R$ {{$prod->erp_price}}" readonly>
</div>
</div>
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">Informações de peso e medida</h3>
</div>
<div class="panel-body">
<h5>Peso</h5>
<input class="form-control" type="text" placeholder="{{$prod->erp_weight}} kg" readonly>
<h5>Comprimento</h5>
<input class="form-control" type="text" placeholder="{{$prod->erp_lenght}} cm" readonly>
<h5>Largura</h5>
<input class="form-control" type="text" placeholder="{{$prod->erp_width}} cm" readonly>
<h5>Altura</h5>
<input class="form-control" type="text" placeholder="{{$prod->erp_height}} cm" readonly>
</div>
</div>
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title">Informações de estoque</h3>
</div>
<div class="panel-body">
<h5>Quantidade</h5>
<input class="form-control" type="text" placeholder="{{$prod->erp_quantity}}" readonly>
</div>
</div>
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title">Informações técnicas e descrição</h3>
</div>
<div class="panel-body">
<h5>Descrição</h5>
<p></p>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">OK</button>
</div>
</div>
</div>
</div>
</div>
</td>
<style>
td{
max-width: 100px;
min-width: 80px;
}
</style>
</tbody>
</thread>
@endforeach
</table>
有什么建议吗?每次都会显示第一个模态,因为每个模态都有相同的id视图 所以,在代码中做以下更改,它就会起作用
<button href="#" data-toggle="modal" data-target="#view_{{$prod->id}}" class="btn btn-default"><i class="glyphicon glyphicon-eye-open"></i></button>
<div class="modal fade" id="view_{{$prod->id}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
这将为每个模式创建不同的ID
这不是让它工作的好方法
对于每一行,您应该只有一个模式,并使用模式显示按钮的jQuery on click事件动态更改数据
因为如此多的记录会使HTML文件变大,而且性能会变慢。我认为这不是在foreach循环中呈现模式的好方法。试想一下,如果你有很多产品,那么你的模式代码也会根据你的产品数量呈现 这里有一个我自己使用的解决方案。你可以试试这个 步骤1: 在按钮中添加一个名为“modal global”的类,并将特定URL添加到href属性中 例如:
<a href="your/url/{{$prod->id}}" class="btn btn-default modal-global"><i class="glyphicon glyphicon-eye-open"></i></a>
<div class="modal fade" id="modal-global">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<div class="text-center">
<i class="fa fa-3x fa-refresh fa-spin"></i>
<div>Please wait...</div>
</div>
</div>
</div>
</div>
public function viewProduct($id)
{
$product = Product::findOrfail($id);
return "
<table class='table table-hover'>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td>{$product->id}</td>
<td>{$product->name}</td>
<td>{$product->description}</td>
</tr>
</tbody>
</table>
";
}
步骤4:
下面是控制器方法的一个示例,它将返回带有特定产品详细信息的html内容,我们的ajax将接收此html内容作为响应
例如:
<a href="your/url/{{$prod->id}}" class="btn btn-default modal-global"><i class="glyphicon glyphicon-eye-open"></i></a>
<div class="modal fade" id="modal-global">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<div class="text-center">
<i class="fa fa-3x fa-refresh fa-spin"></i>
<div>Please wait...</div>
</div>
</div>
</div>
</div>
public function viewProduct($id)
{
$product = Product::findOrfail($id);
return "
<table class='table table-hover'>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td>{$product->id}</td>
<td>{$product->name}</td>
<td>{$product->description}</td>
</tr>
</tbody>
</table>
";
}
公共功能视图产品($id)
{
$product=产品::findOrfail($id);
返回“
身份证件
名称
描述
{$product->id}
{$product->name}
{$product->description}
";
}
您为每一行创建模式的方法不太好,因为这会降低页面性能,减缓页面加载速度,增加页面大小,并且加载需要很长时间。对于这一点,有一个简单的解决方案
查看产品
或任何其他要保留的id。使用此id,我们将打开modal
<div class="modal fade" id="view-product" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<!-- Your Code goes here-->
</div>
</div>
查看产品
类上添加click事件,并使用AJAX通过我们在每个产品的链接中传递的数据id
获取产品详细信息。检查下面。在ajax响应之后,您可以为要设置值的每个字段分配值
$('.view-product').on("click", function(event) {
$("#view-product").modal('show');
var product_id = $(this).data('id');
$.ajax({
url: "url/to/product/details/" + product_id,
type: 'GET',
dataType: 'json',
}).done(function(response) {
$("#product_name").val(response.product_name);
// Assign values to other fields
});
});
<button href="#" data-toggle="modal" data-target="#view-{{$prod->id}}" class="btn btn-default"><i class="glyphicon glyphicon-eye-open"></i></button>
<div class="modal fade" id="view-{{$prod->id}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
前面的问题是什么?请检查答案,我建议了更好的方法。Pandhi的建议非常有效:使用Jquery动态加载modals。如果您想坚持使用此解决方案,则aria标签存在问题。阅读此堆栈输入:我使用了您的代码,但传递了错误的“id”变量。我的“id”被称为“产品id”,而不仅仅是“id”。我把它修好了,它成功了。顺便说一句:使用ajax很容易,并且可以放置