交响乐 - Ajax Select

Symfony - Ajax Select

我正在做一个 Symfony 项目,我有一个产品实体,我需要一个 Ajax 搜索栏来搜索我的产品和 select 其中一些产品。问题是我有一个搜索栏,它为我提供来自数据库的实时结果,但如果我 select 产品,它应该在我的 table 中显示数据。出于某种原因,我无法在 table.

中显示 selected 结果

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/ 它会返回我的结果,但我无法在我的 table.

中显示这些 selected 产品

你漏掉了什么。 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>'); }); } } }); 您可以使用不同的方法来呈现内容,您可以重新加载整个 table 或仅重新加载您需要的行。