How to implement Ajax in Symfony2 - php

I have a basic but functional search mechanism in my Symfony2 project.This will query and display back to the user the data using Doctrine2 LIKE expression.But I want to make more 'dynamic' and more 'user-friendly' by adding Ajax functionality.I added some Ajax code in my controller, but I don't know how to make it work.The image loader is just 'spinning' without displaying the results.
//controller
public function searcAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$query = $this->getRequest()->get('query');
if(!$query) {
if(!$request->isXmlHttpRequest()) {
return $this->redirect($this->generateUrl('voters_list'));
} else {
return new Response('No results.');
}
}
$city = $em->getRepository('DuterteBundle:City')->findCity($query);
if($request->isXmlHttpRequest()) {
if('*' == $query || !$city || $query == '') {
return new Response('No results.');
}
//display a another page
return $this->render('DuterteBundle:City:list1.html.twig', array('city' => $city));
}
return $this->render('DuterteBundle:City:search.html.twig', array('city' => $city));
}
// routing
search:
path: /search
defaults: { _controller:DuterteBundle:City:Searc }
requirements:
//search.html.twig
{% extends '::base.html.twig' %}
{% block body %}
<div id="city">
{% include 'DuterteBundle:City:list1.html.twig' with {'city': city} %}
</div>
{% endblock %}
//list1.html.twig
{% block body %}
<div class="page-header">
<h4>City/Municipality/Town and Its Corresponding Numbers of Voters</h4>
</div>
<table class="table table-hover table-bordered table-condensed">
<thead>
<tr>
<th>City</th>
<th>Votes</th>
<th>Percent</th>
</tr>
</thead>
<tbody>
{% for city in city %}
<tr>
<td>{{ city }}</td>
<td>{{ number_votes_city(city.id) }}</td>
<td></td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
My search form is embedded in navbar in the main layout.
<div class="col-sm-3 col-md-3" id="search">
<form class="navbar-form" role="search" action ="{{ path('search')}}" method ="post">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search city/town" name="query" value="{{ app.request.get('query') }}" id="search_keywords">
<div class="input-group-btn">
<button class="btn btn-default" type="submit"><i class="fa fa-search"></i></button>
<img id="loader" src="{{ asset('bundles/duterte/images/loader.gif') }}" style="vertical-align: middle; display: none" />
</div>
</div>
</form>
//repository
public function findCity($city)
{
return $this
->createQueryBuilder('c')
->select('c')
->where('c.name LIKE :name_city')
->setParameter('name_city', '%'.$city.'%')
->orderBy('c.name', 'ASC')
->getQuery()
->getResult()
;
}
and finally the js file
$(document).ready(function()
{
$('.search input[type="submit"]').hide();
$('#search_keywords').keyup(function(key)
{
if(this.value.length >= 3 || this.value == '') {
$('#loader').show();
$('#city').load(
$(this).parent('form').attr('action'),
{ query: this.value ? this.value + '*' : this.value },
function() {
$('#loader').hide();
}
);
}
});
});
Any help is appreciated

The same functionality, but a different approach. Listen to the keyup event on the search box
and then make ajax calls to a controller that returns the list of matched results as json. Check the response and
based on the status of the response, hide the existing listing and replace its contents with the markup returned in the
json response for the AJAX call.
Here, the example is for search in user listing.
Table markup on the twig file
<div id="user-list-div">
<table class="records_list" id="user-list">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Username</th>
<th>Password</th>
<th>Salt</th>
<th>Email</th>
<th>Isactive</th>
<th>Createdat</th>
<th>Updatedat</th>
<th>Isbillableuser</th>
<th>Isdeleted</th>
<th>Actions</th>
</tr>
</thead>
<tbody id = "user-table">
{% for entity in entities %}
<tr>
<td>{{ entity.id }}</td>
<td>{{ entity.name }}</td>
<td>{{ entity.username }}</td>
<td>{{ entity.password }}</td>
<td>{{ entity.salt }}</td>
<td>{{ entity.email }}</td>
<td>{{ entity.isActive }}</td>
<td>{% if entity.createdAt %}{{ entity.createdAt|date('Y-m-d H:i:s') }}{% endif %}</td>
<td>{% if entity.updatedAt %}{{ entity.updatedAt|date('Y-m-d H:i:s') }}{% endif %}</td>
<td>{{ entity.isBillableUser }}</td>
<td>{{ entity.isDeleted }}</td>
<td>
<ul>
<li>
show
</li>
<li>
edit
</li>
</ul>
</td>
</tr>
{% endfor %}
</tbody>
Search form markup
<div class="col-lg-6">
<div class="input-group">
<input type="text" class="form-control" id="search-field">
<span class="input-group-btn">
<button class="btn btn-default" type="button">Go!</button>
</span>
</div>
</div>
javascript part
<script>
$(function(){
console.log('desperate for');
var searchField = $('#search-field');
var userTable = $('#user-table');
var userListDiv = $('#user-list-div');
searchField.keyup(function(evt){
console.log($(this).val());
$.ajax({
url: '{{ path('admin_user_search') }}',
method: "POST",
data: "id=" + $(this).val() ,
dataType: 'html',
success: function(result, request) {
var parsedData =JSON.parse(result);
console.log(parsedData);
if(parsedData.status ==='success'){
console.log('hete');
userListDiv.empty();
userListDiv.html(parsedData.data);
}else{
//handle no result case
}
}
});
});
});
</script>
ajax_template.html.twig file
The same table markup as given above
controller action
public function searchuserAction(){
$em = $this->getDoctrine()->getManager();
$request = $this->get('request');
$searchParameter = $request->request->get('id');
//call repository function
$entities = $em->getRepository('LBCoreBundle:User')->findUsersForname($searchParameter);
$status = 'error';
$html = '';
if($entities){
$data = $this->render('LBCoreBundle:User:ajax_template.html.twig', array(
'entities' => $entities,
));
$status = 'success';
$html = $data->getContent();
}
$jsonArray = array(
'status' => $status,
'data' => $html,
);
$response = new Response(json_encode($jsonArray));
$response->headers->set('Content-Type', 'application/json; charset=utf-8');
return $response;
}
Respository function
public function findUsersForname($name){
$em = $this->getEntityManager();
$query = $em->createQuery("SELECT e FROM LBCoreBundle:User e
WHERE e.username LIKE '%$name%'");
$entities = $query->getResult();
return $entities;
}

Related

Laravel 8 dropdown list pull from data and filter to the Data-table

Laravel 8 with User & teamwork together
I am working on data-table javascript, and controller but the error is still there, and it bugged me as I am not knowledgeable with laravel as I only work on laravel for one month (it's perfect code and way better than WordPress PHP). I look at details on google, but most of them are details write dropdowns based on the hardcode selection list.
I'm using
<label for="selectTeam">{{ __('Team')}}</label>
<select class="form-control" id="selectTeam">
#foreach($teamlistfliter as $row)
<option>{{ $row->team }}</option>
#endforeach
</select>
and it is working fine and but not related to data table show user in the list
but when I want this dropdown can query to list of users, that where it became error
here are the controller pages I wrote
class TeamUserListController extends Controller
{
public function index(Request $request)
{
if ($request->ajax())
{
$teamlistfliter = User::join('teams', 'teams.id', '=', 'users.current_team_id')
->get(['users.name as username', 'teams.name as team', 'users.firstname as first_name', 'users.surname as surname']);
return Datatables::of($teamlistfliter)
->filter(function ($instance) use ($request) {
if ($request->get('team') == '0' || $request->get('team') == '1') {
$instance->where('team', $request->get('team'));
}
if (!empty($request->get('search'))) {
$instance->where(function($w) use($request){
$search = $request->get('search');
$w->orWhere('name', 'LIKE', "%$search%")
->orWhere('email', 'LIKE', "%$search%");
});
}
})
->rawColumns(['team'])
->make(true);
}
return view('teamlistfliter');
}
}
and blade pages
<div class="row">
<div class="col-sm-12">
<div class="card">
<div class="card-header d-block">
<div class="col-md-6">
<div class="form-group">
<label for="selectTeam">{{ __('Team')}}</label>
<select class="form-control" id="selectTeam">
#foreach($teamlistfliter as $row)
<option>{{ $row->team }}</option>
#endforeach
</select>
</div>
</div>
</div>
<div class="card-body">
<div class="dt-responsive">
<table id="simpletable"
class="table table-striped table-bordered nowrap">
<thead>
<tr>
<th>{{ __('First Name')}}</th>
<th>{{ __('Surname')}}</th>
<th>{{ __('Username')}}</th>
<th>{{ __('Team')}}</th>
</tr>
</thead>
<tbody>
#foreach($teamlistfliter as $row)
<tr>
<td>{{ $row->first_name }}</td>
<td>{{ $row->surname }}</td>
<td>{{ $row->username }}</td>
<td>{{ $row->team }}</td>
</tr>
#endforeach
</tbody>
<tfoot>
<tr>
<th>{{ __('First Name')}}</th>
<th>{{ __('Surname')}}</th>
<th>{{ __('Username')}}</th>
<th>{{ __('Team')}}</th>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- push external js -->
#push('script')
<script src="{{ asset('plugins/DataTables/datatables.min.js') }}"></script>
<script src="{{ asset('js/datatables.js') }}"></script>
<script type="text/javascript">
$(function () {
var table = $('#selectTeam').DataTable({
processing: true,
serverSide: true,
ajax: {
url: "{{ route('teamlistfliter.index') }}",
data: function (d) {
d.approved = $('#selectTeam').val(),
d.search = $('input[type="search"]').val()
}
},
columns: [
{data: 'firstname', name: 'firstname'},
{data: 'surname', name: 'surname'},
{data: 'username', name: 'username'},
{data: 'email', name: 'email'},
{data: 'team', name: 'team'},
]
});
$('#selectTeam').change(function(){
table.draw();
});
});
</script>
#endpush
#endsection
I fear I miss something as I assume the issue is $teamlistfilter outside the statement.
[2021-09-03 19:28:18] local.ERROR: Undefined variable: teamlistfliter (View: C:\Ergnation-rowing\resources\views\teamlistfliter.blade.php) {"view":{"view":"C:\\Ergnation-rowing\
esources\\views/teamlistfliter.blade.php","data":{"errors":"<pre class=sf-dump id=sf-dump-1080913330 data-indent-pad=\" \"><span class=sf-dump-note>Illuminate\\Support\\ViewErrorBag</span> {<a class=sf-dump-ref>#1449</a><samp data-depth=1 class=sf-dump-expanded>
#<span class=sf-dump-protected title=\"Protected property\">bags</span>: []
</samp>}
</pre><script>Sfdump(\"sf-dump-1080913330\", {\"maxDepth\":3,\"maxStringLength\":160})</script>
"}},"userId":1,"exception":"[object] (Facade\\Ignition\\Exceptions\\ViewException(code: 0): Undefined variable: teamlistfliter (View: C:\\Ergnation-rowing\
esources\\views\\teamlistfliter.blade.php) at C:\\Ergnation-rowing\
esources\\views/teamlistfliter.blade.php:45)
Try get basic value from controller and use JavaScript to write on blade pages
public function index()
{
$teamlistfliter = User::join('teams', 'teams.id', '=', 'users.current_team_id')
->get(['users.name as username', 'teams.name as team', 'users.firstname as first_name', 'users.surname as surname']);
return view('teamlistfliter', compact('teamlistfliter'));
}
and use JavaScript on blade pages
<div class="row">
<div class="col-sm-12">
<div class="card">
<div class="card-header d-block">
<h3>{{ __('Team')}}</h3>
<div class="Team-filter">
<select id="TeamFilter" class="form-control">
<option value="">Show All</option>
#foreach($teamlistfliter as $row)
<option>{{ $row->team }}</option>
#endforeach
</select>
</div>
</div>
<div class="card-body">
<div class="dt-responsive">
<table id="filterTable"
class="table table-striped table-bordered nowrap">
<thead>
<tr>
<th>{{ __('First Name')}}</th>
<th>{{ __('Surname')}}</th>
<th>{{ __('Username')}}</th>
<th>{{ __('Team')}}</th>
</tr>
</thead>
<tbody>
#foreach($teamlistfliter as $row)
<tr>
<td>{{ $row->first_name }}</td>
<td>{{ $row->surname }}</td>
<td>{{ $row->username }}</td>
<td>{{ $row->team }}</td>
</tr>
#endforeach
</tbody>
<tfoot>
<tr>
<th>{{ __('First Name')}}</th>
<th>{{ __('Surname')}}</th>
<th>{{ __('Username')}}</th>
<th>{{ __('Team')}}</th>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- push external js -->
#push('script')
<script src="{{ asset('plugins/DataTables/datatables.min.js') }}"></script>
<script src="{{ asset('js/datatables.js') }}"></script>
<script>
$("document").ready(function () {
$("#filterTable").dataTable({
"searching": true
});
//Get a reference to the new datatable
var table = $('#filterTable').DataTable();
//Take the Team filter drop down and append it to the datatables_filter div.
//You can use this same idea to move the filter anywhere withing the datatable that you want.
$("#filterTable_filter.dataTables_filter").append($("#TeamFilter"));
//Get the column index for the Team column to be used in the method below ($.fn.dataTable.ext.search.push)
//This tells datatables what column to filter on when a user selects a value from the dropdown.
//It's important that the text used here (Team) is the same for used in the header of the column to filter
var TeamIndex = 0;
$("#filterTable th").each(function (i) {
if ($($(this)).html() == "Team") {
TeamIndex = i; return false;
}
});
//Use the built in datatables API to filter the existing rows by the Team column
$.fn.dataTable.ext.search.push(
function (settings, data, dataIndex) {
var selectedItem = $('#TeamFilter').val()
var Team = data[TeamIndex];
if (selectedItem === "" || Team.includes(selectedItem)) {
return true;
}
return false;
}
);
//Set the change event for the Team Filter dropdown to redraw the datatable each time
//a user selects a new filter.
$("#TeamFilter").change(function (e) {
table.draw();
});
table.draw();
});
</script>
#endpush
#endsection
that should should be working
This is what I'm looking for and thank you so much!

FormCollection and KnpPaginator Symfony 4

Privet all!
I'm new to Symfony 4 and generally to PHP and OOP in general. I was helped to make the form using FormCollection the code looks so
AbilityContoller.php
/**
* #Route("/edit", name="ability_edit_all")
*/
public function edit(Request $request)
{
$abilitys = $this->getDoctrine()->getRepository(Ability::class)->findAll();
$form = $this->createForm(AbilityArrayType::class, ['abilitys' => $abilitys]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
foreach ($form->getData()['abilitys'] as $ability){
$this->getDoctrine()->getManager()->persist($ability);
}
$this->getDoctrine()->getManager()->flush();
$this->addFlash('notice', 'Your changes were saved!');
return $this->redirectToRoute('ability_edit_all');
}
return $this->render('ability/edit.all.html.twig', [
'form' => $form->createView()
]);
}
and screenshot
you see that my record with the form has both tags with a choiceType and a simple input
And I also figured out myself with knp_paginator, because if I output more than 15 records with FormCollection, I get an error about the lack of memory in php. I made a static table where I output all my records, sorting and filtering works, pagination is broken as it should.
here is the code
ServiceFile.php
class AbilityService{
protected $em;
protected $container;
public function __construct(EntityManagerInterface $entityManager, ContainerInterface $container)
{
$this->em = $entityManager;
$this->container = $container;
}
public function ReturnData($request){
$em = $this->em;
$container = $this->container;
$query = $em->createQuery(
'
SELECT
t.idItem,
t.code,
t.reloadTime,
t.durationTime,
t.idAbilityType
FROM
App\Entity\Ability t
'
);
dump($query);
//$result = $query->execute();
$pagenator = $container->get('knp_paginator');
$results = $pagenator->paginate(
$query,
$request->query->getInt('page', 1),
$request->query->getInt('limit', 10)
);
return ($results);
}
}
and controller
use App\Service\ServiceFile;
/**
* #Route("/test-edit", name="ability_test_edit_all")
*/
public function edit_test(AbilityService $query, Request $request)
{
$data = $query->ReturnData($request);
return $this->render('ability/edit.alltest.html.twig',[
'form' => $data,
]);
}
here screenshot
Work a perfect!!
I bet I can not figure out how to make the same result, but with FormCollection?
The second question, if it turns out to be pagination, will filtering and sorting work from knp_paginator?
ok,
ok, something happened and it's not right for mine, but Pagination and sorting + filtering work only on <input> tags, inside <select> <option selected> var </ select> nothing works, but the solution is working
controllerFile.php
/**
* #Route("/edit", name="ability_edit_all")
*/
public function edit(AbilityService $abilityService, Request $request)
{
$data = $abilityService->ReturnData($request);
$form = $this->createForm(AbilityArrayType::class, ['abilitys' => $data->getItems()]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
foreach ($form->getData()['abilitys'] as $properties){
dump($properties);
$ability = $this->getDoctrine()->getRepository(Ability::class)->find($properties['id']);
$ability->setIdItem($properties['idItem']);
$ability->setCode($properties['code']);
$ability->setReloadTime($properties['reloadTime']);
$ability->setDurationTime($properties['durationTime']);
$ability->setIdAbilityType($properties['idAbilityType']);
dump($ability);
$this->getDoctrine()->getManager()->persist($ability);
}
$this->getDoctrine()->getManager()->flush();
$this->addFlash('notice', 'Your changes were saved!');//проделать со всеми см edit.html.twig
return $this->redirectToRoute('ability_edit_all');
}
return $this->render('ability/edit.all.html.twig', [
'form' => $form->createView(),
'paginator' => $data
]);
}
twig tempalte
{% extends 'base.html.twig' %}
{% block title %}Ability edit all{% endblock %}
{% block body %}
<style>
label{display:none}
</style>
<h1>Ability edit all</h1>
<a class="btn-add mar-bot-top-20" href="{{ path('ability_new') }}"><i class="fas fa-plus"></i> Create new</a>
<div class="row">
<div class="col-4 text-center">
<div class="navigation">
{{ knp_pagination_render(paginator) }}
</div>
</div>
<div class="col-8 text-left">
<div class="filtration">
{{ knp_pagination_filter(paginator, {
't.idItem': 'IdItem',
't.code': 'Code',
't.reloadTime': 'ReloadTime',
't.durationTime': 'DurationTime',
't.idAbilityType': 'IdAbilityType',
}) }}
</div>
</div>
</div>
<table class="table">
<tr>
<th>{{ knp_pagination_sortable(paginator, 'idItem', 't.idItem') }}</th>
<th>{{ knp_pagination_sortable(paginator, 'Code', 't.code') }}</th>
<th>{{ knp_pagination_sortable(paginator, 'ReloadTime', 't.reloadTime') }}</th>
<th>{{ knp_pagination_sortable(paginator, 'DurationTime', 't.durationTime') }}</th>
<th>{{ knp_pagination_sortable(paginator, 'idAbilityType', 't.idAbilityType') }}</th>
</tr>
{{ form_start(form) }}
{% for ability in form.abilitys %}
<tr>
<td>{{ form_row(ability.idItem) }}</td>
<td>{{ form_row(ability.code) }}</td>
<td>{{ form_row(ability.reloadTime) }}</td>
<td>{{ form_row(ability.durationTime) }}</td>
<td>{{ form_row(ability.idAbilityType) }}</td>
</tr>
{% else %}
<tr>
<td colspan="9">no records found</td>
</tr>
{% endfor %}
{{ form_end(form) }}
</table>
<div class="row">
<div class="col-4 text-center">
<div class="navigation">
{{ knp_pagination_render(paginator) }}
</div>
</div>
<div class="col-8 text-left">
<div class="filtration">
{{ knp_pagination_filter(paginator, {
't.idItem': 'IdItem',
't.code': 'Code',
't.reloadTime': 'ReloadTime',
't.durationTime': 'DurationTime',
't.idAbilityType': 'IdAbilityType',
}) }}
</div>
</div>
</div>
<a class="btn-add" href="{{ path('ability_new') }}"><i class="fas fa-plus"></i> Create new</a>
{% for message in app.flashes('notice') %}
<div class="flash-notice">
{{ message }}
</div>
{% endfor %}
{% endblock %}
and ServiceFile.php
<?php
namespace App\Service;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Ability;
class AbilityService{
protected $em;
protected $container;
public function __construct(EntityManagerInterface $entityManager, ContainerInterface $container)
{
$this->em = $entityManager;
$this->container = $container;
}
public function ReturnData($request){
$em = $this->em;
$container = $this->container;
$query = $em->createQuery(
'
SELECT
t.id,
t.idItem,
t.code,
t.reloadTime,
t.durationTime,
t.idAbilityType,
t.dateCreated,
t.author,
t.dateChange,
t.lastAuthor
FROM
App\Entity\Ability t
'
);
$pagenator = $container->get('knp_paginator');
$results = $pagenator->paginate(
$query,
$request->query->getInt('page', 1),
$request->query->getInt('limit', 6)
);
return ($results);
}
}
if anyone can tell how to implement sorting within <select> <option selected> var </ select>, you can use <th> {{knp_pagination_sortable (paginator, 'idItem', 't.idItem')}} </ th>
write please!

Symfony get url parameters with spaces and underscore

I'm new to Symfony, and I'm trying to filter my table with a search field. I'm using KnpPaginatorBundle to paginate and sort my table, and I've created a form to filter my request ( method GET )
It is generally working, but when I use spaces or underscores in my search input, it doesn't work, I assume there is something to do about the GET method and the way to encode the text, but I don't know how.
Here's my code :
View :
<div class="row">
<div class="well row">
<form action="" method="get">
<div class="col-md-2">
<label for="famille">Famille d'articles</label>
<select name="famille">
<option value="0">Toutes</option>
{% for famille in listFamilles %}
<option value="{{ famille.id }}" {% if data.famille is defined %} {% if famille.id == data.famille %} selected {% endif %} {% endif %}>{{ famille.nom }}</option>
{% endfor %}
</select>
</div>
<div class="col-md-4">
<input type="checkbox" name="rds" {% if data.rds == 1 %} checked {% endif %}>
<label for="rds" style="margin-left:5px">Montrer les articles en rupture de stock</label>
</div>
<div class="col-md-4">
<label for="recherche">Recherche</label>
<input name="recherche" style="width:100%" type="text" placeholder="Recherche" {% if data.recherche is defined %} value="{{ data.recherche }}" {% endif %}>
</div>
<div class="col-md-2" style="text-align:center">
<button type="submit" class="btn btn-primary">Rechercher</button>
</div>
</form>
</div>
<div class="well row">
<table class="table table-bordered table-striped" style="width: 100%" cellspacing="0">
<thead>
<tr>
<th>{{ knp_pagination_sortable(listArticles, 'Référence client', 'a.ref_article') }}</th>
<th>{{ knp_pagination_sortable(listArticles, 'Référence interne', 'a.ref_logistique') }}</th>
<th>{{ knp_pagination_sortable(listArticles, 'Famille', 'f.nom') }}</th>
<th>{{ knp_pagination_sortable(listArticles, 'Libellé', 'a.libelle') }}</th>
<th>{{ knp_pagination_sortable(listArticles, 'Alerte', 'a.stock_alerte') }}</th>
<th>{{ knp_pagination_sortable(listArticles, 'Stock', 'a.stock_actuel') }}</th>
</tr>
</thead>
<tbody id="bodyListeArticles">
{% for article in listArticles %}
<tr>
<td>{{ article.refArticle }}</td>
<td>{{ article.refLogistique }}</td>
<td>{{ article.famille.nom }}</td>
<td>{{ article.libelle }}</td>
<td>{{ article.StockAlerte }}</td>
<td>{{ article.StockActuel }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="navigation text-center">
{{ knp_pagination_render(listArticles) }}
</div>
</div>
</div>
Controller :
public function listeAction(Request $request) {
if ($this->get('security.authorization_checker')->isGranted('ROLE_OPERATEUR')) {
$session = $request->getSession();
if ($session->get('client_id')) {
$clientId = $session->get('client_id');
} else {
$request->getSession()->getFlashBag()->add('info', 'Vous devez sélectionner un client pour accéder à la liste de ses articles.');
return $this->redirectToRoute('gr_bo_liste_clients');
}
} elseif ($this->get('security.authorization_checker')->isGranted('ROLE_SUPERCOLLABORATEUR') || ($this->get('security.authorization_checker')->isGranted('ROLE_COLLABORATEUR') && $this->getUser()->getListeArticles())) {
$clientId = $this->getUser()->getClient()->getId();
} else {
$request->getSession()->getFlashBag()->add('info', 'Vous n\'avez pas les permissions requises pour accéder à cette page.');
return $this->redirectToRoute('gr_bo_liste_commandes');
}
$em = $this->getDoctrine()->getManager();
$data = [];
$data['clientId'] = $clientId;
if ($request->query->getAlnum('recherche')) {
$data['recherche'] = $request->query->getAlnum('recherche');
}
if ($request->query->getAlnum('famille') && $request->query->getAlnum('famille') != "0") {
$data['famille'] = $request->query->getAlnum('famille');
}
if ($request->query->getAlNum('rds') == "on" || ($request->query->getAlnum('rds') == "" && $request->query->getAlnum('famille') == "" && $request->query->getAlnum('recherche') == "")) {
$data['rds'] = 1;
} else {
$data['rds'] = 0;
}
$listArticles = $em->getRepository('GRBackOfficeBundle:Article')->getQueryArticles($data);
/**
* #var $paginator \Knp\Component\Pager\Paginator
*/
$paginator = $this->get('knp_paginator');
$result = $paginator->paginate(
$listArticles, $request->query->getInt('page', 1), $request->query->getInt('limit', 5)
);
$listFamilles = $em->getRepository('GRBackOfficeBundle:Famille')->findAll();
return $this->render('GRBackOfficeBundle:Article:liste_articles.html.twig', array(
'listArticles' => $result,
'listFamilles' => $listFamilles,
'data' => $data
));
}
Repository :
public function getQueryArticles($data) {
$query = $this->createQueryBuilder('a')
->leftJoin('a.images', 'i')
->addSelect('i')
->leftJoin('a.type_stockage', 't')
->addSelect('t')
->leftJoin('a.famille', 'f')
->addSelect('f');
if (array_key_exists('famille', $data)) {
$query->andWhere('f.id = :famille')
->setParameter('famille', $data['famille']);
}
if (array_key_exists('rds', $data)) {
if ($data['rds'] == 0) {
$query->andWhere('a.stock_actuel > 0');
}
}
if (array_key_exists('recherche', $data)) {
$query->andWhere('a.ref_article LIKE :recherche OR a.ref_logistique LIKE :recherche OR a.libelle LIKE :recherche')
->setParameter('recherche', '%' . $data['recherche'] . '%');
}
$query->leftJoin('a.sousfamille', 's')
->addSelect('s')
->leftJoin('a.client', 'c')
->addSelect('c')
->andWhere('c.id = :client')
->setParameter('client', $data['clientId'])
->orderBy('a.ref_article', 'ASC')
->getQuery();
return $query;
}
When I use a space or an underscore in my "recherche" search filter, my table appears empty, and it seems to delete the spaces or the underscore so if I try with "John Doe" or "John_Doe", it will return me the results for "JohnDoe", which is empty.
If someone have an idea of how I can proceed, it will be appreciated !
You can use urlencode on your data.recherche. But there also more natural way to do this in twig

Ajax occasionally don't work. It does depends by the browser?

I have an huge issue with my shopping cart system. The problem is when I select products and try to send through ajax to server side, products not always counted. I have to use ajax to collect all data because I'm using JQuery datatables plugin.
List of products should looks like this:
Incorrect view is a screen from my windows firefox, correct view is from my chromium broswer on linux. It looks like there is some dependency on which browser running my webservice. I'm testing my webservice on server production and it works properly only on my chromium browser.
My buddy said there is in inappropriate data sent in Ajax code.
Here's my HTML template:
{% block content %}
<div class="panel-body">
<form method="post" action="{{ path('order') }}" id="orderList">
<table class="table table-bordered table-striped mb-none" id="datatable-default">
<thead>
<tr>
<th>Nazwa</th>
<th>Kod</th>
<th>Cena netto(zł)</th>
<th class="hidden-xs">Cena brutto(zł)</th>
<th class="hidden-xs">Ilość</th>
</tr>
</thead>
<tbody>
{% for product in products %}
<tr>
<td>{{ product.productName }}</td>
<td>{{ product.code }}
</td>
<td>{{ product.priceNetto }}</td>
<td class="center hidden-xs">{{ product.priceBrutto }}</td>
<td>
<input type="number" class="spinner-input form-control" min="0" value="0" name="cart[{{ product.code }}]" />
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% if app.user != null %}
<button id="getData" type="submit" class="btn btn-primary hidden-xs">Zamów</button>
{% else %}
<p>Zaloguj się aby dokonać zamówienia</p>
{% endif %}
</form>
</div>
{% endblock %}
AJAX code:
$(document).ready(function () {
$('#getData').click( function (event) {
//event.preventDefault();
var paginator = $('#datatable-default').DataTable();
var dat = paginator.$('input').serialize();
console.log(dat);
console.log(decodeURIComponent(dat));
$.ajax({
type: "POST",
url: $('#orderList').attr('action'),
data: {'orders': decodeURIComponent(dat)},
success: function (response) {
console.log(response)
}
});
});
});
And my server side code:
public function orderAction(Request $request)
{
$session = $request->getSession();
$totalPriceNetto = 0;
$totalPriceBrutto = 0;
$user = $this->getUser();
$address = $user->getStreet();
if($request->isXmlHttpRequest())
{
$repository = $this->getDoctrine()->getRepository('AppBundle:Products');
$jsitems = $request->get('orders');
$items = [];
parse_str($jsitems, $items);
$orderItems = [];
foreach ($items['cart'] as $index=>$item)
{
if( $item != 0)
{
$orderItems[$index] = $item;
$orders[$index] = $repository->findBy(['code' => $index]);
//$orders[$index][0]->setPriceBrutto(str_replace(',', '.', str_replace('.', '', $orders[$index][0]->getPriceBrutto())));
//$orders[$index][0]->setPriceNetto(str_replace(',', '.', str_replace('.', '', $orders[$index][0]->getPriceNetto())));
$orders[$index]['value'] = $item;
}
}
$session->set('orders', $orders);
$session->set('orderItems', $orderItems);
foreach ($orders as $index=>$item)
{
$productObject = $item[0];
$totalPriceNetto += floatval(str_replace(',', '.', str_replace('.', '', $productObject->getPriceNetto()))) * (float)$item['value'];
$totalPriceBrutto += floatval(str_replace(',', '.', str_replace('.', '', $productObject->getPriceBrutto()))) * (float)$item['value'];
}
$totalPriceBrutto = round($totalPriceBrutto - ($totalPriceBrutto * $user->getPromo()/100), 2);
$session->set('dd', $totalPriceNetto);
$session->set('de', $totalPriceBrutto);
return $this->render("/default/shop/makeOrder.html.twig", ['totalPriceNetto' => $totalPriceNetto, 'totalPriceBrutto' => $totalPriceBrutto, 'address' => $address]);
}
if($session->has('dd') && $session->has('de'))
{
$totalPriceBrutto = $session->get('de');
$totalPriceNetto = $session->get('dd');
$session->remove('dd');
$session->remove('de');
}
return $this->render("/default/shop/makeOrder.html.twig", ['totalPriceNetto' => $totalPriceNetto, 'totalPriceBrutto' => $totalPriceBrutto, 'address' => $address]);
}

Symfony 2 - Update database on click

I try to update the 'archive' value of the database when I click on a check box. After searching the site and google, I managed to do it.
However, by adding a second article, it only works for the first one. If you ever have ideas, I will be grateful!
Thank you
I have fosjsroutingbundle installed.
Here is my controller :
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');
}
}
My route :
sos_montage_archive:
path: /admin/archive/{id}
defaults: { _controller: SosMontagesBundle:Admin:archive }
requirements:
id: \d+
options:
expose: true
My view :
{% extends '#SosMontages/layout.html.twig' %}
{% block content %}
<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><i class="fa fa-pencil fa-2x" aria-hidden="true"></i>
<i class="fa fa-trash-o fa-2x" aria-hidden="true"></i></td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="navigation">
{{ knp_pagination_render(pagination) }}
</div>
</div>
{% endblock %}
{% block 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>
{% endblock %}
Your click listenner on your checkbox may not be correct. You should use a change listener while it's a checkbox. Change your click function in js to change:
$('#archive').change(function() {
if($(this).is(":checked")) {
//'checked' event code
return;
}
//'unchecked' event code
});
However you also have two checkboxes and only one id. This might create weird bugs. Changing your ids to #archive1 and #archive2 may fix your code.
You have to fix your HTML and differentiate your #archive id selector.
You can do that by using loop.index for example:
id="archive{{ loop.index }}" //output : archive1, archive2...
Twig doc :
http://twig.sensiolabs.org/doc/2.x/tags/for.html#the-loop-variable

Categories