foreach loop : I can't see my added input - php

I work with Symfony and Twig. I currently have a twig page containing a list of products, I display them with a foreach loop and I put pagination to limit the display of products.
I'm trying to put a form in this page with a checkbox as input and my problem is the following:
the checkboxes appear on all the products on the first page but when I go to the second page, I don't see the checkboxes, I need to reload the page to see them.
there is some code :
view of my loop :
<form>
<div class="row" >
{% for annonce in annonces %}
<div class="col-12 col-md-6 col-lg-4">
<p class="text text--blue text--bold m-0 text--medium mt-2">
{{ annonce._source.titre }}
</p>
<p class="m-0">{{ 'Réf' }}: {{ annonce._source.reference }}</p>
<div class="d-flex mt-2 text--bold">
<div class="d-flex me-2">
{{ annonce._source.ville }}
</div>
</div>
<div>
<input type="checkbox" name="checkbox[]" id="checkbox_pdf" value="{{annonce._id}}" multiple/>
</div>
</div>
{% endfor %}
</div>
<input type="submit" id="pdf_submit" value="Create PDF" name="submit_pdf" class="btn btn-primary">
</form>
view of the pagination :
<div class="col d-flex justify-content-between align-items-center">
<div class="d-flex">
{% if page > 0 %}
<a href="#" data-action="pagination" data-uri="{{ path('ajax_annonce_pagination',{'page':0, 'type':'frontoffice'}) }}" data-target="pagination-target">
«
</a>
<a href="#" data-action="pagination" data-uri="{{ path('ajax_annonce_pagination',{'page':page-1, 'type':'frontoffice'}) }}" data-target="pagination-target">
{{ 'Précédent' }}
</a>
{% else %}
<a href="#" disabled="disabled" >
{{ 'Précédent' }}
</a>
{% endif %}
</div>
<div>
<ul class="list-unstyled pagination m-0">
{% for i in (page+1)..(page+4) %}
{% if i <= numberOfMaxPage %}
{% if i == (page+1) %}
<li>
<a href="#" data-action="pagination" data-uri="{{ path('ajax_annonce_pagination',{'page':(i-1), 'type':'frontoffice'}) }}" data-target="pagination-target">
{{ i }}
</a>
</li>
{% else %}
<li>
<a href="#" data-action="pagination" data-uri="{{ path('ajax_annonce_pagination',{'page':(i-1), 'type':'frontoffice'}) }}" data-target="pagination-target">
{{ i }}
</a>
</li>
{% endif %}
{% endif %}
{% endfor %}
</ul>
</div>
<div class="d-flex">
{% if page < (numberOfMaxPage-1) %}
<a href="#" data-action="pagination" data-uri="{{ path('ajax_annonce_pagination',{'page':page+1, 'type':'frontoffice'}) }}" data-target="pagination-target">
{{ 'Suivant' }}
</a>
<a href="#" data-action="pagination" data-uri="{{ path('ajax_annonce_pagination',{'page':numberOfMaxPage-1, 'type':'frontoffice'}) }}" data-target="pagination-target">
»
</a>
{% endif %}
</div>
</div>
JS of the pagination :
$( document ).ready(function() {
$(document).on('click', 'a.pagination',function(e) {
e.preventDefault();
$.ajax({
url: $(this).data('uri'),
}).done(function(html) {
$('#pagination-target').html(html);
$('html,body').animate({scrollTop: $('#pagination-target').offset().top - 80}, 200);
var $scrollable = document.getElementById('pagination-target');
$scrollable.scrollIntoView();
});
});
});
In my controller I render my view like this :
return $this->render('front/annonce/list.html.twig', array(
'annonces' => $results['hits']['hits'],
'total' => $results['hits']['total']['value'],
'website' => $website,
'page' => $request->query->getInt('page')
));
What is this problem related to? is this due to the fact that it is not possible to add inputs in a looped element?
Is it related to pagination?
thanks you in advance

Related

I want to avoid button repetition

I display the content of the category and I display the button "displays more" if I have other content but the button has been marked according to the number of ads in the category, and when I paste the button outside "for" I get always this button even when the category content is empty
<div class="pedagogical pedagogical--category js-pedagogical-items" data-type="category" data-nbdisplayed="4">
{% for categoryId in questionCategories %}
<div class="row pedagogical__items" data-type="category" data-value="{{ categoryId }}">
{% for tool in tools if tool.questionCategory == categoryId %}
<div class="col-12 col-md-6 pedagogical__item__wrapper">
{% include 'components/tool-preview-item.html.twig' with {'tool' : tool} %}
</div>
<div class="col-12 text-center">
<button class="btn btn-outline-secondary js-show-more" data-type="category" data-value="{{ categoryId }}">{{ 'show-more'|trans({}, 'action') }}</button>
</div>
{% endfor %}
</div>
{% endfor %}
</div>
I think you just need to add a check before rendering your button:
<div class="pedagogical pedagogical--category js-pedagogical-items" data-type="category" data-nbdisplayed="4">
{% for categoryId in questionCategories %}
<div class="row pedagogical__items" data-type="category" data-value="{{ categoryId }}">
{% set has_items = 'false' %}
{% for tool in tools if tool.questionCategory == categoryId %}
{% set has_items = 'true' %}
<div class="col-12 col-md-6 pedagogical__item__wrapper">
{% include 'components/tool-preview-item.html.twig' with {'tool' : tool} %}
</div>
{% endfor %}
{% if has_items == 'true' %}
<div class="col-12 text-center">
<button class="btn btn-outline-secondary js-show-more" data-type="category" data-value="{{ categoryId }}">{{ 'show-more'|trans({}, 'action') }}</button>
</div>
{% endif %}
</div>
{% endfor %}
</div>

Symfony Panther : can't login with form

I'm testing an application with Symfony Panther. I want to log in the user to have not provided elements in anonymous conditions. Here is my test class :
namespace App\Tests\Functional;
use Symfony\Component\Panther\PantherTestCase;
class SecurityControllerTest extends PantherTestCase
{
public function testConnexion()
{
$client = static::createPantherClient('127.0.0.1', '9001');
$crawler = $client->request('GET', '/connexion');
$form = $crawler->selectButton('Se connecter')->form();
$form['_email'] = 'email#domain.com';
$form['_password'] = 'password';
$crawler = $client->submit($form);
// $link = $crawler->filter('a:contains("Déconnexion")')->link();
// $crawler = $client->click($link);
$link = $crawler->selectLink('Déconnexion');
$link->click();
$this->assertSame(self::$baseUri.'/', $client->getCurrentURL());
}
}
The associated template (connexion.html.twig) :
{% extends "layout.html.twig" %}
{% block page_title 'Login' %}
{% block final_javascripts %}
{{ encore_entry_script_tags('sendCredentials') }}
{% endblock %}
{% block content %}
(...)
<div class="row mt-4">
<div class="col-md-6">
<form id="connexion-form" action="{{ path('security_connexion') }}" method="post">
<div class="form-group">
<label for="email">Email</label>
<input type="text" id="email" name="_email" class="form-control">
</div>
<div class="form-group">
<label for="password">Mot de passe</label>
<input type="password" id="password" name="_password" class="form-control">
</div>
<button type="submit" class="btn btn-primary button">Se connecter</button>
</form>
</div>
</div>
</div>
</div></div>
{% endblock %}
And the _nav.html.twig template which represents the navigation bar :
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="{{ path("index") }}"><img src="{{ asset("images/temporary-logo.png") }}" alt="shinigami-laser-logo" style="max-height: 60px;"></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
(...)
{% if is_granted('IS_AUTHENTICATED_FULLY') %}
{% if is_granted('ROLE_USER') %}
<li>
Mes infos
</li>
<li>
Gerer mes cartes
</li>
<li>
Mes stats
</li>
{% endif %}
<li class="nav-item">
<a class="nav-link shogun-link" href="{{ path('security_deconnexion') }}">Déconnexion</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link shogun-link" href="{{ path('user_enregistrement') }}">S'enregistrer</a>
</li>
<li class="nav-item">
<a class="nav-link shogun-link" href="{{ path('security_connexion') }}">Se connecter</a>
</li>
{% endif %}
</ul>
</div>
</div>
</nav>
In dev mode the user can authenticate and log out with the Déconnexion link in the navbar.
But the command php bin/phpunit tests/Functional/SecurityControllerTest.php says : InvalidArgumentException: The current node list is empty. pointing on this line : $link->click(); revealing that the Déconnexion link is not visible.
Is that user is not logged in ?
How to fix that and access provided elements for authanticated user like the Déconnexion link ?
Try these methods:
$crawler->selectButton('.btn-primary');
$crawler->executeScript("document.querySelector('.btn-primary').click()");
https://symfony.com/doc/current/components/dom_crawler.html#forms

October cms blog plugin: How to update component when the button is clicked

description = "Display Blog list"
[viewBag]
snippetCode = "post-list.htm"
snippetName = "Display Blog list"
snippetProperties[heading][title] = "Heading"
snippetProperties[heading][type] = "string"
snippetProperties[heading][default] = "News & Events"
snippetProperties[heading][options][] = ""
[blogPosts]
pageNumber = "{{ :page }}"
postsPerPage = 6
noPostsMessage = "No posts found"
sortOrder = "published_at desc"
categoryPage = "article-post"
postPage = "article-post"
==
<?php
function onLoadMore()
{
$this['pageNumber'] = 10;
}
?>
==
<section class="section section-fat news">
<div class="section-header">
<div class="container text-center">
<h2 class="heading-large">{{ heading|raw }}</h2>
</div>
</div>
<div class="container-fluid">
<div id="loadmore" class="row">
{% set posts = blogPosts.posts %}
{# fetch all posts in Blog #}
{% for post in posts %}
<div class="item col-sm-6 col-md-4 col-lg-6">
<a href="{{ post.url }}" class="news-box">
<div class="news-box-container">
<div class="news-box-content">
<h4>{{ post.title }}</h4>
<p>{% if post.categories.count %} in {% endif %} {% for category in post.categories %}{{ category.name }}{% if not loop.last %}, {% endif %} {% endfor %} {{ post.published_at|date('M d, Y') }}</p>
</div>
</div>
<div class="news-box-arrow"><i class="icon-angle-circle-right"></i></div>
<div class="news-box-image" style="background-image: url('{{ post.featured_images[0].thumb(2500,1000) }}');"></div>
</a>
</div>
{% else %}
<div class="text-center">{{ noPostsMessage }}</div>
{% endfor %}
<div class="text-center news-load">
<a data-request="onLoadMore" data-request-update="'load-more': '#loadmore'" data-attach-loading class="btn btn-lg btn-invert">Load More</a>
</div>
</div>
</section>
This is the code of the snippet I created for showing the blogs.
I'm using blog plugin of Rainlab, is there a way that I can customize or modify the code that will help me update the components? I want to add 3 posts to "postsPerPage = 6" every time I clicked the button. I've been trying to find a solution or plugin for this problem but sadly I can't find one.

display error collapse accordion bootstrap

I do a file explorer with symfony2 and Php 5.3 :)
I wanna display {{ Twig error }} when a directory is empty . Here my twig view :
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" href=".mycollapse3">
<i class="fa fa-caret-square-o-right"></i> Tableaux de bord mensuels
</a>
</div>
{% for tab in tableauxliste %}
<div class="accordion-group">
{% if tableauxliste is empty %}
<div class="mycollapse3 collapse alert alert-info">
{{ erreur }}
</div>
<div class="accordion-heading mycollapse3 collapse">
<ul>
<a data-toggle="collapse" href="#collapse3{{ tab[0] }}">
{{ tab[0] }}
</a>
</ul>
{% endif %}
</div>
<div id="collapse3{{ tab[0] }}" class="accordion-body collapse">
<div class="accordion-inner">
{% if tab[0] is empty %}
<div class="alert alert-info">
{{ erreur }}
</div>
{% endif %}
{% for file in tab[1] %}
{% set repertoire = dir_tableaudebord ~ '/' ~ tab[0] %}
<ul><a target="_blank" href="{{ path('affiche', { 'repertoire':repertoire, 'file':file }) }}"><i class="fa fa-file-text-o"></i> {{ file | convert_encoding('UTF-8', 'Windows-1252') }}</a></ul>
{% endfor %}
</div>
</div>
</div>
{% endfor %}
</div>
In this case, the error isn't hidden and I don't know why.
I just wanna hide the error and display it after click on it.
Any ideas ? Thanks !
Add to you action some condition and return response (render this data's in template).
return $this->render(
'FolderBundle:To/you:template.html.twig',
[
'erreur' => $erreur
'tableauxliste' => $tableauxliste,
]
);
And use this variable in you template.

OctoberCMS: How to remove an item from todo list?

I was following this tutorial to learn how to create a TODO list. But it ends without explaining how to remove an item from the list. Could somebody please post a sample code for it?
Here is the content of Todo\default.htm:
<script type="text/javascript" src="{{ ['assets/vendor/jquery.min.js']|theme }}"></script>
{% framework %}
<form
data-request="{{ __SELF__ }}::onAddItem"
data-request-success="$('#inputItem').val('')"
data-request-update="'{{ __SELF__ }}::tasks': '#result'"
>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Tasks assigned to: {{__SELF__.name}} </h3>
</div>
<div class="panel-body">
<div class="input-group">
<input name="task" type="text" id="inputItem" class="form-control" value=""/>
<span class="input-group-btn">
<button type="submit" class="btn btn-primary">Add</button>
</span>
</div>
</div>
<ul class="list-group" id="result">
{% partial __SELF__ ~ '::tasks' tasks = __SELF__.tasks %}
</ul>
</div>
</form>
And the content of tasks.htm partial:
{% for task in tasks %}
<li class="list-group-item">
{{ task }}
<button class="close pull-right">×</button>
</li>
{% endfor %}
I need to get the [X] button which removes a single task from the todo list working. My guess is that I should go with this:
{% for task in tasks %}
<li class="list-group-item">
{{ task }}
<button class="close pull-right" data-request="{{ __SELF__ }}::onDeleteItem">×</button>
</li>
{% endfor %}
I don't know though how to pass the id of each item to the onDeleteItem function.
Following is the code for default.htm
<form
data-request="{{ __SELF__ }}::onAddItem"
data-request-update="'{{ __SELF__ }}::tasks': '#result'"
data-request-success="$('#input-item').val('')"
>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">To Do List</h3>
</div>
<div class="panel-body">
<div class="input-group">
<input type="text" id="input-item" class="form-control" value="" name="task" placeholder="What needs to be done?">
<span class="input-group-btn">
<button type="submit" class="btn btn btn-primary" data-attach-loading>Add</button>
</span>
</div>
</div>
<ul class="list-group" id="result">
{% partial __SELF__ ~ '::tasks' tasks = __SELF__.tasks %}
</ul>
</div>
Following is the code in Todo.php
public function onRun(){
//$this->tasks=Task::lists('title');
$this->tasks=Task::all();
}
public function onAddItem()
{
$taskName = post('task');
$task=new Task;
$task->title=$taskName;
$task->save();
$this->page['tasks']=Task::all();
}
public function onDeleteItem()
{
$id = post('id');
$task=Task::find($id);
$task->delete($id);
$this->page['tasks']=Task::all();
}
and following is the code for tasks.php partial
{% for task in tasks %}
<ul class="list-group">
<li class="list-group-item">{{task.title}}
<button type="button" class="close pull-right"
data-request="{{ __SELF__ }}::onDeleteItem"
data-request-update="'{{ __SELF__ }}::tasks': '#result'"
data-request-data="id:'{{task.id}}'"
>×</button>
</li>
</ul>
{% endfor %}
It works perfectly for me in case of any problem let me inform
The [X] Button should use:
data-request to tell the php ajax handler, which procedure to call
data-request-data to tell the php ajax handler, which data to use
data-request-update to tell the javascript ajax handler, which partial to update
<button
data-request="onDeleteItem"
data-request-data="deleteItem: '{{ task }}'"
data-request-update="'{{__SELF__}}::tasks': '#result'"
class="close pull-right">
&times
</button>
...and you will find the content of the task variable with
$taskToDelete=post('deleteItem');

Categories