【问题标题】:cakephp checkbox naming patternscakephp 复选框命名模式
【发布时间】:2013-06-10 23:00:55
【问题描述】:

(已编辑以提供更清晰的示例) 我有document->section->variables的cakephp模型结构

在一个表单中,我希望能够列出所有变量,但嵌套在文档和节级别中。那一点没问题。

我想要变量级别的每个项目旁边的复选框 - 以允许选择变量。

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

我想要一个文档级别的复选框,这样当被选中时,它将选择该文档变量的所有复选框(即在所有部分中。)

为方便起见,我将复选框命名为(好吧,我已经设置了 ID)(伪代码):

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

我希望如果我选择节级别复选框,我可以通过在变量级别复选框的所有 id 开头使用文档 id_Section_id 存根来设置该部分的所有变量级别复选框......但也许我是抓稻草?

以下是各个级别的复选框示例:(为布局道歉)

    <?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

?>

渲染后可能看起来像这样;

<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>

有人对如何解决这个问题有任何建议吗?我敢肯定有不止一种方法可以杀死这只鸟..

谢谢

保罗

【问题讨论】:

    标签: cakephp checkbox setting


    【解决方案1】:

    如果我理解您的问题,只需使用 FormHelper 创建您的复选框 - 他们会将模型包含在其 id 中,然后您可以单击使用 Javascript(jQuery 使其变得容易)来修改复选框包含“_variable”或“_section”...等

    【讨论】:

    • 这是我不确定戴夫的最后一点。您如何按部分名称“过滤”复选框(例如,使用 LIKE 运算符的 SQL 等效项)。我找不到任何关于如何按部分名称(或 ID)选择复选框的信息。
    • 请作为一个专门与 javascript 相关的新问题提出这个问题。
    • 感谢 Dave,我得到了正确解决方案的指导,现在可以从不同级别选择变量。
    猜你喜欢
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多