Symfony2实现输入文本并重新加载元素列表


Symfony2 implement a input text and reload a list of elements

我是symfony2的新手,我需要一些指南来实现一件事。

我有一个输入文本和一个包含数据库数据的表格。

我希望当用户在输入文本中键入文本时,使用数据库中的新数据重新加载表。

我希望从控制器调用的函数:

public function reloadCountryAction($name){
    $em = $this->getDoctrine()->getEntityManager();
    $qb = $em->createQueryBuilder();
    $qb->select("*")
        ->from("COUNTRY","p")
        ->where("p.COUNTRYNAME like '%:identifier%'")
        ->setParameter('identifier', $name);
    return $qb->getArrayResult();
}

输入文本,用户将在其中键入要搜索的项目:

<input type="search" id="searchCountry" >

首次馈送表的函数:

public function countryListAction() {
    $usr = $this->get('security.context')->getToken()->getUser();
    $allCountry = $this->getDoctrine()
            ->getRepository('backendentityBundle:Country')
            ->findAll();
    return $this->render('backendcountryBundle:Default:countryFirst.html.twig', array('sessionname' => $usr->getUsername(),
                'allCountry' => $allCountry));
}

和我要重新加载的表:

<table id="selectableTableCountry">
    {% for country in allCountry %}
        <tr id="rowtable" class="ui-widget-content">
            <td>
                <p class="txtCountryName">{{ country.countryname}}</p>
            </td>
        </tr>
    {% endfor %}
</table>

谢谢

这是JavaScript问题,而不是symfony。您应该使用 ajax 调用从数据库中重新加载数据,或者只是在元素上触发取消焦点时发布它。

这不是一个关于symfony的问题,而是我认为javascript的问题。您应该侦听(例如onkeyup)输入 #searchCountry 并执行ajax来路由调用reloadCountryAction并呈现新的表<table id="selectableTableCountry">...</table>。只需在当前 DOM 中更改它即可。