Filter empty values in DetailView - yii

Is there an easy way to force DetailView in Yii2 to ignore these fields in its attributes list, that for particular model are empty?
Or the only way is to define every attribute on attributes list with own function and filter empty fields inside it (sound like a little bit of madness)?
Edit: I thought, that this is pretty self-explanatory, but it turned out, it isn't. So, basically, I want to force DetailView to ignore (not render) rows for these elements of attributes list, that have empty (null, empty string) values in corresponding model and thus would result in rendering empty table cell:

You can define template parameter of DetailView widget as a callback function with following signature function ($attribute, $index, $widget) and this callback will be called for each attribute, so you can define desired rendering for your rows:
DetailView::widget([
'model' => $model,
'template' => function($attribute, $index, $widget){
//your code for rendering here. e.g.
if($attribute['value'])
{
return "<tr><th>{$attribute['label']}</th><td>{$attribute['value']}</td></tr>";
}
},
//other parameters
]);

Would something like this work better? It preserves some of the niceties like: updated_at:datetime, which with one of the solutions above will just show the underlying value, not a converted value.
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
[
'attribute' => 'my_attribute',
'visible' => !empty($model->my_attribute)
],
]
]);

Related

Yii2 attributeLabels - wrap part of the label in a tag

Inside User model, I am using attributeLabels method to create text for the field:
public function attributeLabels()
{
return [
'terms' => 'I accept the Terms and Coditions'
];
}
And in the view I am using standard $form->field method to show the input field:
<?php $form->field($model, 'terms')->checkbox() ?>
I need to wrap Terms and Coditions inside a tag with a link. How can I do this?
The checkbox() method takes an options array where you can override the default label:
$form->field($model, 'terms')->checkbox([
'label' => 'I accept the Terms and Conditions'
]);
Detailed Checkbox Options are in the API Documentation at http://www.yiiframework.com/doc-2.0/yii-widgets-activefield.html#checkbox()-detail

Yii, custom filter is not working

My custom filter isn't working. Can anyone please correct me?
In my
public function actionAdmin($mid=null) {
// the appropriate codes here...
$date = ">= ".date("Y-m-d");
$this->render('admin', array(
'model' => $model,
'mid' => $mid,
'date'=>$date,
));
}
In my admin.php, I added this line in the appropriate field, in this case, dateEnd.
UPDATED
array(
'name'=>'dateEnd',
'htmlOptions'=>array('width'=>'150px'),
'filter'=>array('0'=>'', '1'=>$date),
),
Okay, so here's the problem. No matter what I click on, it's not filtering anything. I want it to filter either a blank space OR a date of today.
Can I please know what have I done wrong? Please feel free to correct me. Thanks!
You can't access passed variable inside cgridview. For this purpose, you can define a global variable inside your controller, and access it inside cgridview, something like this:
class Yourcontroller extends Controller {
public $date;
public function actionAdmin($mid=null) {
// the appropriate codes here...
$this->date = ">= ".date("Y-m-d");
$this->render('admin', array(
'model' => $model,
'mid' => $mid
));
}
}
Now, you can access date inside grid:
array(
'name'=>'dateEnd',
'htmlOptions'=>array('width'=>'150px'),
'filter'=>array('0'=>'', '1'=>$this->date),
),
Change this line :
'filter'=>array('0'=>'', '1'=>$date),
To this line:
'filter' => array(">= ".date("Y-m-d") => Yii::t('app', 'Still On Leave')),
Basically the first parameter is the real value, where as the second part is the View to be displayed.
In the First line, '1'=>$date means 1 value but to display a $date field on the CGridView's filter. It will take the value 1 to filter instead of the $date field.
Treat it like the Select element in HTML, where you have this line:
<select>
<option value="1">One</option>
<option value="2">Two</option>
</select>
Javascript basically takes the value instead of the word One being displayed.
This is the same explanation for it.

Creating Prestashop back-office module with settings page

I'm creating a back-office module for Prestashop and have figured out everything except the best way to display the admin page. Currently I'm using the renderView() method to display the content of view.tpl.
I would like to display a table with values and an option to add a new row. Should I just create it in the view.tpl or is there a better way? I've seen the renderForm() method but haven't figured out how it works yet.
The biggest question I have is, how do I submit content back to my controller into a specific method?
ModuleAdminController is meant for managing some kind of records, which are ObjectModels. Defauly page for this controller is a list, then you can edit each record individually or view it's full data (view).
If you want to have a settings page, the best way is to create a getContent() function for your module. Besides that HelperOptions is better than HelperForm for this module configuration page because it automatically laods values. Define the form in this function and above it add one if (Tools::isSubmit('submit'.$this->name)) - Submit button name, then save your values into configuration table. Configuration::set(...).
Of course it is possible to create some sort of settings page in AdminController, but its not meant for that. If you really want to: got to HookCore.php and find exec method. Then add error_log($hook_name) and you will all hooks that are executed when you open/save/close a page/form. Maybe you'll find your hook this way. Bettter way would be to inspect the parent class AdminControllerCore or even ControllerCore. They often have specific function ready to be overriden, where you should save your stuff. They are already a part of execution process, but empty.
Edit: You should take a look at other AdminController classes, they are wuite simple; You only need to define some properties in order for it to work:
public function __construct()
{
// Define associated model
$this->table = 'eqa_category';
$this->className = 'EQACategory';
// Add some record actions
$this->addRowAction('edit');
$this->addRowAction('delete');
// define list columns
$this->fields_list = array(
'id_eqa_category' => array(
'title' => $this->l('ID'),
'align' => 'center',
),
'title' => array(
'title' => $this->l('Title'),
),
);
// Define fields for edit form
$this->fields_form = array(
'input' => array(
array(
'name' => 'title',
'type' => 'text',
'label' => $this->l('Title'),
'desc' => $this->l('Category title.'),
'required' => true,
'lang' => true
),
'submit' => array(
'title' => $this->l('Save'),
)
);
// Call parent constructor
parent::__construct();
}
Other people like to move list and form definitions to actual functions which render them:
public function renderForm()
{
$this->fields_form = array(...);
return parent::renderForm();
}
You don't actually need to do anything else, the controller matches fields to your models, loads them, saves them etc.
Again, the best way to learn about these controller is to look at other AdminControllers.

Yii cgridview give reference to checkbox

Because when adding the CCheckBoxColumn to my vgridview in a form, in return I have no precise index for working with the checked box data's. So I try to add the 'uncheckValue' but I am unable to link it to the reference value of my table. Is there a way to access this value for the current row ? (the $data->reference in my code return a Undefined variable).
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'gview',
'dataProvider'=>$dataProvider,
'columns'=>array(
'client',
'reference',
array(
'class'=>'CCheckBoxColumn',
'id'=>'CB',
'selectableRows'=>2,
'checkBoxHtmlOptions'=>array(
'uncheckValue'=>$data->reference, ),
)),));
Tks anyone could put me on the way (if there is one... )
I finally find one way is to extend CCheckBoxColumn.
As inside this code I have access to $data. Now my form return checkbox with his 'name' as the 'reference' column of my table, so I can do further batch treatment.
The uncheckValue hidden field was not suitable as it was only giving index for unchecked fields (!).
I believe this code should not stay in view but in extension...
Any comments still welcome....
Yii::import('zii.widgets.grid.CCheckBoxColumn');
class LIndexCheckBoxColumn extends CCheckBoxColumn {
public $linkId;
public function renderDataCellContent($row,$data)
{
if($this->value!==null)
$value=$this->evaluateExpression($this->value,array('data'=>$data,'row'=>$row));
else if($this->name!==null)
$value=CHtml::value($data,$this->name);
else
$value=$this->grid->dataProvider->keys[$row];
$checked = false;
if($this->checked!==null)
$checked=$this->evaluateExpression($this->checked,array('data'=>$data,'row'=>$row));
$options=$this->checkBoxHtmlOptions;
//$name=$options['name'];
$varLink=$this->linkId;
$name=$data->$varLink;
unset($options['name']);
$options['value']=$value;
$options['id']=$this->id.'_'.$row;
echo CHtml::checkBox($name,$checked,$options);
}
}
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
'client',
'reference',
array(
'class'=>'LIndexCheckBoxColumn',
'id'=>'cb',
'selectableRows'=>2,
'linkId'=>'reference',
)),));

Always show Pager on CGridView?

I've build a CGridView menu, and I want to always display the pager
(even when it's showing all the data and the navigation is not needed)
This is the current code I have:
$this->widget('zii.widgets.grid.CGridView',
array('dataProvider'=>$search,
'columns' => $columns,
'itemsCssClass' => 'list_table',
'template' => '{pager}{summary}{items}',
'pager' => array(
'cssFile'=>false,
'class'=>'CLinkPager',
'firstPageLabel' => '<<',
'prevPageLabel' => '<',
'nextPageLabel' => '>',
'lastPageLabel' => '>>',
'header' => '',
'footer' => $footer_btns,
),
'pagerCssClass' => 'pagination',
));
You could do this by overriding the renderPager() method -- however, it seems that the pager gets put together in a few files so one way to do it by only overriding one class would be to:
override zii.widgets.grid.CGridView to add your custom renderPager() method with something like:
Yii::import('zii.widgets.grid.CGridView');
class MyGrid extends CGridView {
public function renderPager() { ... }
}
the default renderPager() function is here.
What you want to do is look for the line that tests for pager content:
if($pager['pages']->getPageCount()>1) {
and change the "else" statement to put in your default "empty" pager content, which could use the same <ul> structure. Since you are not providing any navigation for the blank view, you don't need to worry about that data if this is used in multiple places. That could look something like:
else {
echo '<div class="'.$this->pagerCssClass.'">';
## YOUR CUSTOM "EMPTY PAGER" HTML HERE ##
echo '</div>';
}
You might need to define a couple extra css classes as well. On pages where only part of the pagination is showing (e.g., the first and last page), you can use CSS to redefine the ".hidden" class(es).