yii框架分类树扩展示例
* true 多选
* false 单选
* @var type
*/
public $isMultiple = false;
/**
* 绑定到下拉框的默认值
* @var type
*/
public $bindSelectValue = 0;
/**
* 运行
*/
public function run() {
if (is_array($this->dataProvider) && count($this->dataProvider) > 0)
$data = $this->_run($this->dataProvider);
else if (is_object($this->dataProvider) && count($this->dataProvider->rawData) > 0)
$data = $this->_run($this->dataProvider->rawData);
$this->render('tree' , array('data'=>$data));
}
/**
*
* @return type
*/
private function _run($datas){
foreach ($datas as $data)
$this->arrAll[] = $data;
$this->dataKey = array_keys($data);
$this->processData();
if ($this->treeType === true)
$data = $this->getTable();
else
$data = $this->getSelect($this->pid, $this->bindSelectValue, $this->isMultiple, $this->selectClass, $this->defaultSelectValue);
return $data;
}
/**
* 获得html
* @return type
*/
public function getHtml() {
return $this->genHtml();
}
/**
* 设置分层字段
* 表格类型
* @return string
*/
public function getItemName(){
$html = '<tr>';
foreach($this->dataKey as $v) {
if ($this->formatParam == $v)
$str = '{$strSep}';
else
$str = '';
$html .= '<td>'.$str.'{$'.$v.'}</td>';
}
$html .= '</tr>';
return $html;
}
/**
* 获取表格列名称
* @return string
*/
public function getTableHead(){
$html = '<tr>';
foreach($this->tableHead as $v)
$html .= '<th>'.$v.'</th>';
$html .= '</tr>';
return $html;
}
/**
* 获得表格形式的树
* @return string
*/
public function getTable() {
$this->strItem = $this->getItemName();
$strRe = '<table class="'.$this->tableClass.'">';
$strRe .= '<thead>'.$this->getTableHead().'</thead><tbody>';
$strRe .= $this->genHtml();
$strRe .= '</tbody></table>';
return $strRe;
}
/**
* 获取下拉框形式的树
* @param type $strName
* @param array $arrValue
* @param type $blmMulti
* @param type $strExt
* @param type $arrFirst
* @return string
*/
public function getSelect($strName = 'tree', $arrValue = array(), $blmMulti = false, $strExt = '', $arrFirst = null) {
!is_array($arrValue) && $arrValue = array($arrValue);
foreach ($this->arrIdAll as $strTemp => $arrTemp) {
$this->arrIdAll[$strTemp]['selected'] = '';
if (in_array($arrTemp['id'], $arrValue)) {
$this->arrIdAll[$strTemp]['selected'] = ' selected="selected"';
}
}
$this->strItem = '<option value=\"{$'.$this->optionValue.'}\"{$selected} title=\"{$'.$this->formatParam.'}\">{$strSep}{$'.$this->formatParam.'}</option>';
$strRe = '<select id="id_' . $strName . '" name="' . $strName . ($blmMulti ? '[]' : '') . '"';
$strRe .= ($blmMulti ? ' multiple="multiple"' : '') . (empty($strExt) ? '' : ' ' . $strExt) . '>';
if (is_array($arrFirst) && count($arrFirst) == 2) {
$strRe .= '<option value="' . $arrFirst[0] . '">' . $arrFirst[1] . '</option>';
}