如何创建一个MVC模式的Joomla组件教程(十五) - 创建管理员界面 增加编辑功能 上...

现在hellos view完成了,现在需要完成hello view和 model, 他们实际复杂数据处理工作。

Hello Controller
默认的controller仅仅是展示数据,现在需要controller能处理从hellos view发出的添加,删除,修改任务,
 
 添加和编辑本质是相同的任务,他们都是显示给用户一个form来做greeting编辑,不同的是添加是一个空的form,而edit有数据,因此我们把add任务映射给edit钩子函数。下面的构造器中做了这个操作


/**
 * constructor (registers additional tasks to methods)
 * @return void
 */
function __construct()
{
    parent::__construct();

    // Register Extra tasks
    $this->registerTask( 'add'  ,     'edit' );
}

JController::registerTask是将add映射到edit钩子函数上。

现在我们处理编辑任务,controller处理edit任务相当简单,所要做的仅仅设指定view和模板,这里在编辑的同事要禁用主菜单,从而用户不能离开而不保存数据。


edit 任务的钩子函数清单:

/**
 * display the edit form
 * @return void
 */
function edit()
{
    JRequest::setVar( 'view', 'hello' );
    JRequest::setVar( 'layout', 'form'  );
    JRequest::setVar('hidemainmenu', 1);

    parent::display();
}

Hello View


Hello view 显示一个表单允许用户编辑. display要完成下面一些简单任务:

从model获取数据
创建toolbar
给模板template传递数据
调用display()填充模板

因为要处理编辑和添加,所以稍稍有点复杂。在toolbar中需要用户知道是否在添加或编辑,从而我们决定触发那个任务。

因为我们已经从model获取了数据,因而可以推断是那个任务被触发,如果是编辑,那么id字段是有数据的,否则,这个字段就是空的,由此可以确定是添加还是编辑记录。

在toolbar中还添加了两个按钮,保存和取消。
We will add two buttons to the toolbar: save and cancel. Though the functionality will be the same, we want to display different buttons depending on whether it is a new or existing record. If it is a new record, we will display cancel. If it already exists, we will display close.

display 方法的清单如下:

/**
 * display method of Hello view
 * @return void
 **/
function display($tpl = null)
{
    //get the hello
    $hello        =& $this->get('Data');
    $isNew        = ($hello->id < 1);

    $text = $isNew ? JText::_( 'New' ) : JText::_( 'Edit' );
    JToolBarHelper::title(   JText::_( 'Hello' ).': <small><small>[ ' . $text.' ]</small></small>' );
    JToolBarHelper::save();
    if ($isNew)  {
        JToolBarHelper::cancel();
    } else {
        // for existing items the button is renamed `close`
        JToolBarHelper::cancel( 'cancel', 'Close' );
    }

    $this->assignRef('hello', $hello);
    parent::display($tpl);
}

转载于:https://www.cnblogs.com/vicenteforever/articles/1633017.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值