IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Symfony PHP Discussion :

Gestion dynamique d'un tableau HTML


Sujet :

Symfony PHP

  1. #1
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2014
    Messages
    236
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 31
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juin 2014
    Messages : 236
    Points : 61
    Points
    61
    Par défaut Gestion dynamique d'un tableau HTML
    Bonjour,

    Mon tableau :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
            <table class="detailsTable">
                <tr class="tableHeader">
                    <th class="tableDesignationColumn">{{ "orderTable.designation"|trans }}</th>
                    <th class="tableVATColumn">{{ "orderTable.tva"|trans }}</th>
                    <th class="tablePUColumn">{{ "orderTable.unitPrice"|trans }}</th>
                    <th class="tableQteColumn">{{ "orderTable.quantity"|trans }}</th>
                    <th class="tableAmountColumn">{{ "orderTable.total"|trans }}</th>
                </tr>
                {% for item in items %}
                    <tr class="tableBody">
                        <td class="firstCell">{{ item.designation}}</td>
                        <td class="align-right">
                            {% if item.tva is not null %}
                                {{ item.tva }}
                            {% endif %}
                        </td>
                        <td class="align-right">{{ item.unitPrice }}</td>
                        <td class="align-right">{{ item.quantity }}</td>
                        <td class="align-right">{{ item.total }}</td>
                    </tr>
                {% endfor %}
                <tr class="firstTableFooter">
                    <td class="tableDeliveryFooter">
                    </td>
                    <td class="tableHTFooter" colspan="3">{{ "orderTable.totalHT"|trans }}</td>
                    <td class="align-right">{{ order.totalHt }}</td>
                </tr>
                {% for rate, total in vats %}
                    <tr class="tableFooter">
                        <td></td>
                        <td class="tableHTFooter" colspan="3">{{ "orderTable.totalVAT"|trans }} {{ rate }}</td>
                        <td class="align-right">{{ total }}</td>
                    </tr>
                {% endfor %}
                <tr class="tableFooter">
                    <td></td>
                    <td class="tableHTFooter" colspan="3">{{ "orderTable.totalTTC"|trans }}</td>
                    <td class="align-right">{{ order.totalTtc }}</td>
                </tr>
            </table>
    Me retourne, dans un fichier PDF, le tableau de ma facture :

    Nom : Capture.PNG
Affichages : 826
Taille : 20,6 Ko

    Je désire réaliser, par exemple à l'aide d'une fonction Twig, afficher ou non certaines colonnes du tableau en mettant des paramètres à true ou false.

    Pour l'instant, dans mon fichier "AppExtension", j'ai fait ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    namespace AppBundle\Twig;
    use AppBundle\Entity\Order;
     
    class AppExtension extends \Twig_Extension
    {
        public function getFilters()
        {
            return array(
                new \Twig_SimpleFilter('array_sum', 'array_sum'),
            );
        }
     
        public function getFunctions()
        {
            return array(
                new \Twig_SimpleFunction('getInvoiceTable', [$this,'getInvoiceTable']),
            );
        }
     
        public function getInvoiceTable(Order $order, Array $elementToDisplay = ['getTotal' => 'true', 'getVat' => 'false'])
        {
            return 'table';
        }
    }
    Je récupère mon Order et les paramètres que je veux afficher dans mon tableau.

    Ensuite dans mon template Twig :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
            <h1>{{ getInvoiceTable(order, [{
                    'getTotal' : 'true',
                    'getVat' : 'false'
                }]) }}</h1>
    J'arrive à récuperer ma valeur de retour (table).

    Y a t'il une méthode plus simple pour ce que je veux faire ou je continue avec ma fonction Twig ? Si je continue, comment je procède pour afficher mon tableau ?

    Merci

  2. #2
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2014
    Messages
    236
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 31
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juin 2014
    Messages : 236
    Points : 61
    Points
    61
    Par défaut
    J'ai réussi à récupérer les valeurs que je voulais dans ma fonction Twig :

    Mon fichier AppExtension (là où j'ai ma fonction twig) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
        public function getInvoiceTable(Order $order, Array $elementToDisplay = ['label' => 'false',
                                                                                'vtaRate' => 'false',
                                                                                'unitPrice' => 'false', 
                                                                                'qty' => 'false',
                                                                                'total' => 'false',
                                                                                'totalHt' => 'false',
                                                                                'vat' => 'false'])
        {
     
            $label = $order->getItems()->first()->getLabel();
            $vtaRate = $order->getItems()->first()->getVtaRate();
            $unitPrice = $order->getItems()->first()->getPrice();
            $qty = $order->getItems()->first()->getQty();
            $total = $order->getItems()->first()->getTotalTtc();
            $totalHt = $order->getItems()->first()->getOrder()->getTotalHt();
            $vat = $order->getItems()->first()->getTotalVat();
     
     
            $elementToDisplay = array('label' => $label,
                                    'vtaRate' => $vtaRate,
                                    'unitPrice' => $unitPrice, 
                                    'qty' => $qty,
                                    'total' => $total,
                                    'totalHt' => $totalHt,
                                    'vat' => $vat);
     
            return $elementToDisplay;
        }
    Et mon template :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
        <div>
            <div class="amountCurrencyLabel">
                {{ "generic.amountCurrency"|trans }}
            </div>
            <h1>{{ getInvoiceTable(order, [{
                    'label' : 'true',
                    'vtaRate' : 'false',
                    'unitPrice' : 'false',
                    'qty' : 'false',
                    'getTotal' : 'true',
                    'getTotalHt' : 'false',
                    'getVat' : 'false'
                }]) }}</h1>
            <table class="detailsTable">
                <tr class="tableHeader">
                    {% if {{ getInvoiceTable(order, [{'label' : 'true'}]) }} %}
                        <th class="tableDesignationColumn">{{ "orderTable.designation"|trans }}</th>
                    {% endif %}
                    <th class="tableVATColumn">{{ "orderTable.tva"|trans }}</th>
                    <th class="tablePUColumn">{{ "orderTable.unitPrice"|trans }}</th>
                    <th class="tableQteColumn">{{ "orderTable.quantity"|trans }}</th>
                    <th class="tableAmountColumn">{{ "orderTable.total"|trans }}</th>
                </tr>
                {% for item in items %}
                    <tr class="tableBody">
                        {% if {{ getInvoiceTable(order, [{'label' : 'true'}]) }} %}
                            <td class="firstCell">{{ item.designation}}</td>
                        {% endif %}
                        <td class="align-right">
                            {% if item.tva is not null %}
                                {{ item.tva }}
                            {% endif %}
                        </td>
                        <td class="align-right">{{ item.unitPrice }}</td>
                        <td class="align-right">{{ item.quantity }}</td>
                        <td class="align-right">{{ item.total }}</td>
                    </tr>
                {% endfor %}
                <tr class="firstTableFooter">
                    <td class="tableDeliveryFooter">
                    </td>
                    <td class="tableHTFooter" colspan="3">{{ "orderTable.totalHT"|trans }}</td>
                    <td class="align-right">{{ order.totalHt }}</td>
                </tr>
                {% for rate, total in vats %}
                    <tr class="tableFooter">
                        <td></td>
                        <td class="tableHTFooter" colspan="3">{{ "orderTable.totalVAT"|trans }} {{ rate }}</td>
                        <td class="align-right">{{ total }}</td>
                    </tr>
                {% endfor %}
                <tr class="tableFooter">
                    <td></td>
                    <td class="tableHTFooter" colspan="3">{{ "orderTable.totalTTC"|trans }}</td>
                    <td class="align-right">{{ order.totalTtc }}</td>
                </tr>
            </table>
        </div>
    Pour afficher le contenu de ma balise h1 j'ai cette erreur : "An exception has been thrown during the rendering of a template Notice: Array to string conversion"

    Et les erreurs de mes conditions : "A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value \"{\"."}"

    Ça doit être une erreur de syntaxe que je n'arrive pas à corriger

    Merci

Discussions similaires

  1. Gestion dynamique d'un tableau
    Par direct dans le forum C
    Réponses: 5
    Dernier message: 24/04/2009, 16h29
  2. Tri dynamique d'un tableau HTML et cellules en EURO
    Par Tchupacabra dans le forum Général JavaScript
    Réponses: 9
    Dernier message: 04/06/2008, 15h25
  3. Ajouter dynamiquement des lignes à un tableau HTML
    Par jeannot1974 dans le forum Général JavaScript
    Réponses: 14
    Dernier message: 20/11/2006, 15h39
  4. [Javascript] Connaître la hauteur d'un tableau HTML dynamique ?
    Par renaud26 dans le forum Général JavaScript
    Réponses: 5
    Dernier message: 21/04/2006, 17h35
  5. [Tableaux] Tableau HTML dynamique
    Par gunth dans le forum Langage
    Réponses: 2
    Dernier message: 14/12/2005, 16h59

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo