Show/Hide Grid View Action Column Based On Condition - Yii2 - php

I was trying to show/hide ActionColumn based on some condition.
In my system, 2 roles are defined : Primary & Secondary. I wanted to hide ActionColumn for Role Secondary and to show ActionColumn for Role Primary.
I got one visible attribute option from $visible. Where, 'visible'=> true and 'visible'=> false are working properly.
<?
[
'class' => 'yii\grid\ActionColumn',
'visible' => false,
.
.
.
]
But, Problem is: I wanted to set visible option as True / False dynamically based on some condition.
<?
[
'class' => 'yii\grid\ActionColumn',
'visible' => function ($data) {
if (Yii::$app->userinfo->hasRole([AR::ROLE_PRIMARY])) {
return true;
}
if (Yii::$app->userinfo->hasRole([AR::ROLE_SECONDARY])) {
return false;
}
},
.
.
.
]
I tried in this way too. But, didn't got luck. Any help/hint/suggestion is appreciable.
I searched Yii2 GridView hide column conditionally.

You can't set visible to a callable, although there is nothing to stop you setting a variable before calling the gridview.
In this case though, visibility is only dependant on whether they have the primary role, you can just use:
'visible' => Yii::$app->userinfo->hasRole([AR::ROLE_PRIMARY])

You can use conditional statement to hide particular checkbox in grid view
Here is simple code which works for me
[
'class' => 'yii\grid\CheckboxColumn',
'checkboxOptions' => function($dataProvider) {
return ["value" => ($dataProvider['tiIsPaid'] == 0)?$dataProvider['iDriverEarningId']:'',"style"=>($dataProvider['tiIsPaid'] == 0)?'':'display:none'];},
]
Here i have used simple logic to hide checkbox for particular column
Set value null or blank so it can not be selected when you click on select all checkbox
Hide check box using display none property of css
Hope this help you in hiding particular column based on your conditions.

Related

yii2 DetailView template/layout without values

I have a little problem in yii2 framework.
I have DetailView widget
<?= DetailView::widget([
'model' => $table_1,
'attributes' => [
'year',
'table_zs_field_1',
'table_zs_field_2',
'table_zs_field_3',
'table_zs_field_4',
'table_zs_field_5',
'table_zs_field_6',
'table_zs_field_7',
'table_zs_field_8',
'table_zs_field_9',
'table_zs_field_10',
'table_zs_field_11',
'table_zs_field_12',
'table_zs_field_13',
'table_zs_field_14',
'table_zs_field_15',
'table_zs_field_16',
'table_zs_field_17',
'table_zs_field_18',
'table_zs_field_19',
],
]) ?>
If i write this to code I'll see a DetailView widget with names of fields(get from model) and values.
Problem: I want to hide values and show only names of fields from model and in next time hide names and show only values. Anybody know ?
Change the $template property of the Detailview.
The Default is
$template = '<tr><th>{label}</th><td>{value}</td></tr>'
Adding
'template'=>'<tr><th>{label}</th></tr>' ,
to the config array of your DetailView should show only the names of the fields.
Adding
'template'=>'<tr><td>{value}</td></tr>',
should show only the value.
See the corresponding section in the Documentation of DetailView.

Date field do not display dropdown in popup of SugarCRM

Hy Guys!
I am facing a problem regarding datetime field display in Popup. If i add a datetime field to Advanced Search of ProspectLists it get displayed as shown below and works perfectly:
in the custom modules ProspectLists searchdefs advanced_search array it is defined as :
array (
'type' => 'datetime',
'label' => 'LBL_DATE_ENTERED',
'width' => '10%',
'default' => true,
'name' => 'date_entered', ),
but when i try to select a ProspectList from a Prospect List subpanel in Campaigns, the popup that gets displayed render the date field with out dropdown as shown below:
The other problem is this that when i perform search from popup for a specific date it displays nothing.
I am using SugarCRM CE 6.5.11.
Any idea how to display dropdown with date field.?
In method SugarFieldBase::isRangeSearchView you should check condition
$_REQUEST['action']!='Popup'
File include/SugarFields/Fields/Base/SugarFieldBase.php
I remove it from conditions.
protected function isRangeSearchView($vardef)
{
//return !empty($vardef['enable_range_search']) && !empty($_REQUEST['action']) && $_REQUEST['action']!='Popup';
return !empty($vardef['enable_range_search']) && !empty($_REQUEST['action']);
}
I think what you're looking for is the "Ranged Search" attribute.
You can enable it in studio by going to the custom field and checking the "Enable Range Search" checkbox.
Or, you can edit the custom/modules/{module}/metadata/SearchFields.php and add the following to the field in question:
'enable_range_search' => true

Add product selection widget to custom attribute in Magento

Does anyone know how can I add a custom product attribute with a widget renderer?
You can see this in Promo rules if you select SKU you'll got an Ajax popup with product selection.
so how would I go about it?
in :
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY...
In other words, how can I use a widget to select custom attribute values?
EDIT:
The scenario is as follows:
I would like to create a product attribute that will, upon a button click, open a product selection widget.
After the selection, the selected SKU's will go in in a comma delimited format.
This behavior can be seen in the catalog and shopping cart price rules.
If you filter the rule by SKU (SKU attribute must be enabled to "apply to rules"), you'll get a field and a button that will open the product selection widget.
Here is some thoughts that should get you going on the right track:
First, in a setup script, create your entity:
$installer->addAttribute('catalog_product', 'frontend_display', array(
'label' => 'Display Test',
'type' => 'varchar',
'frontend_model' => 'Test_Module/Entity_Attribute_Frontend_CsvExport',
'input' => 'select',
'required' => 0,
'user_defined' => false,
'group' => 'General'
));
Make sure to set the frontend_model to the model that you are going to use. The frontend model affects the display of the attribute (both in the frontend and the adminhtml sections).
Next, create yourself the class, and override one or both of the following functions:
public function getInputType()
{
return parent::getInputType();
}
public function getInputRendererClass()
{
return "Test_Module_Block_Adminhtml_Entity_Renderer_CsvExport";
}
The first (getInputType()) is used to change the input type to a baked in input type (see Varien_Data_Form_Element_* for the options). However, to set your own renderer class, use the latter function - getInputRendererClass(). That is what I am going to demonstrate below:
public function getElementHtml()
{
return Mage::app()->getLayout()->createBlock('Test_Module/Adminhtml_ExportCsv', 'export')->toHtml();
}
Here, to clean things up, I am instantiating another block, as the element itself doesn't have the extra functions to display buttons and the like.
Then finally, create this file:
class Test_Module_Block_Adminhtml_ExportCsv extends Mage_Adminhtml_Block_Widget
{
protected function _prepareLayout()
{
$button = $this->getLayout()->createBlock('adminhtml/widget_button')
->setData(array(
'label' => $this->__('Generate CSV'),
'onclick' => '',
'class' => 'ajax',
));
$this->setChild('generate', $button);
}
protected function _toHtml()
{
return $this->getChildHtml();
}
}
This doesn't cover the AJAX part, but will get you very close to getting the rest to work.

set the attributes in PHP 5

In my Zend framework I have two row one rows contains state dropdown with label state and the other contains a text box with label other state. Below is the code:
'state' => array('select', array(
'required' => true,
'decorators' => $elementDecorators,
'label' => 'State:',
'multiOptions' => $values["state"]
)),
'other_state' => array('text', array(
'required' => true,
'filters' => array('StringTrim'),
'decorators' => $elementDecorators,
'label' => 'Other State:',
'class' => 'other_state',
))
Here the other state is set as required. I need it required only when the user select "Other" value from the state drop down.
Client Side:
jQuery solution:
Showing your HTML output would have been a help here. But the following will add the attribute required if other is selected - this will also enable the input and disable it so the user can only enter something in other state, if they select other:
$("#state").change(function(){
if ($(this).val() == "other"){
$("#other_state").removeAttr("disabled");
$("#other_state").attr("required", "required");
}
else {
$("#other_state").removeAttr("required");
$("#other_state").attr("disabled", "true");
}
});
See a demo here
The above will do the validation on the clients side - with jQuery, however if the user has javascript turned off, it would allow the user to select other and leave other_state blank!
Server Side:
Zend solution:
What you should also do is add some validation to the zend_form. However, you can't add them the normal way - if you added a validator to say other_state can't be empty - you would have an error when a state is selected and you want it to be empty.
In your form class you could override the isValid call to add your custom validation, see the discussion here: There is another example on how to do this here
/**
/* override the isValid function of Zend_Form
/* to set a required field based on a condition
*/
public function isValid($value) {
// Check the key exists in the stack, and if its set to other:
if (array_key_exists('state', $value) && $value['state'] == 'other') {
// It is so make sure other_state is a required field:
$this->other_state->setRequired(true);
}
parent::isValid($value);
}

Magento add a column to backend newsletter gridview

In the magento system, I added the columns subscriber_firstname and subscriber_lastname to the newsletter_subscriber db table.
In the admin area of magento, I want the Newsletter>Newsletter Subscribers grid table to show:
customer first name if it exists, otherwise show newsletter_subscriber.subscriber_firstname if it exists, otherwise show nothing
customer last name if it exists, otherwise show newsletter_subscriber.subscriber_lastname if it exists, otherwise show nothing
Which magento files do I need to edit to make this work? How do I go about editing the files to make this work?
app/code/core/Mage/Adminhtml/Block/Newsletter/Subscriber/Grid.php
You'll want to condition this based off if subscriber_firstname or subscriber_lastname have values or not:
$this->addColumn('subscribername', array(
'header' => Mage::helper('newsletter')->__('Subscriber First Name'),
'index' => 'subscriber_firstname',
'default' => '----'
));
Also, make sure to make a copy of the core files and NOT edit them directly!
the quick and easy solution is to create a column render and select the correct field based on the subscriber type e.g.
app/code/local/Mage/Adminhtml/Block/Newsletter/Subscriber/Renderer/FirstName.php
class Mage_Adminhtml_Block_Newsletter_Subscriber_Renderer_FirstName extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {
public function render(Varien_Object $row) {
$value = '';
if ($row->getData('type') == 2) {
$value = $row->getData('customer_firstname');
}
else {
$value = $row->getData('subscriber_firstname');
}
return $value;
}
}
then add your render to a local copy of the subscriber grid class
app/code/local/Mage/Adminhtml/Block/Newsletter/Subscriber/Grid.php
$this->addColumn('firstname', array(
'header' => Mage::helper('newsletter')->__('First Name'),
'index' => 'customer_firstname',
'default' => '----',
'renderer' => 'Mage_Adminhtml_Block_Newsletter_Subscriber_Renderer_FirstName'
));
Note. the search and sort will not work on the subscriber name fields, to get this working you will need to extend app/code/core/Mage/Newsletter/Model/Mysql4/Subscriber/Collection.php

Categories