## General developer forum

### Insert Form Element Inside Table

Insert Form Element Inside Table

I've been following the guide here https://docs.moodle.org/dev/Form_API and this post https://moodle.org/mod/forum/discuss.php?d=126935 to create a form and learn to develop in Moodle. It's basically a bunch of radio buttons inside a table.

I've been using $radioarray[] =$mform->createElement('radio', ...) to create the radio buttons and $mform->addElement('html', ...) to create the html to wrap around the radio buttons. The problem is that the html is being run first and then all the radio buttons are being created. This makes the table appear at the top and the radio buttons down below the table instead of inside it. I posted my code below but omitted a lot of it because there is a lot of repetition.$mform->addElement('html', '<table class="table table-condensed ">'

. '<colgroup>

<col>

...

...

...

$radioarray=array();$mform->addElement('html', '<td>');

$radioarray[] =$mform->createElement('radio', 'grade', '', '', 80, array('class'=>'commenttextarea'));

$mform->addElement('html', '</td>' . '<td class="text-center">'); ... ... ...$radioarray[] = $mform->createElement('radio', 'grade', '', '', 95,$attributes);

$mform->addElement('html', '</td>' . '<td class="text-center">');$radioarray[] = $mform->createElement('radio', 'grade', '', '', 100,$attributes);

$mform->addElement('html', '</td>' . '</tr>' . '</table>');$mform->addGroup($radioarray, 'radioar', '', array(' '), false);$this->add_action_buttons();

Average of ratings: -
Re: Insert Form Element Inside Table

As far as I can see, the forms library has produced exactly what you've asked for - you've gone down the form, outputting the content of a table, whilst building up a list of radio button elements, then you've output all the radio button elements as a group at the end of the table.

If you want the radio buttons to appear within the table, then use $mform->addElement('radio', ...) instead of adding them all as a group at the end of the table. For clarity, the 'addGroup()' function has nothing to do with the functionality of the radio buttons (they are associated with each other by the fact they have the same name), it is entirely to do with the visual layout of the buttons on the page (so that they appear together as a single block, with just one main label). Average of ratings: - Re: Insert Form Element Inside Table Thanks for that. I made the change you suggested and now it's working as expected. On my original code,$mform->createElement was only creating the elements but were not being displayed until $mform->addGroup was called?$mform->addElement both creates and displays the element?

Average of ratings: -
Re: Insert Form Element Inside Table

Yes, createElement() creates an element, without adding it to the form content (usually only used when you want it as part of a grouped element, using addGroup(), but it can also be used when you want to add an element somewhere other than below the existing elements); addElement() creates an element then immediately adds it to the form content.

Average of ratings: -
Re: Insert Form Element Inside Table

Hey Edgar

There is also the toHtml() method that outputs the html for a form field, so you can put it where you want it.

So create the element:

$checkbox =$mform->createElement('advcheckbox', 'mycheckbox');

Then use something like this to display the field in your table somewhere:

$output .=$checkbox->toHtml();

Average of ratings: -
Re: Insert Form Element Inside Table

Hey Edgar

There is also the toHtml() method that outputs the html for a form field, so you can put it where you want it.

So create the element:

$checkbox =$mform->createElement('advcheckbox', 'mycheckbox');

Then use something like this to display the field in your table somewhere:

$output .=$checkbox->toHtml();

Average of ratings: -