在Joomla前端使用JToolbar或JToolbarHelper进行自定义组件的CRUD


Using JToolbar or JToolbarHelper for CRUD in Joomla frontend for the custom component

这些类适用于哪些情况?我一直在试着用这两种方法,但都不行。生成了组件骨架,并且在管理员端有CRUD操作。我尝试使用JToolbarHelper从这个生成的代码,像这样在mycomponent/view.html.php:

// Overwriting JView display method
function display($tpl = null)
{
    // Include helper submenu
    InvoiceHelper::addSubmenu('invoice');
    // Assign data to the view
    $this->items = $this->get('Items');
    $this->pagination = $this->get('Pagination');
    // Check for errors.
    if (count($errors = $this->get('Errors'))){
        JError::raiseError(500, implode('<br />', $errors));
        return false;
    };
    // Set the toolbar
    $this->addToolBar();
    // Show sidebar
    $this->sidebar = JHtmlSidebar::render();
    // Display the view
    parent::display($tpl);
}
protected function addToolBar()
{
    JLoader::register('JToolbarHelper', JPATH_ADMINISTRATOR.'/includes/toolbar.php');
    $canDo = InvoiceHelper::getActions();
    JToolBarHelper::title(JText::_('Invoice Manager'), 'invoice');
    if($canDo->get('core.create')){
        JToolBarHelper::addNew('invoic.add', 'JTOOLBAR_NEW');
    };
    if($canDo->get('core.edit')){
        JToolBarHelper::editList('invoic.edit', 'JTOOLBAR_EDIT');
    };
    if($canDo->get('core.delete')){
        JToolBarHelper::deleteList('', 'invoice.delete', 'JTOOLBAR_DELETE');
    };
 }

但是它甚至没有出现在页面上。

然后我遇到了这个教程http://docs.joomla.org/J3.x:Using_the_JToolBar_class_in_the_frontend,它有点工作,除了我无法想象实现像一个带有复选框的实体列表和每个实体的操作。我不清楚如何使用这种方法处理表单提交,似乎是通过JS实现的,对吗?

那么,请告诉我,第一种方法有什么区别,为什么第一种方法甚至没有使工具栏出现?

我知道这是很久以前的事了,但我一直在寻找实现相同的结果,并找到了以下内容来加载页面上的工具栏。使用上面的代码:

protected function addToolBar()
{
    JLoader::register('JToolbarHelper', JPATH_ADMINISTRATOR.'/includes/toolbar.php');
    $canDo = InvoiceHelper::getActions();
    JToolBarHelper::title(JText::_('Invoice Manager'), 'invoice');
    if($canDo->get('core.create')){
        JToolBarHelper::addNew('invoic.add', 'JTOOLBAR_NEW');
    }
    if($canDo->get('core.edit')){
        JToolBarHelper::editList('invoic.edit', 'JTOOLBAR_EDIT');
    }
    if($canDo->get('core.delete')){
        JToolBarHelper::deleteList('', 'invoice.delete', 'JTOOLBAR_DELETE');
    }
    $this->toolbar = JToolbar::getInstance(); // <<<---------- ADD THIS TO METHOD!
 }

然后在你的视图中这样做:

<?php echo $this->toolbar->render(); ?>

希望这有帮助!!享受。