I would like to display data with jquery by clicking on a link. Here's the code but it lacks some code jquery to browse data and the code twig to display them.
this is the Twig:
<a href class="list">List</a>
<div id="travels">
// display data here
</div>
<script>
$(document).ready(function () {
$(".list").click(function () {
$.ajax({
type: 'POST',
dataType: 'json',
url: Routing.generate('travel_list_ajax'),
success: function () {
// rest of code
}
});
return false;
});
});
</script>
Action
public function listAjaxAction(Request $request)
{
if ($request->isXmlHttpRequest()) {
$em = $this->getDoctrine()->getManager();
$travels = $em->getRepository('AppBundle:Travel')->findAll();
$response = new JsonResponse();
return $response->setData(array('travels'=>$travels));
}
return null;
}