OctoberCMS: How to remove an item from todo list? - php

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');

Related

foreach loop : I can't see my added input

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

Search mechanism on symfony 5

I am writing a diploma in symfony 5 and studying symfony in parallel. I reached the implementation of the search in the table. I have a search field
<div class="search__container">
<div class="search__block">
<img class="search__glass" src="{{ asset('images/search-glass.svg')}}">
<input class="search__field" type="text">
</div>
</div>
and the actual table itself, which is output as follows
<div class="table__body">
{% block table_items %}
{% for item in items %}
<div class="body__item">
<a class="item__detail" href="item.html"></a>
<img class="item__icon" src="images/icons/{{ item.extension }}.svg">
<div class="item__title">{{ item.fileName }}</div>
<div class="item__extension">{{ item.extension }}</div>
<div class="item__updated">{{ item.uploaded }}</div>
<div class="item__size">{{ item.size }}b</div>
<div class="item__act">
<span></span>
<span></span>
<span></span>
<div class="action__popap">
<ul class="actions__list">
<li class="act__item detail">
Detail
</li>
<li class="act__item delete">
Delete
</li>
<li class="act__item download">
Download
</li>
</ul>
</div>
</div>
</div>
{% endfor %}
{% endblock %}
</div>
I want to do a search by "item.fileName" without reloading the page. And when I started doing it, I was at a dead end, not knowing what to do. I can do this with a page reload, but as for me it is not very convenient.
So, how can i do this?
I hope you can help me or point me to the correct vector.

how to add button on .php file by editing it

{!! view_render_event('bagisto.shop.layout.header.account-item.before') !!}
<login-header></login-header>
{!! view_render_event('bagisto.shop.layout.header.account-item.after') !!}
<script type="text/x-template" id="login-header-template">
<div class="dropdown">
<div id="account">
<div class="welcome-content pull-right" #click="togglePopup">
<i class="material-icons align-vertical-top">perm_identity</i>
<span class="text-center">
#guest('customer')
{{ __('velocity::app.header.welcome-message', ['customer_name' => trans('velocity::app.header.guest')]) }}!
#endguest
#auth('customer')
{{ __('velocity::app.header.welcome-message', ['customer_name' => auth()->guard('customer')->user()->first_name]) }}
#endauth
</span>
<span class="select-icon rango-arrow-down"></span>
</div>
</div>
<div class="account-modal sensitive-modal hide mt5">
<!--Content-->
#guest('customer')
<div class="modal-content">
<!--Header-->
<div class="modal-header no-border pb0">
<label class="fs18 grey">{{ __('shop::app.header.title') }}</label>
<button type="button" class="close disable-box-shadow" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true" class="white-text fs20" #click="togglePopup">×</span>
</button>
</div>
<!--Body-->
<div class="pl10 fs14">
<p>{{ __('shop::app.header.dropdown-text') }}</p>
</div>
<!--Footer-->
<div class="modal-footer">
<div>
<a href="{{ route('customer.session.index') }}">
<button
type="button"
class="theme-btn fs14 fw6">
{{ __('shop::app.header.sign-in') }}
</button>
</a>
</div>
<div>
<a href="{{ route('customer.register.index') }}">
<button
type="button"
class="theme-btn fs14 fw6">
{{ __('shop::app.header.sign-up') }}
</button>
</a>
</div>
</div>
</div>
#endguest
#auth('customer')
<div class="modal-content customer-options">
<div class="customer-session">
<label class="">
{{ auth()->guard('customer')->user()->first_name }}
</label>
</div>
<ul type="none">
<li>
{{ __('shop::app.header.profile') }}
</li>
<li>
{{ __('velocity::app.shop.general.orders') }}
</li>
<li>
{{ __('shop::app.header.wishlist') }}
</li>
<li>
{{ __('velocity::app.customer.compare.text') }}
</li>
<li>
{{ __('shop::app.header.logout') }}
</li>
</ul>
</div>
#endauth
<!--/.Content-->
</div>
</div>
</script>
#push('scripts')
<script type="text/javascript">
Vue.component('login-header', {
template: '#login-header-template',
methods: {
togglePopup: function (event) {
let accountModal = this.$el.querySelector('.account-modal');
let modal = $('#cart-modal-content')[0];
if (modal)
modal.classList.add('hide');
accountModal.classList.toggle('hide');
event.stopPropagation();
}
}
})
</script>
#endpush
I have a php file for creating login page
it look like this page view
how to add a new login button like login with google by editing in it.
Please be detail while giving your answer, it is very helpful for those who are new in php.
It is better to provide general answers that is helpful for those who are working in other domain related to php.
This code belong to bagisto framework which is base on laravel.Bagisto is greate framework for creating ecommerce sites.
This is the repo of bagisto https://github.com/bagisto/bagisto.git
I am new to stackoverflow so feel free to improve my question for more clarity friends if need.
We have added social login in our master branch, you may take a pull from there. Below is the provided PR link
https://github.com/bagisto/bagisto/pull/3367
PS - as the user asked the questions regarding the platform bagisto that's why I am answering for that purpose.

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

Laravel button does not link to route

#extends('layout')
#section('content')
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1>{{ $id->title }}</h1>
<ul class="list-group">
#foreach($id->notes as $note)
<li class="list-group-item">
{{ $note->body }}
<span class="pull-right">
<button href="/notes/{{ $note->id }}/edit" type="button" class="label label-default pull-xs-right">Edit</button>
</span>
</li>
#endforeach
</ul>
<hr>
<h3>Add a New Note</h3>
<div class="form-group">
<form method="post" action="/cards/{{ $id->id }}/notes">
<div class="form-group">
<textarea name="body" class="form-control"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Add Note</button>
</div>
</form>
</div>
</div>
</div>
#stop
The button in the foreach loop has the correct link, /note/id/edit. I can type that into chrome and go to the edit page. However, the button does not head there. I just see the click animation. Why would this be?
Button element does not have the href attribute so you can either wrap it in a link tag like this. and this is the easiest way
<a href="{{"/notes/". $note->id. "/edit"}}" >
<button type="button" class="label label-default pull-xs-right">Edit</button
></a>
or you can wrap it in a form element and set the action attribute to your desired link like this.
<Form method="get" action="{{"/notes/". $note->id. "/edit"}}">
<button type="submit" class="label label-default pull-xs-right">Edit</button>
</Form>
the 3rd way is to use javascript and onclick listener.
use <a></a> tag, instead of button, and set type="button"
Check Any

Categories