Form checkboxes in Html tables

Form checkboxes in Html tables

by Ricardo Godinho -
Number of replies: 8

Is it possible to add form checkboxes, as a column, to an Html_table ?  I have this enrollment table which is read from a csv file, and for teacher conviniency, I was asked to add the possibility for teachers to filter which students they want to enroll on their course page( for those students who are enrolled on an external db, but eventually don't show up to classes). I would like to place a column of checkboxes( probably advcheckboxes) for each username. Checked boxes would correspond who was getting enrolled.

 

Average of ratings: -
In reply to Ricardo Godinho

Re: Form checkboxes in Html tables

by Ricardo Godinho -

If this isn't possible, can this table be turned into a form? Bear in mind that most of these fields are data that I get from an url and read from a csv file.

In reply to Ricardo Godinho

Re: Form checkboxes in Html tables

by Ricardo Godinho -

So, anyone knows?. Or adding checkboxes is only possible to a regular form? This table was created from html_table()

In reply to Ricardo Godinho

Re: Form checkboxes in Html tables

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

You can add any HTML you like to a html_table.

Integrating a table into a moodleform object is a bit more fiddly, but if you want to just add some form fields to a table:

$table = new html_table();
// Additional table set up goes here
$table->data[] = array(
'First field',
html_writer::empty_tag('input', array('type' => 'checkbox', 'name' => 'fieldname')),
'Another table field',
);

echo html_writer::start_tag('form', array('action' => '/destination/url.php', 'method' => 'post'));
echo html_writer::table($table);
echo html_writer::end_tag('form');

 

Average of ratings: Useful (1)
In reply to Davo Smith

Re: Form checkboxes in Html tables

by Ricardo Godinho -

Thank you Davo. Is there a way to add a select all/none form type option to the checkbox column? And can these checkboxes come checked, by default?

Example: A teacher has 300 students to enroll, but for some reason, wants to enroll 280 only. Clicks continue  and a new table is presented with the 280 students enrolled.  Are these changes  saved only by form buttons?

Lots of questions I know :S, but the original goal of "everything automated enrollement" of my internship got, in the last minute, deviated by my teachers. 

 

In reply to Ricardo Godinho

Re: Form checkboxes in Html tables

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

To make them all checked, you need to add a new attribute to the array:

array('type' => 'checkbox', 'name' => 'fieldname', 'checked' => 'checked);

Adding a 'select all' or 'deselect all' would require a bit of javascript.

Make sure the checkboxes all have the same 'class' (e.g. 'class' => 'enrolcheckbox') and then create some javascript that selects all the checkboxes (see http://docs.moodle.org/dev/YUI/Modules for how to create a YUI module to contain your javascript). Something like:

Y.all('.enrolcheckbox').set('checked', 'checked') (to set all of them)
or
Y.all('.enrolcheckbox').removeAttribute('checked'); (to unset all of them).

Average of ratings: Useful (1)
In reply to Davo Smith

Re: Form checkboxes in Html tables

by Ricardo Godinho -

Following the link you provided, i came up with this:

M.local_ecoclipaluno={

init= function(){
Y.all('.enrolcheckbox').set('checked','checked');||
Y.all('.enrolcheckbox').removeAttribute('checked');
e.target.addClass(somefile.php);
};

Not much, but the examples didn't seem to fit my needs. Also added  $PAGE->requires->yui_module('local/ecoclipaluno','init'); to the top of my script file. Select/Deselect all does not appear.

I confess I'm a total newbie to js.  

In reply to Ricardo Godinho

Re: Form checkboxes in Html tables

by David Mudrák -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators

This can't work. What you need to do is to read some YUI tutorials on how new nodes are created and how event listeners are registered.

Basically, you should prepare an empty div container in the form as a part of the mform definition (using static field type should work). Give that div some unique id or class, something like

<div id="myselectallcontainer"></div>

Then, in your YUI module, fill the container with the actual link

var c = Y.one('#myselectallcontainer');
c.set('innerHTML', '<a id="myselectalllink" href="#">Select all</a>');

Then, register the listener that will be triggered by clicking the link and in the callback, set all checkboxes are checked. E.g.

var l = Y.one('#myselectalllink');
l.on('click', function (e) { Y.all('.enrolcheckbox').set('checked','checked'); }

But really, do not just simply copy some examples with a hope it would automagically work. Read more docs on YUI nodes and events.

In reply to David Mudrák

Re: Form checkboxes in Html tables

by Ricardo Godinho -

Hello David, so are you saying that I would have to have a form anyway?  to place <div id="myselectallcontainer"></div> in it?