The Joomla! Forum ™



Forum rules


Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
PostPosted: Tue Dec 14, 2010 10:04 am 
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Sun Jul 12, 2009 12:04 pm
Posts: 204
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:
<?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:
<?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:
<?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:
<?php
defined( '_JEXEC' ) or die( 'Restricted access' ); ?>
<h1>My Component</h1>


views/myview/view.html.php
Code:
<?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:
<?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


Top
 Profile  
 
PostPosted: Tue Dec 14, 2010 7:01 pm 
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Tue Jul 21, 2009 3:30 pm
Posts: 226
Location: United Kingdom
In your controller file with the function add should it not be like this?

Code:
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:
<?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 --


Top
 Profile  
 
PostPosted: Tue Dec 14, 2010 8:18 pm 
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Sun Jul 12, 2009 12:04 pm
Posts: 204
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:
Quote:
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:
<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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 



Who is online

Users browsing this forum: No registered users and 29 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group