Hi everyone I come to you because I have a small problem, I create a dynamic comments system in ajax and I would like to edit each comment except that when I do it it changes me the last and not the element clicked
I've missed that things that I think
I have not tried anything yet I do not see how to do it
My code :
js:
$('#edit-comments').hide();
$('#add-comments').on('click', function (e) {
e.preventDefault();
$.ajax({
url: $(this).attr('action'),
type: 'POST',
data: $('#formsComments').serialize(),
dataType: 'json',
success: function (data) {
$('#comments_commentsContent').val('');
$('#content-comments').append(data.content);
},
error: function (data) {
console.log(data);
if(data.status === 500) {
$('#errors-comments').html('<div class="error">Vous n\'avez pas ajouté de commentaires\n</div>')
}
}
});
});
$('.comments-content').on('click', function (e) {
e.preventDefault();
$('#add-comments').hide();
$('#edit-comments').show();
var idEdit = $(this).attr('id');
var idContent = idEdit.split('-');
var res = $('p#comments-content-'+idContent[2]).text();
console.log(idContent);
$('#comments_commentsContent').val(res);
$('#edit-comments').on('click', function (e) {
e.preventDefault();
$.ajax({
url: '/article/commentaires/'+idContent[2]+'/edit/'+idContent[3],
type: 'POST',
data: $('#formsComments').serialize(),
dataType: 'json',
success: function (data) {
console.log(data);
$('#comments_commentsContent').val('');
$('#add-comments').show();
$('#edit-comments').hide();
$('#comments-content-'+idContent[2]).html(data.content.message);
},
error: function (data) {
console.log(data);
}
});
});
});
My view twig :
{% for comment in article.comments %}
<div class="media" id="media">
<div class="media-body">
<div class="media-heading">
<h4>{{ comment.userComments.username }}</h4>
<span class="time">{{ comment.createdAt | date("d/m/Y à H:i") }}</span>
{% if app.user %}
{% if comment.userComments.id == app.user.id %}
<a href="{{ path('front_article_edit_comments', {'id': comment.id, 'user': comment.userComments.id}) }}#comments" class="comments-content" id="edit-comments-{{ comment.id }}-{{ comment.userComments.id }}" >
<i class="fa fa-pencil" aria-hidden="true"></i>
</a>
{% endif %}
{% endif %}
{% if is_granted('ROLE_ADMIN') %}
<a href="{{ path('front_article_delete_comments', {'id': comment.id}) }}#comments" onclick="return confirm('Vous voulez vous vraiment supprimer ce commenaitre ?');">
<i style="padding-left: 10px; color: #CBBBA3;" class="fa fa-times" aria-hidden="true"></i>
</a>
{% endif %}
</div>
<p id="comments-content-{{ comment.id }}">{{ comment.commentsContent|nl2br }}</p>
</div>
</div>
{% endfor %}
new comments :
if($request->isXmlHttpRequest() && $form->isValid()) {
$user = $this->getUser();
$comments->setUserComments($user);
$comments->setArticleComments($articles);
$em->persist($comments);
$em->flush();
$html = $this->renderView('articles/comments.html.twig', ['comment' => $comments]);
return $this->json(['content' => $html]);
}
edit comments :
if($request->isXmlHttpRequest() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->flush();
return $this->json(['content' => [
'message' => $comments->getCommentsContent(),
'user' => $comments->getUserComments()->getId(),
'id-comments' => $comments->getId(),
]]);
}
When I want to edit the first comment or the second it does not work.
Related
I am new at Symfony.
I have 2 entities Position and Candidat with relationship as in a position we can have multiple candidats.
Now I have a dropdown populate by positions and below a table with a list of candidat.
I want filter list of candidat when I select a position using the dropdown.
below the codes:
Controller
/**
* #Route(name="filter",path="/filter")
* #param Request $request
*/
public function test2Action(Request $request)
{
$em = $this->getDoctrine()->getManager();
$positions = $em->getRepository('AppBundle:Position')->findAll();
$candidats = $em->getRepository('AppBundle:Candidat')->findAll();
if($request->request->get('myselect')){
$val = $request->request->get('myselect');
$candidats = $em->getRepository('AppBundle:Candidat')->find($val);
return $this->render('postionsearch.html.twig',
array( 'candidats' => $candidats , 'positions' => $positions ) );
}
return $this->render('postionsearch.html.twig',
array( 'candidats' => $candidats , 'positions' => $positions ) );
}
twig:
<body>
<div class="container">
<div class="row">
<div class="col-xs-12">
<select name="position" id="selectpos">
<option ></option>
{% for position in positions %}
<option value="{{ position.id }}">{{ position.titre }}
</option>
{% endfor %}
</select>
</div>
</div>
</div>
</body>
<div id="ajax-results">Results : </div>
{% if candidats is defined %}
<table id="dataTable">
<tbody>
{% for candidat in candidats %}
<tr>
<td>{{candidat.id}}</td>
<td>{{candidat.nom}}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
JS:
<link rel="stylesheet" href="{{
asset('assets/bootstrap/css/bootstrap.min.css') }}" />
<script src="{{ asset('assets/jquery/jquery.min.js') }}"></script>
<script src="{{ asset('assets/bootstrap/js/bootstrap.min.js') }}"></script>
<script>
$(document).ready(function(){
console.log("jQuery is ready");
$("#selectpos").change(function(){
var value= $( "#selectpos option:selected" ).val();
$.ajax({
url:'{{ (path('filter')) }}',
type: "POST",
dataType: "json",
data: {
"myselect": value
},
async: true,
success: function (data)
{
console.log(data)
$('#ajax-results').html(data.output);
},
error: function (err)
{
console.log(err.statusText);
}
});
return false;
})
});
</script>
You need to pass filter parameter to your action:
/**
* #Route(name="filter",path="/filter/{positionId}")
* #param Request $request
*/
Or do it via get:
$request->get('positionId')
And then use it as a criteria in your repository:
$positions = $em->getRepository('AppBundle:Position')->findBy([
'positionId' => $positionId
]);
Ajax request:
$.ajax({
url:'path_to_your_controller',
data: {
"myselect": value
})
I am trying to add the Jquery UI Sortable behaviour to an existing table (which is populated through a JavaScript Code).I think I followed the right steps from this link Using Jquery UI sortable to sort table rows, yet the order never changes, it's as if the controller function is never called. Here is my code
This is the code in my twig
<table class="table">
<thead>
<tr>
<th class="col-md-2" data-sort="none">{{ 'element.single'|trans }}<span class="required"> *</span></th>
<th class="col-md-2"data-sort="none">{{ 'operation.type.single'|trans }}<span class="required"> *</span></th>
<th class="col-md-4" data-sort="none">{{ 'inspection.point.label'|trans }}<span class="required"> *</span></th>
<!--09:26-----edit-->
<th class="col-md-2" data-sort="none">{{ 'inspection.point.referenceValue'|trans }}</th>
<th class="col-md-2" data-sort="none">
{#bouton import des gammes d'opérations #}
{% if 'inspection_sheet_edit' != app.request.attributes.get('_route') %}
{% if is_granted('INSPECTIONSHEET_IMPORT',equipment) %}
<div class="btn-group pull-right">
<button class="btn btn-default btn-sm" type="submit" name="import">
<i class="fa fa-file"></i>
<span>{{ 'import'|trans }}</span>
</button>
</div>
{% endif %}
{% endif %}
</th>
</tr>
</thead>
<tbody id="tabledivbody" ></tbody>
</table>
<script>
var fixHelper = function(e, ui) {
ui.children().each(function() {
$(this).width($(this).width());
});
return ui;
}
$("#tabledivbody").sortable({
items: "tr",
cursor: 'move',
opacity: 0.6,
helper: fixHelper,
update: function() {
sendOrderToServer();
}
});
function sendOrderToServer() {
var order = $("#tabledivbody").sortable("serialize");
var test='console text';
$.ajax({
type: "POST", dataType: "json", url: "{{ path('post_order') }}",
data: order,
success: function(response) {
if (response.status == "success") {
console.log(test);
window.location.href = window.location.href;
} else {
alert('Some error occurred');
}
}
});
}
</script>
This is the function in my controller
/**
* #Route("/post_order", name="post_order")
*/
public function updateInspectionPointOrder(Request $request)
{
$em=$this->getDoctrine()->getManager();
$data = json_decode($request->request->get('request'));
$count=1;
foreach ($data->getPoints() as $point) {
//$count++;
$em->getRepository('EpxInspectionBundle:InspectionPoint')->updateDisplayOrderInspectionPoint($count,$point->getId());
$em->flush();
$point->setDisplayOrder($count);
$em->getRepository('EpxInspectionBundle:InspectionPoint')->updateDisplayOrderInspectionPoint($count,$point->getId());
$em->flush();
$count++;
}
}
Any insights please.
I am trying to figure out how to enable/disable user with checkbox without page reload.
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');
}
Question
How can I set corresponding user id to enabledChange function and change checked state depending on user state?
You need to add quotes to your variables, and pass changePath as an argument:
onclick="enabledChange('{{ user.id }}', '{{ path('user_enable_change', {'id': user.id}) }}')"
then:
function enabledChange(id, changePath) {
var value = $(this).val();
console.log('value: ' + value);
$.ajax({
type: "POST",
url: changePath,
async: true,
data: { },
success: function () {
console.log('success');
}
});
}
I hope this will help.
Another clean way of doing this is by passing parameters and routes via data attributes and accessing the button by class. In my example I had to increase product quantity without refreshing:
<button class="btn btn-sm btn-info increase"
data-id="{{ p.id }}"
data-url="{{ path('cart_add', {'pid': p.id}) }}">
Increase qty
</button>
$(document).ready(function() {
$(".increase").click(function (e) {
var value = $(this).data('id');
console.log('value: ' + value);
$.ajax({
type: "POST",
url: $(this).data('url'),
async: true,
data: {},
success: function () {
console.log('success increment');
}
});
});
});
I am trying to create simple "admin" page for my blog. For now I want to create some "dynamic" behaviour which will lock/unlock user. Then I want to replace single table row with another, taken from json. I wrote a simple function but unfortunatelly it is not working properly...
When I use constant value for user id (just for tests) it works, but only for ONE row. Other buttons do nothig. Then I tried to send id to my function as a parameter but now it says it doesn't exist in my TWIG (what is true).
I want to make it work, because reloading all page when you only locked one user or did another single action, is not a good idea.
How can I make my function work in a good way?
SCRIPT
$(document).ready(function(){
$(".LockUserButton").click(function(id){
$(this).closest("tr").toggleClass('locked progress-bar-striped');
$.ajax({
type: 'POST',
url: "{{ path('lock_ajax', {'id': id }) }}",
data: { user_id: "{{ user.id }}" },
dataType: 'json',
success: function () {
$(this).closest("tr").toggleClass('locked progress-bar-striped');
}
});
});
});
TWIG
<div class="table-content">
{% for u in users %}
{% if u.locked %}
<tr id="tableRow" class="locked progress-bar-striped">
{% else %}
<tr id="tableRow" class="">
{% endif %}
{% if u.roles[0] == 'ROLE_SUPER_ADMIN' %}
<td id="roles">
<h4><span class="glyphicon glyphicon-star admin-star" data-toggle="tooltip" data-placement="right" title="{{ u.roles[0] }}"></span></h4>
</td>
<td>{{ u.username }}</td>
<td>{{ u.email }}</td>
<td>
<span class="glyphicon glyphicon-lock admin-lock" data-toggle="tooltip" data-placement="right" title="Cannot modyfi this user!"></span>
</td>
<td>
<span class="glyphicon glyphicon-lock admin-lock" data-toggle="tooltip" data-placement="right" title="Cannot modyfi this user!"></span>
</td>
{% else %}
<td id="roles">
<h4><span class="glyphicon glyphicon-star-empty user-star" data-toggle="tooltip" data-placement="right" title="{{ u.roles[0] }}"></span></h4>
</td>
<td>{{ u.username }}</td>
<td>{{ u.email }}</td>
<td>
<div class="btn btn-custom LockUserButton">LOCK USER WITHOUT RELOADING</div>
</td>
<td>
<a href="/profile/admin/delete_user/{{ u.id }}" onclick="return confirm('{{ 'user.deleting'|trans }}')">
<div class="btn btn-custom hvr-grow">
<span class="glyphicon glyphicon-trash"></span>
</div>
</a>
</td>
{% endif %}
</tr>
{% endfor %}
</div>
CONTROLLER ACTION
/**
* #Route("/profile/ajax/{id}", name="lock_ajax")
*/
public function ajaxLock($id, Request $request)
{
$entityManager = $this->getDoctrine()->getManager();
$user = $entityManager->getRepository('AppBundle:User')->find($id);
if($user->isLocked()){
$user->setLocked(false);
}else{
$user->setLocked(true);
}
$entityManager->persist($user);
$entityManager->flush();
$result = array();
$result[0] = $user;
return new JsonResponse($result);
}
Thanks to Artamiel I have finally figure it out! Here's the solition for my case:
1) I have added data-id="{{ u.id }}" attribute to my button. Now i have access to my entity ID.
2) I have modify a little my script:
$(document).ready(function(){
$(".LockUserButton").click(function(){
//get user id from twig layout
var user = $(this).data("id");
//ajax request
$.ajax({
type: 'POST',
url: "/profile/ajax/"+user,
data: { user_id: "user" },
dataType: 'json',
context: this,
success: function () {
//i've wrote some function to toggle html
$(this).toggleText('{{ 'user.unlocked'|trans }}', '{{ 'user.locked'|trans }}');
//find table row and toggle classes when button clicked and request ok
$(this).closest("tr").toggleClass('locked progress-bar-striped');
},
error: function(){
alert("Error!");
}
});
});
});
I have a form wher I have to fill in some information in a field.
Even if I put something inside, I am getting the error:
The CSRF token is invalid. Please try to resubmit the form
Related to this question: symfony2 CSRF invalid I am using correctly the $form->bindRequest()
if ($this->getRequest()->getMethod() == 'POST') {
$form->bindRequest($this->getRequest());
if ($form->isValid())
{
...
}
Here is my template (twig) code:
<div class="item item-last">
<h1>Create Affiliation</h1>
{% if valid == false %}
<div class="error">
{{ form_errors(form) }}
{{ form_errors(form.affiliation) }}
{{ error }}
</div>
{% endif %}
{% if app.session.flash('user-notice') != '' %}
<div class="flash-notice">
{% autoescape false %}
{{ app.session.flash('user-notice') }}
{% endautoescape %}
</div>
{% endif %}
</div>
<div class="item item-last">
<form action="{{ path('SciForumVersion2Bundle_user_submission_affiliation_create', {'hash_key' : submission.hashkey, 'author_id' : author.id }) }}?ajax=no" method="POST" class="authorForm" {{ form_enctype(form) }}>
<div style="float:left;">
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td>
{{ form_label(form.affiliation) }}
</td>
<td>
{{ form_widget(form.affiliation, { 'attr': {'size': 40} }) }}
</td>
</tr>
<tr>
<td>
</td>
<td>
<div class="button button-left button-cancel">
<img src="{{ asset('bundles/sciforumversion2/images/design/new/button-red.png') }}"/>
cancel
</div>
<div style="float: left;"> </div>
<div class="button button-left button-cancel">
<img src="{{ asset('bundles/sciforumversion2/images/design/new/button.png') }}"/>
<input type="submit" name="login" value="submit" />
</div>
<div style="clear: both;"></div>
</td>
</tr>
</table>
</div>
{{ form_rest(form) }}
</form>
</div>
And here is the js code:
function init_submission_functions()
{
init_fck();
$(".submission_link").unbind("click").bind("click", function() {
var href = $(this).attr("href");
if( href == null || href == '' ) return false;
$.ajax({
type: "POST",
async: true,
url: href,
cache: false,
dataType: "json",
success: function(data) {
$("#content .contentwrap .itemwrap").html( data.content );
init_submission_functions();
}
});
return false;
});
$(".authorForm").unbind("submit").bind("submit", function() {
var href = $(this).attr("action");
if( href == null || href == '' ) return false;
var affiliation = "blabla";
$.ajax({
type: "POST",
async: true,
url: href,
affiliation: affiliation,
cache: false,
dataType: "json",
success: function(data) {
$("#content .contentwrap .itemwrap").html( data.content );
init_submission_functions();
}
});
return false;
});
}
But I am still getting the same error.
Send a serialized form using the serialize jQuery method:
$form.submit(function (e) {
e.preventDefault();
$this = $(this);
$.post($this.attr('action'), $this.serialize(), function (response) {
// handle the response here
});
});
This way Symfony will handle the submit request as a normal request — you don't have to do anything special to handle an Ajax form submission. All you'll need to do is to return a JsonResponse — if you need it, of course.
Here is an example of handling the form — adapt it to your needs:
if ('POST' === $request->getMethod()) {
$form->bind($request);
if ($form->isValid()) {
// do something here — like persisting or updating an entity
return new JsonResponse([
'success' => true,
]);
}
return new JsonResponse([
'success' => false,
'form' => $this->render($pathToTheTemplateHere, [
'form' => $form,
],
]);
}
The other way would be to use different templates: form.json.twig and form.html.twig — read the docs for details.