2013-05-11 137 views
2

我使用的CheckBoxList像所謂默認警予

CHtml::checkBoxList('Interests', $selectedInterests, CHtml::listData($interests, 'interest_id','interest'), array('uncheckValue'=>'0',''checkAll' => 'Check all')); 

我已經使用的選項,有一個「檢查所有」框的CheckBoxList檢查所有的,但我想檢查所有箱子默認情況下當用戶第一次進入該頁面時。然後他們可以取消那些不適用的檢查。

當用戶首次使用Yii的複選框列表進入頁面時,如何默認選中所有框?

回答

3

您應該將$ selectedInterests中的checkBoxList的所有值作爲數組傳遞。 我現在不能測試,但或許這應該工作:

$checked=!is_array($select) && !strcmp($value,$select) || is_array($select) && in_array($value,$select); 

其中$選擇:

CHtml::checkBoxList('Interests', CHtml::listData($interests, 'interest_id','interest_id'), CHtml::listData($interests, 'interest_id','interest'), array('uncheckValue'=>'0',''checkAll' => 'Check all')); 

在該方法的源代碼,以一看,你應該在這一行返回true是你的$ selectedInterest,你的情況下的$ value是你的每個'interest_id'屬性。

+0

它的工作原理!謝謝。 – goose 2013-05-11 21:17:29

1

checkBoxList有三個參數。

  1. 第一個參數是字段名
  2. 第二參數是選擇鍵的陣列。該參數使複選框列表被選中。
  3. 第三個參數是選擇
  4. 陣列

示例代碼:

$books = CHtml::listData(Book::model()->findAll(), 'id', 'name'); 
$selected_keys = array_keys(CHtml::listData($model->books, 'id' , 'id')); 
echo CHtml::checkBoxList('Author[books][]', $selected_keys, $books); 

更多細節都在我的博客文章可供選擇: How to make Yii checkBoxList selected