Question

(Edited to provide clearer example) I have cakephp model structure of document->section->variables

In a form I want to be able to list all variables, but nested within document and then section levels. That bit is fine.

I want check boxes alongside each item at the variable level - to allow selection of variables.

I want a checkbox at sectionlevel, that when selected will select all the checkboxes for the variables in that section.

I want a checkbox at document level so that when selected it will select all checkboxes for the variables for that document (ie in all sections.)

To facilitate this I have named the checkboxes thus (well, I have set the ids)(pseudo code):

document level: id = Document id
section level id = Document id_Section id
variable level id = Document id_Section id_Variable_id

My hope this that if I select the section level checkbox I can set all the variable level checkboxes FOR THAT SECTION by using the Document id_Section_id stub at the beginning of all the ids for the variable level checkboxes... but maybe I am grasping at straws?

Here is a sample of checkboxes in levels: (apologies for layout)

    <?php
//Document level
//all of this within a ForEach loop for documents
echo "<table>";
echo "<tr><td>Collection dates</td><td>".$project_document['data_collection_dates']."</td>";
echo "<td>Select (toggle) all variables"
        .$this->Form->checkbox($project_document['id'], // note that this sets the chkbox id to the value of the current document id
                            array('hiddenField' => false,'onclick'=> 'SelectSection(this,this.name,this.checked);'))
                            ."</td></tr>";
echo "</table>";

foreach ($project_document['ProjectDocumentSection'] as $project_document_section): 
    echo "<h4>" .  $project_document_section['title']. "</h4>";
    echo "<table>";
    echo "<tr><td>Collection Method</td><td>" . $project_document_section['method']. "</td></tr>";
    echo "<tr><td>Collection objective</td><td>" . $project_document_section['objective']. "</td>";
    echo "<td>Select (toggle) all section variables"
            .$this->Form->checkbox($project_document['id']
                ."_".$project_document_section['id'] // and here the chkbox id is set to combination of document id and section is (using _ as separator)
                , array('hiddenField' => false))."</td></tr>";
    echo "</table>";
        echo "<table><tr><th>Full text</th><th>Type</th><th>Format</th><th>Codeset</th></tr>" ; // header for variable table
        foreach ($project_document_section['ProjectDocumentSectionVariable'] as $project_document_section_var):
            echo"<tr><td>".$project_document_section_var['variable_type'] 
                            ."</td><td>".$project_document_section_var['variable_format'] 
                            ."</td><td>";
            echo "<td>".$this->Form->checkbox($project_document['id']
                    ."_".$project_document_section['id']
                        ."_".$project_document_section_var['id'], array('hiddenField' => false))."</td></tr>";
        endforeach;
        echo "</table>";

endforeach;
// and later endforeach for the document itself

?>

And when rendered this might look something like this;

<td>Select (toggle) all variables
<input type="checkbox" name="data[Project][11]"  onclick="SelectSection(this,this.name,this.checked);" value="1" id="Project11"/>
// note that checkbox id and name include ref to document id (11)
</td></tr>

<h4>Details of Health Workers at Facility</h4>

<table>
 <tr><td>Collection Method</td><td>FW completed evaluation on paper</td></tr>
<tr><td>Collection objective</td><td>blah blah blah blah</td>
<td>Select (toggle) all section variables
<input type="checkbox" name="data[Project][11_24]"  value="1" id="Project1124"/></td></tr>
// note that checkbox id and name include ref to document id (11) and section id (24)
</table>
<table>
<tr><th>Full text</th><th>Type</th><th>Format</th><th>Codeset</th></tr>
<tr><td>Site</td><td>Categorical</td><td>Quantative</td>
<td><input type="checkbox" name="data[Project][11_24_402]"  value="1" id="Project1124402"/></td></tr>
// note that checkbox id and name include ref to document id (11), section id (24) and variable id (402)
<tr><td>Facility</td><td>Categorical</td><td>Quantative</td>
<td><input type="checkbox" name="data[Project][11_24_403]"  value="1" id="Project1124403"/></td></tr>
<tr><td>Name</td><td>Text</td><td>Quantative</td>
<td><input type="checkbox" name="data[Project][11_24_404]"  value="1" id="Project1124404"/></td></tr>
<tr><td>Position of Health Worker</td><td>Text</td><td>Quantative</td>
<td><input type="checkbox" name="data[Project][11_24_405]"  value="1" id="Project1124405"/></td></tr>
<tr><td>Description</td><td>Text</td><td>Quantative</td>
<td><input type="checkbox" name="data[Project][11_24_406]"  value="1" id="Project1124406"/></td></tr>
<tr><td>Interviewer Code</td><td>Categorical</td><td>Quantative</td>
<td><input type="checkbox" name="data[Project][11_24_407]"  value="1" id="Project1124407"/></td></tr>
<tr><td>Date of Facility Visit </td><td>Date</td><td>Quantative</td>
<td><input type="checkbox" name="data[Project][11_24_408]"  value="1" id="Project1124408"/></td></tr>
</table>

Has anyone any advice on how to approach this? I am sure there is more than one way to kill this bird..

Thanks

Paul

Was it helpful?

Solution

IF I understand your question, just use the FormHelper to create your checkboxes - they'll include the model in their id, and you can then upon click use Javascript (jQuery makes it easy) to modify checkboxes that contain '_variable', or '_section'...etc

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top