【问题标题】:Symfony - Ajax SelectSymfony - Ajax 选择
【发布时间】: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) =&gt; $('.result').html(response))——但在这种情况下你不会使用JsonResponse
  • @DarraghEnright 我想用 select2 ajax 调用中的选择产品替换所有结果的表格,但我现在不知道如何用一个结果替换结果

标签: php ajax symfony jquery-select2


【解决方案1】:

你在这里遗漏了一些东西。 Symfony 不像 vue.js 或类似的前端框架那样工作。您正在做的是呈现服务器端请求,之后,您只需通过 AJAX 获取数据,而您对这些数据什么也不做。 jQuery 需要有关如何处理从服务器获得的数据的说明。您总是可以将 Symfony 与某些前端框架一起使用,但您需要了解服务器端呈现您的 twig 模板和前端框架呈现它的时间的区别。

http://api.jquery.com/jquery.ajax/

提示: $('.js-data-example-ajax').select2({ ajax: { url: '/product/api/search', dataType: 'json', success: function (data) { $.each(response, function () { $('#mytable').append('<tr><td>' + this.product_name + '</td><td>' + this.product_price + '</td></tr>'); }); } } }); 您可以使用不同的方法来呈现内容,您可以重新加载整个表格或只重新加载您需要的行。

【讨论】:

  • 好的,你能帮我如何处理这些数据吗?
  • 您必须在收到服务器的响应后呈现表格。我将编辑我的答案。
  • 您使用 jQuery 选择表,然后根据从服务器获取的数据呈现该表。哪一部分不清楚?
  • 有一个包含所有结果的表,它从控制器获取结果。还有一个 ajax 搜索栏,这些结果是 json 格式的,我可以通过它进行搜索。但是一旦我选择了一个产品,我就无法将它呈现给 symfony 控制器。我不明白你用 jquery 选择表并根据从服务器获得的数据呈现该表是什么意思。
  • 您收到想要在浏览器上显示的产品的 JSON 列表?这是正确的吗?
猜你喜欢
  • 1970-01-01
  • 2015-03-30
  • 2017-01-16
  • 2018-09-12
  • 2016-11-06
  • 2016-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多