2
私は、ページをリロードせずにチェックボックスを使ってユーザを有効/無効にする方法を理解しようとしています。Symfony:Ajax urlにパラメータを渡す
index.html.twig
<table>
<thead>
<tr>
<th>UserName</th>
<th>Enabled</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<td><a href="{{ path('user_show', {'id': user.id}) }}">
{{ user.username }}</a>
</td>
<td><input id="user_enable_{{ user.id }}" onclick="enabledChange({{ user.id }})"
type="checkbox" {% if user.enabled %}checked{% endif %}/>
</td>
{% endfor %}
</tbody>
</table>
<script>
var changePath = {{ path('user_enable_change', {'id': user.id}) }};
function enabledChange(id)
{
var value = $(this).val();
console.log('value: ' + value);
$.ajax({
type: "POST",
url: changePath,
async: true,
data: { },
success: function() {
console.log('success');
}
});
}
</script>
UserControllerで
/**
* @Route("/enable/{id}", name="user_enable_change")
*/
public function userDisableAction(User $user) {
if($user->isEnabled()){
$user->setEnabled(false);
}else {
$user->setEnabled(true);
}
try {
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
}
catch(\Exception $e) {
return new JsonResponse('error');
}
return new JsonResponse('success');
}
私はenabledChange機能するためのユーザーIDに対応する設定と変更が依存状態を確認することができますどのように質問
ユーザーst食べた?
現在のコードではどのような問題がありますか? –
@alokpatelエラーが発生しました:パラメータ%user.idを{user%}のユーザの%%の外にchangePathに設定しているため、 '変数'ユーザが@ Admin/UserPanel/index.html.twigに存在しません – blahblah
'内部で 'var changePath'関数を定義し、渡されたパラメータを使用しました。 –