multiple activecheckboxlists in yii from the same model on the same page
I have a pages with multiple activeCheckBoxLists which are actually generated in a foreach loop. The issue that I am having is that each list is generated with the same name's and id's as every other generated list. I need a way to either send an iteration or something into the id's and name's so that they are unique.
When I use jquery to select the first item of the list it selects the first item of every list.
Here is the code that generates the lists.
// collect all filter titles (level == 0, parent == 0)
$topLevelFilterTitles = $hsf->findAllByAttributes(array('level'=>'0','parent_id'=>'0'));
foreach($topLevelFilterTitles as $filterTitle):
// with each filter title find all children (level == 1, parent == filter title id)
echo "<div class='half menu split'>";
echo "<p class='uppercase-text filter-name'>" . $filterTitle->title . "</p>";
$filterOptions = $hsf->findAllByAttributes(
array(
'level'=>'1',
'parent_id'=>$filterTitle->id,
)
);
$list = CHtml::listData($filterOptions,'filter_name','title');
echo CHtml::activeCheckBoxList(
$hsf, // model
'filter_name',
$list
);
echo "</div>";
endforeach;
The generated lists show up as follows
<div class="halfmenu split">
<p class="uppercase-text filter-name">Filter Name R</p>
<input id="ytHardwareSearchFiltering_filter_name" type="hidden" value="" name="HardwareSearchFiltering[filter_name]" />
<input id="HardwareSearchFiltering_filter_name_0" value="r_value0" type="checkbox" name="HardwareSearchFiltering[filter_name][]" />
<label for="HardwareSearchFiltering_filter_name_0">r_name0</label><br/>
<input id="HardwareSearchFiltering_filter_name_1" value="r_value1" type="checkbox" name="HardwareSearchFiltering[filter_name][]" />
<label for="HardwareSearchFiltering_filter_name_1">r_name1<开发者_如何学运维/label>
</div>
<div class="halfmenu split">
<p class="uppercase-text filter-name">Filter Name T</p>
<input id="ytHardwareSearchFiltering_filter_name" type="hidden" value="" name="HardwareSearchFiltering[filter_name]" />
<input id="HardwareSearchFiltering_filter_name_0" value="t_value0" type="checkbox" name="HardwareSearchFiltering[filter_name][]" />
<label for="HardwareSearchFiltering_filter_name_0">t_name0</label><br/>
<input id="HardwareSearchFiltering_filter_name_1" value="t_value1" type="checkbox" name="HardwareSearchFiltering[filter_name][]" />
<label for="HardwareSearchFiltering_filter_name_1">t_name1</label><br/>
</div>
I need (preferably) the ID to be different between lists, but even if I could get the name different that would be a start. Thanks in advance for the help.
You can add htmloptions to the activeCheckBoxList as indicated in the API documentation. In your case I reckon you could do something like this:
echo CHtml::activeCheckBoxList(
$hsf, // model
'filter_name',
$list, array(
// Here you could use your filterTitle id or any other
// variable that will make your checkbox unique
'id' => 'chk_'.$filterTitle->id,
'name' => 'chk_'.$filterTitle->id
)
);
The Htmloptions are the 4th parameter in the instantiation of activeCheckBoxList as an array. Look at the API!
精彩评论