How to load a specific view in my component ?

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
duddy67
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 218
Joined: Sun Jul 12, 2009 12:04 pm

How to load a specific view in my component ?

Postby duddy67 » Tue Dec 14, 2010 10:04 am

Hi,

I just can't understand the principle of loading views in a component.
For now I just want to load a different empty view when I click on the
Add new button of the default view.
Here's my very simple code:

my files:

admin.mycomponent.php
controller.php
views/mycomponent/view.html.php
views/mycomponent/tmpl/default.php
views/myview/view.html.php
views/myview/tmpl/default.php

admin.mycomponent.php

Code: Select all

<?php
defined('_JEXEC') or die('Restricted Access');

require_once(JPATH_COMPONENT.DS.'controller.php');

if($controller = JRequest::getWord('controller'))
{
  $path = JPATH_COMPONENT.DS.'controllers'.DS.$controller.'.php';

  if(file_exists($path))
    require_once $path;
  else
    $controller = '';
}

$classname = 'MyComponentController'.$controller;
$controller = new $classname();
$controller->execute(JRequest::getWord('task'));
$controller->redirect();


controller.php

Code: Select all

<?php
defined('_JEXEC') or die('Restricted Access');

jimport('joomla.application.component.controller');


class MyComponentController extends JController
{
  function display()
  {
    $view =& $this->getView( 'mycomponent', 'html' );
    $view->display();
  }


  function add()
  {
    $view =& $this->getView( JRequest::getVar( 'view', 'myview' ), 'html' );
    $view->add();
  }
}


views/mycomponent/view.html.php

Code: Select all

<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.view');
 

class MyComponentViewMyComponent extends JView
{
  function display($tpl = null)
  {
    JToolBarHelper::title(JText::_('My Component'), 'generic.png');
    JToolBarHelper::deleteList();
    JToolBarHelper::editListX();
    JToolBarHelper::addNewX();
 
    parent::display($tpl);
  }
}


views/mycomponent/tmpl/default.php

Code: Select all

<?php
defined( '_JEXEC' ) or die( 'Restricted access' ); ?>
<h1>My Component</h1>


views/myview/view.html.php

Code: Select all

<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
 
jimport( 'joomla.application.component.view');

class MyDmsViewMyView extends JView
{
  function display($tpl = null)
  {
   JToolBarHelper::title( JText::_('"My Component - New"'), 'generic.png');
   JToolBarHelper::save();
   JToolBarHelper::apply();
   JToolBarHelper::cancel();

   parent::display($tpl);
  }
}


views/myview/tmpl/default.php

Code: Select all

<?php
defined( '_JEXEC' ) or die( 'Restricted access' ); ?>
<h1>My View</h1>

So far I just have the mycomponent view loaded but I can't have the myview view when
I click on the New button.
I probably missed something but I don't know what.

Can someone help me ?


Thanks for advance

aaron harding
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 226
Joined: Tue Jul 21, 2009 3:30 pm
Location: United Kingdom
Contact:

Re: How to load a specific view in my component ?

Postby aaron harding » Tue Dec 14, 2010 7:01 pm

In your controller file with the function add should it not be like this?

Code: Select all

function add()
  {
    $view =& $this->getView( 'myview', 'html' );
    $view->display();
  }


I'm not familiar with the getView method so i don't know the correct way of doing it. I personally use JRequest::setVar when changing views example using your controller.php:

Code: Select all

<?php
defined('_JEXEC') or die('Restricted Access');

jimport('joomla.application.component.controller');


class MyComponentController extends JController
{
  function display()
  {
    JRequest::setVar('view', 'mycomponent');
    parent::display();
  }


  function add()
  {
    JRequest::setVar( 'view', 'myview' );
    parent::display();
  }
}


You can see example of how the controller file is created here: http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4_-_Creating_an_Administrator_Interface#Controllers
Hope this helps
-- Aaron Harding - Qlue --
-- http://www.qlue.co.uk --

duddy67
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 218
Joined: Sun Jul 12, 2009 12:04 pm

Re: How to load a specific view in my component ?

Postby duddy67 » Tue Dec 14, 2010 8:18 pm

Thanks for your response. :)

Using setVar with the display method is correct
but it's not enought to make it works.

Here's a info I grabed on the web:
The buttons on the toolbar are designed to work with the form named
adminForm. Once adminForm is added with the hidden variable task, the
buttons will function as expected.

So to make it all works I have to add this form in the default.php files:

Code: Select all

<form action="index.php" method="post" name="adminForm">
...
<input type="hidden" name="option" value="com_component" />
<input type="hidden" name="task" value="" />
</form>

and now I can get the view. 8)

Hope it helps


Return to “Joomla! 1.5 Coding”

Who is online

Users browsing this forum: No registered users and 6 guests