How to paginate a grid with selected columns - php

In the Agile Toolkit Tutorial (Jobeet), I setup the quick data model (Page 3) and it looks nice with the CRUD test page. I tried to change a line of code in the test.php file. The problem is when I added the paginator, line of code, the data in the grid disappeared. Is this a limitation to the paginator class? Is there a quick way to get this custom grid paginated? Thanks.
Original Code displays method not defined error when adding addPaginator as shown below:
$this->add('CRUD')->setModel('Category');
//$this->add('CRUD')->setModel('Job');
$jobCRUD=$this->add('CRUD');
$jobCRUD->setModel('Job');
$jobCRUD->addPaginator(3); //This line causes an method not defined error
Modified code using setSource doesn't display an error but displays an empty grid:
class page_test extends Page {
function init(){
parent::init();
//$this->add('CRUD')->setModel('Category'); //Not needed for my example
$grid=$this->add('Grid');
//$grid->setModel('Job'); //Removed this to show custom columns
$grid->addColumn('id');
$grid->addColumn('type');
$grid->addColumn('position');
$grid->setSource('job');
$grid->addPaginator(3); //Added this to paginate the results (doesn't work & removes data)
}
}
Solution:
$this->add('CRUD')->setModel('Category');
//$this->add('CRUD')->setModel('Job');
$jobCRUD=$this->add('CRUD');
$jobCRUD->setModel('Job');
$jobCRUD->grid->addPaginator(3); // This fixed the paginator

Jobeet is for Agile Toolkit 4.1 and uses "setSource" which is obsolete.
I think if you replace setSource with setModel it should be OK.

Related

Why is Yii2 framework showing a 404 error when creating custom view page?

I'm attempting to create a custom display in yii2 framework using this code in my site controller:
/******/
public function actionChartDisplay()
{
return $this->render('chartDisplay');
}
for testing purposes I pasted the form name in my actionAbout function as a parameter to the render function in it. It worked with this:
public function actionAbout()
{
return $this->render('chartDisplay');
}
But I need to create many custom views in yii2 and this won't be a solution.
This is the error I get
I'm curious as to why it is. Since I was following this tutorial and came across this weird behaviour.
My 'chartDisplay.php' file is merely a "hello world" that does work with the action about function.
in yii2, the controllers and actions with multiple words, that are marked by capital letters are divided by - in your request, so in your case the route would be some/chart-display
Apparently as #SmartCoder pointed out it was an error on how Yii2 Handles the action functions in its controller however I didn't mark his answer as the solution right away because implementing it resulted in an error. So aside from that I'm posting the way I solved it.
So instead of using chart-display I simply changed it for "charts" like this:
public function actionCharts(){
return $this->render('charts');
}
Changed the name of my file so it fits to charts.php and it worked.

ATK4 Autocomplete issue

I'm new to ATK4.
I'm trying to implement an Autocomplete field, but I had only an error when I try to define the field.
I'm running all on a MAC with the last version (4.2.4) of ATK4. All other functions (field types) seems to work well, but when I define a field of type autocomplete I get the same error I saw on the example: http://codepad.agiletoolkit.org/autocomplete
The case is I defined a model:
class Model_Agenda extends Model_Table {
public $entity_code='Agenda';
function init(){
parent::init();
$this->addField('DATE');
$this->addField('TIME');
$this->addField('DRIVERID');
$this->addField('STUDENTID');
$this->hasOne('STUDENTID')->display(array('form'=>'autocomplete/Basic'));
}
}
and then on my Page:
$form = $this->add('Form');
$form->addField('ReadOnly','Date')->Set($_GET['date']);
$form->addField('ReadOnly','Time')->Set($_GET['time']);
$form->addField('ReadOnly','Driver')->Set($_GET['driverid']);
$client=$form->addField('autocomplete','studentid');
something simple, but nothing, I cannot get the autocomplete field, I ever get an error that say that "autocomplete.php" doesn't exist (actually the file doesn't exist and I try to download the github module, but either that module includes this file), anyway the error I get is:
Exception_PathFinder, code: 0
Additional information:
file: Form/Field/Autocomplete.php
type: php
attempted_locations:
0: /Library/WebServer/Documents/rutas/lib/Form/Field/Autocomplete.php
1: /Library/WebServer/Documents/rutas/atk4/lib/Form/Field/Autocomplete.php
2: /Library/WebServer/Documents/rutas/atk4-addons/mvc/Form/Field/Autocomplete.php
3: /Library/WebServer/Documents/rutas/atk4-addons/misc/lib/Form/Field/Autocomplete.php
class: Form_Field_Autocomplete
namespace:
orig_class: Form_Field_Autocomplete
/Library/WebServer/Documents/rutas/atk4/lib/PathFinder.php:207
Someone could help me please?
You're talking about this add-on, right: https://github.com/atk4/autocomplete?
Looks like issue with PathFinder unable to find appropriate location of namespaced addons.
Please post here part of your API_Frontend class where you add additional locations to pathfinder and also tell me something more about your folder structure (in which folder you have put autocomplete add-on files).
ATK 4.2.4 version is not last. Can you try to upgrade ATK to version 4.2.5? It's not officially released, but is available in GitHub master branch here: https://github.com/atk4/atk4?
I hope everything will work with 4.2.5 version but if not, then I'll help you solve this issue.
And one more thing - if you're creating form fields manually, then you need to set Model for that $client field. I guess it can be done with $client->setModel('Agenda');
EDIT: correct answer
In line
$client=$form->addField('autocomplete','studentid');
you should write full autocomplete field class name with namespace like this:
$client=$form->addField('autocomplete/Basic', 'studentid');
One more thing to note is to use lowercase function name. So instead of Set() use set().

Changing layout of view in Joomla 2.5

I know there are several similar topics around but I read and tried most of them but still can't figure out how to do this.
I have a written a component in Joomla 2.5 and it works so far. I have different views and I can load the views using the controller.php.
One of the views shows a table out of my data base (data about teams).
Now I'd like to have another layout of the same view which would display the data base table as a form so can change the content.
That's the file structure:
views/
- teams/
- - tmpl/
- - - default.php
- - - modify.php
- - view.html.php
That's out of the view.html.php file:
...
// Overwriting JView display method
function display($tpl = null) {
...
$this->setLayout('modify');
echo $this->getLayout();
// Display the view
parent::display($tpl);
}
I tried different combinations of setLayout, $tpl = ..., default_modify.php, etc.
but I always either get the default layout or some error like 'can't find layout modify'
I load the site with .../index.php?option=com_test&task=updateTeams
And the controller.php looks like this:
function updateTeams(){
$model = $this->getModel('teams');
$view = $this->getView('teams','html');
$view->setModel($model);
$view->display();
}
I had a similar problem, I created some kind of user profile view and wanted them to be able to edit the fields without having to create a new model for it (would have similar functions, hate redundancy...). What worked for me is to simply call the layout like this:
index.php?option=com_mycomponent&view=myview&layout=edit ("edit" would be "modify" in your case)
To do this I didn't touch the view.html.php (well I did at first but I didn't have to.). And you don't need to use the controller either. If you want to load the modify view, just add a button to your regular view linking to the modify layout. No need to change anything else.
I happen to have written a blog article about it, check it out if you want: http://violetfortytwo.blogspot.de/2012/11/joomla-25-multiple-views-one-model.html
Hope this helps.
Ok this is the problem .. you don't want another layout, you want a new MVC triad that is based on forms rather than rendering. So if you look at any of the core content components you will see in the backend they have a mvc for say ... contacts and one for contact and contact is the editor. If in the front end you will notice that com_content and com_weblinks have mvc for artice/weblink and then separate ones for editing.
You need a really different model and layout and set of actions for editng than for just rendering.
Old topic, but it might still help.
It seems that when one wants to change the layout, the $tpl must not be included in the display() or must be null.
So the previous code would be:
function display($tpl = null) {
/* ... */
$this->setLayout('modify');
// Display the view without the $tpl (or be sure it is null)
parent::display();
}

MVCGRID CSV Export error

i'm trying to export a grid i create with the object MVCgrid.
I found out that i can add the the current page the object 'MVCGrid_Export' instead of the object 'MVCGrid'that provides the export buttons and functionalities.
By the way i got an error during the export :
"Fatal error: Call to a member function getField() on a non-object in ..\agiletoolkit\atk4-addons\misc\lib\Export.php on line 42"
At that line, "$b[] = $this->__getHeaderModel()->getField($a)->caption();", i discovered that the result of "$this->_getHeaderModel()" is a string of the model i want to export, and not the object of that model , that is the item i need.
This is the code of my page:
class page_resultsShow extends Page {
function initMainPage() {
$p = $this;
$gr = $this->add('MVCGrid_Export');
$gr->setModel('results',array('name','budget','bestapplicants'));
$gr->addColumnMVC('name');
$gr->addFormatter('name','link');
$gr->addQuickSearch(array('name'));
$gr->addPaginator(20);
}
//details...
function page_details() {
... some code..
}
function defaultTemplate(){
return array('page/results');
}
}
I suppose i need to add some informations to explain to the grid the model that is handling , but i didn't find how!!
Thank you for the help
We have pushed updated export module for 4.2
Please, update atk4-addons and atk4 to make sure you are on master branch (which is now 4.2).
syntax is a bit different now, as Export now acts as controller.
class page_index extends Page {
function init(){
parent::init();
$c=$this->add("Grid");
$c->setModel("A");
$c->addPaginator(1);
$c->add("Export");
$c=$this->add("CRUD");
$c->setModel("A");
if ($c->grid){
$c->grid->addPaginator(1);
}
$c->add("Export");
}
}
From email to atk4 group:
// let's say you have grid
$export = $grid->add("Export");
// this would add export xls & export csv buttons to your grid
// if you have crud:
$export = $crud->add("Export");
// this would add export xls & export csv buttons to your grid
Now, you can easily create new "Parsers", by creating
Export_Parser_Xyz class.
then just add it to your export:
$export->add("Export_Parser_Xyz");
very important update is that it now uses dq after it has been altered by paginators, filters etc. so that it would export exactly what is displayed. Optional, is limit which by default is being removed, but can be controlled on a parser level.
this would automatically add button to grid/crud and handle data flow from respective grid/crud data source.
N.B.!
Export works only with dq based Grid and, if model is defined, attempts to load captions from model's field definitions.
PDF parser is left out at this point, as it was very specific and required specific 3rd party software.
Should you want to have old Export module follow instructions in lib/Export.php to enable compatibility mode.

widget within module in Yii

I'm trying to create a widget within the module and then load that widget from 'outside' of the module. More particularly I'm using user module written by someone else. I don't want to have a separate page for displaying a login form, therefore I tried to make a CPortlet/widget (confusion) displaying the login form. Basically, I've moved the code from LoginController into that widget. Then I try to display the widget on some random page by
<?php $this->widget('user.components.LoginForm'); ?>
However, I get an error
CWebApplication does not have a method named "encrypting".
in UserIdentity class in this line:
else if(Yii::app()->controller->module->encrypting($this->password)!==$user->password)
This happens, because I'm basically trying to execute this code within context of the app and not the module. Thus the "Yii::app()->controller->module" trick doesn't really work as expected.
What am I doing wrong:-\
Is there a better way to achieve this. I.e. display that login form in some other page, which is normally displayed by accessing login controller within user module (user/login) or is a widget the right way of doing it?
Thanks.
The quick solution
Ok, so I simply ended up doing
Yii::app()->getModule('user')->encrypting($this->password)
instead of
Yii::app()->controller->module->encrypting($this->password)
Notice that now the module must be called 'user' in the main config, but I think this allows for more flexibility. I.e. we're not bound to only use module functionality within the module.
Additional insight on displaying widget outside of the module scope
After playing more with it that's what I did. In the UserModule.php I've created a method
public static function id() {
return 'user';
}
Then everywhere where I need the module I use
Yii::app()->getModule(UserModule::id())->encrypting($this->password)
I don't like having many imports related to the module like:
'application.modules.user.models.*',
'application.modules.user.components.*',
Because we already have those imports in the UserModule.php:
public function init()
{
// this method is called when the module is being created
// you may place code here to customize the module or the application
// import the module-level models and components
$this->setImport(array(
'user.models.*',
'user.components.*',
));
}
Therefore whenever you know that some piece of functionality will be used outside of the module it's important to make sure the module is loaded. For example, in the LoginForm widget that I am trying to display NOT in one of the module controllers, I have this line of code:
$model = new UserLogin;
However, UserLogin is a model inside of the User module, and in order to be able to autoload this model we first have to make sure the module was initialised:
$module = Yii::app()->getModule(UserModule::id());
$model = new UserLogin;
I hope this will be helpful if you were stuck with the whole modules concept the way I was.
http://www.yiiframework.com/forum/index.php?/topic/6449-access-another-modules-model/ was useful but hard to find =)
You better move that encrypting() into a MyUserIdentiy class which extends CUserIdentity. Whatever the code you take to use, they putting the method in controller is a bad idea and as a result you cannot reuse that code.
The login form should still post to User/Login controller but I guess they use Yii's standard login code and you might want to modify it to use the MyUserIdentity.

Categories