To make it easy, I have a GridView and I have to pass more parameters to the Ajax Call when I change page. The default parameters are:
current controller;
page number;
ajaxVar (default ajax) with the grid id as default;
I need to pass more parameters because the controller needs more parameters to recreate the correct grid.
On google I have found no answer.
Any advice?
Thanks!
Yii is extremly extendable, so you can create a Custom Pagination Widget. Firstly, have a look at this tutorial:
How to create a Custom Pagination Widget for Yii Framework
Here is example from our Project for createPageButton() function in extended Pagination:
/**
* Creates a page button.
* You may override this method to customize the page buttons.
*
* #param string $label the text label for the button
* #param integer $page the page number
* #param string $class the CSS class for the page button. This could be 'page', 'first', 'last', 'next' or 'previous'.
* #param boolean $hidden whether this page button is visible
* #param boolean $selected whether this page button is selected
* #return string the generated button
*/
protected function createPageButton($label, $page, $class, $hidden, $selected) {
if ($hidden || $selected)
$class .= ' ' . ($hidden ? 'disabled' : 'active');
return CHtml::tag('li', array(
'class' => $class
), CHtml::link($label, $this->createPageUrl($page).'&pageSize='.$this->pageSize));
}
Take a look at following code line:
CHtml::link($label, $this->createPageUrl($page).'&pageSize='.$this->pageSize)
Url Attribute is &pageSize=, and you can extend to any parameters you need with i.e. . '&myParameter=any_parameter'
This is definitely the way you can solve your problem and I hope it will help you. If you have any questions, feel free to ask.
Related
Is it possible to show a user of PhpStorm what the possible function arguments are, not just the type of argument?
For example, I have a function that will show the programmer that two strings are needed, as shown in this graphic:
However, I would like the hint to show the possible values for each variable - tag and tag_type in this example; e.g.,
The possible values for tag are "full, view, edit, add, or delete".
The possible values for variable tag_type is a list of about 10 or so activities in the database.
Here is the code I have. Can it be changed to show the user in PhpStorm what the allowed variable values are?
/**
* #param string $tag
* #param string $tag_type
* #return int
*
* [tag] = full, view, edit, add, or delete
* [type] = all, activities, grades, orders, people, schools, users, team_classes,
* coaches, team_parents, or organization
*
* by default, if leave arguments blank, you are asking if the current user is a full admin,
*/
public function isAdmin(string $tag = '', string $tag_type = ''){
if ($this->isFullAdmin())
return true;
return $this->tagas()
->where('tag', '=', 'full')
->where('tag_type', '=', $tag_type)
->orWhere(function($query) use ($tag, $tag_type) {
$query->where('tag','=',$tag)
->where('tag_type','=', $tag_type);
})->count();
}
Okay, I came up with this, but it doesn't seem to be working. I checked my php version with phpversion() and it is 8.0.2.
public function isAdmin(
#[ExpectedValues(['all', 'activities', 'grades', 'orders', 'people', 'schools', 'users',
'team_classes','coaches', 'team_parents', 'organization'])] string $tag_type = '',
#[ExpectedValues(['*', 'full', 'view', 'edit', 'add', 'delete'])] string $tag = '*'
){
Yet it has only changed the type hinting slightly.
It shows [ExpectedValues] but doesn't show the actual expected values?
You can use PhpStorm Advanced Metadata. In particular: Arguments Accepted by a Method functionality -- https://www.jetbrains.com/help/phpstorm/ide-advanced-metadata.html#expected-arguments.
Make a separate PHP-alike file named .phpstorm.meta.php and place it in the project root. PhpStorm will parse and use the data from this file in appropriate places. Works with any modern PHP version; it is used by Laravel IDE Helper as well as by Symfony plugin to name a few.
<?php
namespace PHPSTORM_META {
expectedArguments(\AUser::isAdmin(), 0, 'full', 'view', 'edit', 'add', 'delete');
}
For PHP 8 you can use custom #[ExpectedValues] PHP attribute made by JetBrains:
https://github.com/JetBrains/phpstorm-attributes#expectedvalues
https://blog.jetbrains.com/phpstorm/2020/12/phpstorm-2020-3-release/#expectedvalues
This way all values are stored together with the method itself and not a separate file.
<?php
declare(strict_types=1);
use JetBrains\PhpStorm\ExpectedValues;
class AUser {
/**
* Bla-bla ...
*
* By default, if leave arguments blank, you are asking if the current user is a full admin.
*
* #param string $tag
* #param string $tag_type
* #return int
*
*/
public function isAdmin(
string $tag = '',
#[ExpectedValues(['all', 'activities', 'grades', 'orders', 'people', 'schools', 'users', 'team_classes', 'coaches', 'team_parents', 'organization'])]
string $tag_type = ''
)
{
if ($this->isFullAdmin()) {
return true;
}
return $this->tagas()
->where('tag', '=', 'full')
->where('tag_type', '=', $tag_type)
->orWhere(function($query) use ($tag, $tag_type) {
$query->where('tag','=',$tag)
->where('tag_type','=', $tag_type);
})->count();
}
}
$user = new AUser();
$user->isAdmin('full', '');
The IDE can also hint you that an unexpected value is used:
NOTE: it's a Weak Warning severity so it's not super visible (you may adjust that in the inspection settings):
Hello everybody I come towards you because I have small one concerns and I hope that to go to be able to you help me, I to create a shop with symfony but I have a small problem with the function "render ()" when I makes cross id in the controller kind by putting 3 or 4 for example it works but when I puts him dynamically that does not work
i share my code
my controller
/**
* #Route("/produits-views/{id}", name="product-views", methods="GET")
* #param int $id
* #return Response
*/
public function viewsProduct(int $id): Response {
$product = $this->getDoctrine()->getRepository(Product::class)->find($id);
return $this->render('inc/views-product.html.twig', [
'productViews' => $product
]);
}
and my function render
{{ render(controller('App\\Controller\\FrontController::viewsProduct',
{id: productViews.id}
)) }}
i include this on footer.html.twig and footer is include in base.html.twig
He takes out to me a message of this type
Variable "productViews" does not exist.
thank you for you helps !
change your action to let Symfony takes care of the fetching process:
/**
* #Route("/produits-views/{id}", name="product-views", methods="GET")
* #param Product $product
* #return Response
*/
public function viewsProductAction(Product $product): Response {
return $this->render('inc/views-product.html.twig', [
'productViews' => $product
]);
}
calling render with productViews.id in base.twig requires that in every view that extends this template need to have the productViews variable accessible which means that it have been provided to the view with render parameters.
you need to think about it, does all the views you have provide this variable like a user in the session if not remove it, you can alter the footer content from the view if you need to show the product according to each view using a dedicated Block for footer.
Using cakephp 3, I have a boolean [tinyint(1)] in a table, and the edit and add templates have a check box on the form, but how do I get the index and view templates to display a string like true/false or yes/no instead of 1/0. Do I map them over in the controller actions, or is there a option I can add to the templates?
The answers given both work fine.
I created a Helper class in /src/View/Helper/FormatBooleanHelper.php as below:
<?php
/*
* FormatBooleanHelper
*
* CakePHP Helper created to format boolean values in either Yes/No or True/False text.
* By: Jared Testa
*
*/
namespace App\View\Helper;
use Cake\View\Helper;
class FormatBooleanHelper extends Helper
{
/**
* yesNo method
*
* #param boolean| $value boolean
* #return string
*
*/
public function yesNo($value, $showNo = true) {
return ($value) ? "Yes" : (($showNo) ? "No" : "");
}
/**
* trueFalse method
*
* #param boolean| $value boolean
* #return string
*
*/
public function trueFalse($value, $showFalse = true) {
return ($value) ? "True" : (($showFalse) ? "False" : "");
}
}
?>
The helper is used in the standard convention by including $this->loadHelper('FormatBoolean'); in your initialize method in the AppView.php file.
You then use the Helper in your view by including $this->FormatBoolean->yesNo($booleanVariable) or $this->FormatBoolean->yesNo($booleanVariable, false) in your view. The latter example will leave the field blank in a false response.
Overkill? Perhaps...but I think it fits into the CakePHP structure, and it was a good exercise in creating your own helper.
I'm using the helper in CakePHP version 3.3.4. Hope this helps someone in the future.
simply:
<?= ($var)?'yes':'no' ?>
When you go in to display the data you can choose to display a string instead of the int. This is a simplified approach since you didn't provide any code or other information:
In the view, $isTrue being the boolean:
<?php if($isTrue){echo "true";}else{echo "false";} ?>
I added (for bottom, jtesta answer) method to get graphically representation - "check" or "x" using Foundation Icon Fonts 3:
public function checkX($value, $showFalse = true) {
return ($value) ? '<i class="fi-check"></i>' : (($showFalse) ? '<i class="fi-x"></i>' : '');
}
Image how it loks like
The main page on my website is an empty link, like:
www.randomlink.com/
That's the controller with the "/" route. The problem is that I have to use get parameters here, according to the following pattern:
key1/value1/key2/value2
I add these parameters on form submit, and the form redirects back to the main page.
The problem is that, as you can see, I get:
www.randomlink.com/key1/value1/key2/value2
And thus it opens key1 controller, instead of the default one.
/**
* Display dashboard
*
* #Route("/{path}",
* name="dashboard",
* defaults={"path" = "-1"},
* requirements={"path" = ".+"})
* #Template()
*/
public function displayAction($path, Request $request)
{
if($_POST)
{
// add get parameters to $path
return $this->redirect($this->generateUrl('dashboard', ['path' => $path]));
}
// do something
}
How can I solve this issue?
Probably your routing configuration order is not correct: see "Earlier Routes always Win" in the docs
Workaround: What about using query string like: www.randomlink.com/?path=key1/value1/key2/value2, then $request->query->get('path') ?
is there a way to customise the Yii CListView Pagination object to show
previous 1 of n pages next
rather then
previous 1,2,3 next ?
thanks
You cannot do this by simply passing parameters to the CLinkPager in order to modify its output. So, there is no more elegant method.
But you can very easily override the Pager class by extending CLinkPager and just change the createPageButtons()-Method in the following way:
Yii::import('web.widgets.pagers.CLinkPager');
class YourLinkPager extends CLinkPager{
/**
* Creates the page buttons.
* #return array a list of page buttons (in HTML code).
*/
protected function createPageButtons()
{
if(($pageCount=$this->getPageCount())<=1)
return array();
list($beginPage,$endPage)=$this->getPageRange();
$currentPage=$this->getCurrentPage(false); // currentPage is calculated in getPageRange()
$buttons=array();
// first page
$buttons[]=$this->createPageButton($this->firstPageLabel,0,self::CSS_FIRST_PAGE,$currentPage<=0,false);
// prev page
if(($page=$currentPage-1)<0)
$page=0;
$buttons[]=$this->createPageButton($this->prevPageLabel,$page,self::CSS_PREVIOUS_PAGE,$currentPage<=0,false);
/*
* !!! change has been made here !!!
*/
$buttons[]='<li>Page '.$this->getCurrentPage(false).' of '.$this->getPageCount().'</li>';
// next page
if(($page=$currentPage+1)>=$pageCount-1)
$page=$pageCount-1;
$buttons[]=$this->createPageButton($this->nextPageLabel,$page,self::CSS_NEXT_PAGE,$currentPage>=$pageCount-1,false);
// last page
$buttons[]=$this->createPageButton($this->lastPageLabel,$pageCount-1,self::CSS_LAST_PAGE,$currentPage>=$pageCount-1,false);
return $buttons;
}
}
http://www.yiiframework.com/forum/index.php/topic/5776-customise-the-clinkpager/