mardi 3 septembre 2019

Display one checkbox in each table row in twig/Symfony3

I would like to display a checkbox with the ID value in each row of a twig table :

First I search the data to display : $em = $this->getDoctrine()->getManager();

    // Récupération des commerçants qui ont des commandes non-facturées
    //$listeCommercants = $em->getRepository('SWPlatformBundle:Commande')->getCommercantsCommandesAFacturer($debut, $fin);

    $debut = $debut . " 00:00";
    $fin = $fin . " 23:59";

    $RAW_QUERY = 'SELECT commande.id, cli.raisonSociale, SUM(commande.montantTTC) AS total
        FROM commande 
        LEFT JOIN client AS cli ON commande.commercant_id = cli.id 
        WHERE commande.fichierFacture IS NULL AND commande.horodatageCreation >= :debut AND commande.horodatageCreation <= :fin
        GROUP BY cli.id;';

    $statement = $em->getConnection()->prepare($RAW_QUERY);

    // Set parameters 
    $statement->bindValue('debut', $debut);
    $statement->bindValue('fin', $fin);

    $statement->execute();

    $listeCommercants = $statement->fetchAll();

AND I send it to my twig template

        $defaultData = ['message' => 'Type your message here'];
        $form = $this->createFormBuilder($defaultData)
            ->add('commande',       EntityType::class, array(
                'class'        => 'SWPlatformBundle:Facture',
                'choice_label' => 'commande',
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('c')
                      ->where('e.active = :un')
                      ->setParameter('un', 1);
                },
                'disabled' => true,
              ))
            ->add('Générer', SubmitType::class)
            ->getForm();

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {

            // TODO ENVOI DU FORMULAIRE


            /*$data = $form->getData();

            $debut = date_format($data['debut'], 'Y-m-d');
            $fin = date_format($data['fin'], 'Y-m-d');          

            return $this->redirectToRoute('sw_platform_facture_recherche', array('id' => $id, 'debut' => $debut, 'fin' => $fin,));*/
        }

        return $this->render('SWPlatformBundle:Facture:indexRecherche.html.twig', array(
          'listeCommercants' => $listeCommercants,
          'detail' => " à facturer",
          //'form' => $form->createView(),
        ));

Twig template

        {# form_start(form, {'attr': {'class': 'form-horizontal'}}) #}

        {# form_errors(form) #}
        <table width="100%" class="table table-striped table-bordered table-hover" > {# id="dataTables-example" #}
            <thead>
                <tr>
                    <th>Commerçant</th>
                    <th>Total à facturer</th>
                </tr>
            </thead>
            <tbody>
            
            </tbody>
        </table>
        {# form_rest(form) #}

        {# form_end(form) #}

I don't reach to build the form with dynamic checkbox and display it on the twig template. Is there someone to help me to do this please?




Aucun commentaire:

Enregistrer un commentaire