【发布时间】:2019-03-12 09:36:31
【问题描述】:
我正在处理一个 Symfony 项目,其中我有一个产品实体,我需要一个 Ajax 搜索栏来搜索我的产品并选择其中一些。问题是我有一个搜索栏,它可以为我提供数据库的实时结果,但如果我选择产品,它应该在我的表中显示数据。由于某种原因,我无法在表格中显示选定的结果。
js
$('.js-data-example-ajax').select2({
ajax: {
url: '/product/api/search',
dataType: 'json',
}
});
控制器
public function viewActionSearch(Request $request)
{
$query = $request->get('term');
$result = [
'results' => [],
];
if ($query !== null){
$products = $this->productRepository->getSearchList($query);
$result['results'];
foreach ($products as $product) {
$result['results'][] = [
'id' => $product['id'],
'text' => $product['name'],
];
}
} else {
$products = $this->productRepository->getResultList(1);
foreach ($products as $entity) {
$result['results'][] = [
'id' => $entity['id'],
'text' => $entity['name'],
];
}
}
return new JsonResponse($result);
}
产品列表
public function getPage(Request $request)
{
$products = $this->productRepository->getAllProducts($currentPage);
return $this->render(
'@app_bar/Product/productList.twig',
[
'products' => $products,
]
);
}
树枝
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<!-- select2 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/gh/ttskch/select2-bootstrap4-theme@master/dist/select2-bootstrap4.min.css" rel="stylesheet">
<div class="container mt-5">
<form>
<div class="form-group">
<select class="js-data-example-ajax form-control"></select>
</select>
</div>
</form>
</div>
<table class="table table-hover table-responsive">
<thead>
<tr>
<th>id</th>
<th>Title</th>
<th>SKU</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for product in products %}
<tr>
<td>
{{ product.id }}
</td>
<td>
{{ product.name }}
</td>
<td>
{{ product.sku }}
</td>
<td>
<a href="{{ path('app_product_getproduct', {'id': product.id}) }}" class="btn btn-success btn-sm" >
<span class="glyphicon glyphicon-pencil"></span>
</a>
<a href="{{ path('app_product_delete', {'id': product.id}) }}" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure?')">
<span class="glyphicon glyphicon-trash"></span>
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
如果我访问路线 /product/api/search/ 它会返回我的结果,但我无法在我的表格中显示这些选定的产品。
【问题讨论】:
-
但是你没有做任何事情来显示你的表格中的数据。
-
@u_mulder 添加了表格,但表格的结果不是从搜索 api 获得的
-
ProductList::getPage()在这里如何适应?这是另一个控制器操作吗? -
另外值得注意的是,使用服务器端呈现的 HTML 响应 AJAX 请求,然后将其插入 DOM 是可以的;例如:
$.get('some/html', (response) => $('.result').html(response))——但在这种情况下你不会使用JsonResponse -
@DarraghEnright 我想用 select2 ajax 调用中的选择产品替换所有结果的表格,但我现在不知道如何用一个结果替换结果
标签: php ajax symfony jquery-select2