function render() { //得到属性的值 $id = $this->_extract('id', 'parent_id'); $value = $this->_extract('value'); $out = null; //得到所有的类别 $teaType = TeaType::find()->asArray()->getAll(); //转换成item=>name数据 $teaTree = Helper_Array::toTree($teaType, 'id', 'parent_id'); $teaTree = TeaType::treeToArray($teaTree); //生成有落差下拉类表的html $out .= "<select id=\"{$id}\" name=\"{$id}\" >"; $out .= "<option value=\"-1\" >请选择类别</option>"; foreach ($teaTree as $option) { $out .= "<option value=\"{$option['id']}\" "; //是否该分类 是父分类 if ($option['id'] == $value) { $out .= "selected=\"selected\""; } $out .= ">"; for ($i = 0; $i < $option['level']; $i++) { $out .= " "; } $out .= $option['name']; $out .= "</option>"; } $out .= "</select>"; return $out; }
static function createForm($id, $action) { //创建表单及添加元素 $form = new Form_Admin_Teatype($id, $action); $form->add(QForm::ELEMENT, 'name', array('_ui' => 'textbox', '_label' => '分类的名称', 'class' => 'txt', '_req' => true, '_tips' => '请输入茶叶分类的名字不要超过40个字符'))->add(QForm::ELEMENT, 'id', array('_ui' => 'hidden'))->add(QForm::ELEMENT, 'parent_id', array('_ui' => 'admin_typelist', '_label' => '父级类名', 'class' => 'txt'))->addValidations(TeaType::meta()); return $form; }
/** * 修改茶叶类别的信息 */ function actionEdit() { $form = Form_Admin_Teatype::createForm('teatype', url('teatype/edit')); //的到栏目的id号 $typeId = $this->_context->id; $data = TeaType::find('id =?', $typeId)->query(); //导入数据到表单 $form->import($data); //是否是post提交 数据是否通过验证 if ($this->_context->isPOST() && $form->validate($_POST)) { try { //更新茶叶类别的 子类数目 $data->sonsCount($form['parent_id']->value); $data->changeProps($form->values()); $data->save(); return $this->_redirectMessage('更新茶叶类别名成功', '如果你不做出选择系统将自动跳转', url('teatype/index'), 3); } catch (QDB_ActiveRecord_ValidateFailedException $ex) { $form->invalidate($ex); } } $this->_view['form'] = $form; }