當我單擊複選框時,嘗試更新數據庫的「存檔」值。在搜索該網站和谷歌後,我設法做到了。 但是,通過添加第二篇文章,它只適用於第一篇文章。如果您有任何想法,我將不勝感激! 謝謝 我已安裝fosjsroutingbundle。Symfony 2 - 單擊更新數據庫
這是我的控制器:
public function archiveAction($id, Request $request)
{
if ($request->isXmlHttpRequest()) {
$em = $this->getDoctrine()->getManager();
$glasse = $em->getRepository('SosMontagesBundle:Glasse')->find($id);
$glasseArchiveStat = $glasse->getArchive();
if ($glasseArchiveStat == false) {
$glasse->setArchive(true);
$em->flush();
} else {
$glasse->setArchive(false);
$em->flush();
}
return new Response('d');
}
}
我的路線:
sos_montage_archive:
path: /admin/archive/{id}
defaults: { _controller: SosMontagesBundle:Admin:archive }
requirements:
id: \d+
options:
expose: true
我的觀點:
{% extends '@SosMontages/layout.html.twig' %}
{%嵌段含量%}
<div class="col-md-6 col-md-offset-3">
<h2>Liste des lunettes</h2>
<table class="table table-striped">
<thead>
<tr>
<th>Id</th>
<th>Marque - Model</th>
<th>Description</th>
<th>Archive</th>
<th>Ordre</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for article in pagination %}
<tr {% if loop.index is odd %}class="color"{% endif %}>
<td>{{ article.id }}</td>
<td>{{ article.name ~ ' - ' ~ article.brand }}</td>
<td>{{ article.content }}</td>
{% if article.archive == false %}
<td><input type="checkbox" name="{{ article.id }}" id="archive"></td>
{% else %}
<td><input type="checkbox" name="{{ article.id }}" id="archive" checked></td>
{% endif %}
<td></td>
<td><a href=""><i class="fa fa-pencil fa-2x" aria-hidden="true"></i></a>
<a href=""><i class="fa fa-trash-o fa-2x" aria-hidden="true"></i></a></td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="navigation">
{{ knp_pagination_render(pagination) }}
</div>
</div>
{%端塊%} { %阻止JavaScript%}
<script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
<script src="{{ path('fos_js_routing_js', { callback: 'fos.Router.setData' }) }}"></script>
<script>
$(document).ready(function() {
$("#archive").click(function() {
var id = $("#archive").attr('name');
var DATA = 'sentValue=' + id;
$.ajax({
type: "POST",
url: Routing.generate('sos_montage_archive', { id: id }),
data: DATA,
cache: false,
success: function (data) {
alert("database has been updated");
}
});
});
});
</script>
{%端塊%}您的複選框
這是基本的HTML ** **。你不能有多個具有相同ID的元素。給這些複選框分配一個css類,並將你的'click'動作綁定到那個複選框上,而不是這個ID。 – yivi