yii中的多个activecheckboxlists来自同一页面上的同一模型

时间:2011-09-08 16:27:03

标签: php activerecord yii

我有一个包含多个activeCheckBoxLists的页面,它们实际上是在foreach循环中生成的。我遇到的问题是每个列表生成的名称和id都与其他生成的列表相同。我需要一种方法来发送迭代或其他东西到id和名称中,以便它们是唯一的。

当我使用jquery选择列表的第一项时,它会选择每个列表的第一项。

以下是生成列表的代码。

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

生成的列表显示如下

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

我需要(最好)ID在列表之间有所不同,但即使我可以得到不同的名称,这将是一个开始。在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您可以将htmloptions添加到activeCheckBoxList,如API documentation中所示。在你的情况下,我认为你可以做这样的事情:

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

Htmloptions是activeCheckBoxList作为数组实例化的第4个参数。看看API!