Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use the multiplier for editing #104

Open
tomminok opened this issue Apr 16, 2024 · 5 comments
Open

How to use the multiplier for editing #104

tomminok opened this issue Apr 16, 2024 · 5 comments
Labels

Comments

@tomminok
Copy link

tomminok commented Apr 16, 2024

Hello
I'm trying to use a multiplier in a data editing form.
According to the records in the database, the appropriate number of Multiplier elements must be displayed.

Unfortunately, I don't know how to properly access the multiplier components.

Can you state the correct use of the multiplier for editing?

$defHeader = json_decode($defVals->header, true);
            
$multiplier = $form->addMultiplier('multiplier', function (\Nette\Forms\Container $container, \Nette\Forms\Form $form) {
    $arrHlavicka = $this->webhookModel->getHlavicka(['stav'=>1])->fetchPairs("nazev", "nazev");

    $container->addSelect('hlavicka', 'Hlavička',$arrHlavicka)
        ->setHtmlAttribute('class', 'form-control')
        ->setHtmlAttribute("placeholder", 'Vyberte hlavičku')
        ->setPrompt('Vyberte hlavičku');

    $container->addText('hlavickaval', 'Request Header')
        ->setHtmlAttribute('class', 'form-control')
        ->setHtmlAttribute("placeholder", 'Hodnota hlavičky');    
}, count($defHeader));   

$multiplier->addCreateButton('Přidat hlavičku')
      ->addClass('btn btn-success');

foreach($defHeader as $k => $v){
    $hlavicka[] = [ 'hlavicka' => $k, 'hlavickaval' => $v ];
}

foreach ($multiplier as $index => $container) {
    $hlavicka = $container->getComponent('hlavicka');
    $hlavickaval = $container->getComponent('hlavickaval');
    
    // Nastavení výchozích hodnot
    $hlavicka->setDefaultValue($hlavicka[$index]['hlavicka']);
    $hlavickaval->setDefaultValue($hlavicka[$index]['hlavickaval']);

    // Přidání tlačítka RemoveButton
    $container->addRemoveButton('Smazat')
              ->addClass('btn btn-danger');
}

Thank you
Thomas

@jtojnar
Copy link
Collaborator

jtojnar commented Apr 16, 2024

Why not just use $multiplier->setDefaults($hlavicka)?

Also addRemoveButton() is method of multiplier too.

@tomminok
Copy link
Author

$multiplier->setDefaults($header);
It seems to work, it will add two entries with the completed data from the database.

But how to add tl to each item. addRemovButton()?

@jtojnar
Copy link
Collaborator

jtojnar commented Apr 16, 2024

Calling Multiplier::addRemoveButton() will ensure each item has a remove button.

@tomminok
Copy link
Author

tomminok commented Apr 16, 2024

please and where to call this Multiplier::add Remove Button()?
my full code is below:

$defHeader = json_decode($defVals->header, true);
            
$multiplier = $form->addMultiplier('multiplier', function (\Nette\Forms\Container $container, \Nette\Forms\Form $form) {
    $arrHlavicka = $this->webhookModel->getHlavicka(['stav'=>1])->fetchPairs("nazev", "nazev");

    $container->addSelect('hlavicka', 'Hlavička',$arrHlavicka)
        ->setHtmlAttribute('class', 'form-control')
        ->setHtmlAttribute("placeholder", 'Vyberte hlavičku')
        ->setPrompt('Vyberte hlavičku');

    $container->addText('hlavickaval', 'Request Header')
        ->setHtmlAttribute('class', 'form-control')
        ->setHtmlAttribute("placeholder", 'Hodnota hlavičky');    
    
}, count($defHeader));   

$multiplier->addCreateButton('Přidat hlavičku')
      ->addClass('btn btn-success');

foreach($defHeader as $k => $v){
    $hlavicka[] = [ 'hlavicka' => $k, 'hlavickaval' => $v ];
}

$multiplier->setDefaults($hlavicka);

$multiplier->addRemoveButton('Smazat')
      ->addClass('btn btn-danger');

Thank you

@jtojnar
Copy link
Collaborator

jtojnar commented Apr 16, 2024

I would expect your code to work. See also https://github.com/contributte/forms-multiplier/tree/master/.docs#basic-usage

Also please wrap code blocks between ```php and ``` so that syntax highlighting works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants