Cannot do filtering via a dropdown menu in Yii TbGridView (v1.x.x) - yii

I am trying to get the filtering to run although I am having some issues below is my view note that this in in a PARTIAL view :-
If I click on the header of the column it does the sorting as expected, however if I select an option from the dropdown it doesn't filter the items...
Any ideas?
<?php
$myuser = new Myuser('search');
$filterBtn = $this->widget('bootstrap.widgets.TbButton', array(
'icon' => 'filter',
'size' => 'small',
'label' => $myuser->paging ? 'View All' : 'View Less',
'htmlOptions' => array('class'=>'pull-right', 'style'=> 'margin:20px 0;'),
'url' => $myuser->paging ? array('/carrot/myuser/admin/paging/0') : array('/carrot/myuser/admin')
), true);
$this->widget('bootstrap.widgets.TbGridView',array(
'id' => 'myuser-grid','type'=>'striped bordered condensed',
'dataProvider' => $myuser->search(),
'filter' => $myuser,
'columns' => array(
array(
'id' => 'user_id',
'class' => 'CCheckBoxColumn',
'checked' => 'in_array($data->user_id, $this->grid->owner->model->student_ids)',
'checkBoxHtmlOptions' => array(
'name' => 'selected_student_id[]',
)
),
'firstname',
'surname',
'username',
array(
'name' => 'year_id',
'filter' => CHtml::activeDropDownList($myuser, 'year_id', CHtml::listData(Organisation::model()->distinctYears, 'year_id', 'year_id'), array('prompt'=>'All Years')),
'htmlOptions' => array('style' => 'text-align:center;'),
'headerHtmlOptions' => array('style' => 'text-align:left;'),
),
array(
'name' => 'form_name',
'header' => 'Form',
'filter' => CHtml::activeDropDownList($myuser, 'form_name', CHtml::listData(Organisation::model()->distinctForms, 'form_name', 'form_name'), array('prompt'=>'All Forms')),
),
array(
'name' => 'House',
'filter' => CHtml::activeDropDownList($myuser, 'House', CHtml::listData(Organisation::model()->distinctHouses, 'House', 'House'), array('prompt'=>'All Houses')),
),
),
)); ?>
My model has the following search() method:
public function search($limit = false)
{
$criteria = new CDbCriteria();
$criteria->compare('t.user_id',$this->user_id,true);
$criteria->compare('t.firstname',$this->firstname,true);
$criteria->compare('t.surname',$this->surname,true);
$criteria->compare('t.username',$this->username,true);
$criteria->compare('t.year_id',$this->year_id);
$criteria->compare('t.form_name',$this->form_name);
$criteria->compare('t.House',$this->House);
$criteria->compare('o.organisation_id',$this->organisation_id);
$criteria->group = 't.user_id';
$criteria->together = true;
return new CActiveDataProvider($this->currentUserOrganisation(), array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => $this->paging ? ($limit) ? $limit : OverviewController::PAGE_SIZE : 2000
),
'sort' => array(
'defaultOrder' => array('firstname'=>false, 'surname'=>false),
'attributes' => array(
'organisation_name' => array(
'asc' => 'organisation_name',
'desc' => 'organisation_name DESC',
'default' => 'desc',
),
'*'
)
),
));
}

I think you can't use a DropDownList as filter, but you can easily use CHTML::listData():
array(
'name' => 'year_id',
'filter' => CHtml::listData(Organisation::model()->distinctYears, 'year_id', 'year_id'),
'htmlOptions' => array('style' => 'text-align:center;'),
'headerHtmlOptions' => array('style' => 'text-align:left;'),
),
array(
'name' => 'form_name',
'header' => 'Form',
'filter' => CHtml::listData(Organisation::model()->distinctForms, 'form_name', 'form_name'),
),
array(
'name' => 'House',
'filter' => CHtml::listData(Organisation::model()->distinctHouses, 'House', 'House'),
),
This will automatically generate a DropDownList for you.
I hope this works for you.

Related

Prestashop HelperForm: switch button

I am using Prestashop HelperForm to generate a switch button. The status of the button depends on data generated from database. The problem is that the button is always set to false.
Here is the code :
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Champs pour feuille de soin'),
'icon' => 'icon-pencil'
),
'input' => array(
array(
'type' => 'hidden',
'name' => 'id_product',
),
array(
'type' => 'textarea',
'label' => $this->l('Label'),
'name' => 'contenu1'
),
array(
'type' => 'hidden',
'name' => 'id_customization_field1',
),
array(
'type' => 'radio',
'label' => $this->l('required'),
'name' => 'relab1',
'is_bool' => false,
'desc' => $this->l('required'),
'values' => array(
array(
'id' => 'label1_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'label1_off',
'value' => 0,
'label' => $this->l('Disabled')
)
)
),
array(
'type' => 'textarea',
'label' => $this->l('Label'),
'name' => 'contenu2'
),
array(
'type' => 'hidden',
'name' => 'id_customization_field2',
),
array(
'type' => 'radio',
'label' => $this->l('required'),
'name' => 'relab2',
'is_bool' => false,
'desc' => $this->l('required'),
'values' => array(
array(
'id' => 'active_on',
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'label' => $this->l('Disabled')
)
)
),
array(
'type' => 'textarea',
'label' => $this->l('Label'),
'name' => 'contenu3'
),
array(
'type' => 'hidden',
'name' => 'id_customization_field3',
),
array(
'type' => 'switch',
'label' => $this->l('required'),
'name' => 'relab3',
'is_bool' => true,
'desc' => $this->l('required'),
'values' => array(
array(
'id' => 'label3_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'label3_off',
'value' => 0,
'label' => $this->l('Disabled')
)
)
),
array(
'type' => 'textarea',
'label' => $this->l('Label'),
'name' => 'contenu4'
),
array(
'type' => 'hidden',
'name' => 'id_customization_field4',
),
array(
'type' => 'checkbox',
'label' => $this->l('Required'),
'name' => 'label4',
'class' => 't',
'multiple' => true,
'values' => array(
'query' => array($label3),
'id' => 'label4',
'name' => 'label4',
'expand' => array(
'default' => 'show',
'show' => array('text' => $this->l('show'), 'icon' => 'plus-sign-alt'),
'hide' => array('text' => $this->l('hide'), 'icon' => 'minus-sign-alt')
),
)
),
),
'submit' => array(
'title' => $this->l('Save'),
'name' => $this->l('submitAddproduct'),
)
),
);
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitUpdate';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
$helper->fields_value['id_product'] = Tools::getValue('id_product'),
'relab4' => 1,
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
);
$helper->fields_value['contenu1'] = $contenu1;
$helper->fields_value['contenu2'] = $contenu2;
$helper->fields_value['contenu3'] = $contenu3;
$helper->fields_value['contenu4'] = $contenu4;
$helper->fields_value['relab1'] = (int)$relab1;
$helper->fields_value['relab2'] = (int)$relab2;
$helper->fields_value['relab3'] = (int)$relab3;
$helper->fields_value['relab4'] = (int)$relab4;
$helper->fields_value['id_customization_field1'] = $id_customization_field1;
$helper->fields_value['id_customization_field2'] = $id_customization_field2;
$helper->fields_value['id_customization_field3'] = $id_customization_field3;
$helper->fields_value['id_customization_field4'] = $id_customization_field4;
return $helper->generateForm(array($fields_form));
I guess you have this part of the code too ($helper is your HelperForm() object):
$helper->tpl_vars = array(
'fields_value' => $this->getConfigFieldsValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
);
So with this function you need to return a relab2 value:
public function getConfigFieldsValues()
{
return array(
'relab2' => 1 // your value, 1 or 0
// other form values
);
}
You need to provide your form values to the HelperForm object.
Here is an example:
$fields_form = array(
'legend' => array(
'title' => $this->('My Form'),
),
'input' => array(
'type' => 'switch',
'label' => $this->l('required'),
'name' => 'relab2',
'is_bool' => true,
'desc' => $this->l('required'),
'values' => array(
array(
'id' => 'label2_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'label2_off',
'value' => 0,
'label' => $this->l('Disabled')
)
)
),
);
$val = getValFromDB(); //example
$helper = new HelperForm();
$helper->module = $this;
$helper->name_controller = 'example';
$helper->title = $this->displayName;
$helper->submit_action = 'example_action';
// [...]
// [...]
// Here you provide your values
$helper->fields_value = array('relab2' => $val);
return $helper->generateForm(array(array('form' => $fields_form)));
Hi I have had the same issue on prestashop 1.6 and 1.7 and I finally figured out the problem so I will share my findings with you guys.
So here is my switch :
array(
'type' => 'switch',
'label' => $this->trans('Captcha Test Mode', array(), 'Admin.Shipping.Feature'),
'name' => 'CORE_CAPTCHA_TEST_MODE',
'required' => false,
'class' => 't',
'is_bool' => true,
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->trans('Enabled', array(), 'Admin.Global'),
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->trans('Disabled', array(), 'Admin.Global'),
),
),
As you can see it has to have an explicit name CORE_CAPTCHA_TEST_MODE.
Where things become interesting is in the getConfigFormValues() method :
protected function getConfigFormValues()
{
return array(
'CORE_CAPTCHA_TEST_MODE' => Tools::getValue('CORE_CAPTCHA_TEST_MODE', Configuration::get('CORE_CAPTCHA_TEST_MODE', true)),
);
}
Unlike any other type of field where a simple Configuration::get would be enough to set the value in the field when the form is loaded for a switch we need to add Tools::getValue(...
Also got the same question, bot issue was elsewhere, just a switch button itself does nothing, you have to provide value data to it and then changes will be shown.
as other noted switch input itself:
array(
'type' => 'switch',
'label' => $this->l('Display map'),
'name' => 'my_data',
'is_bool' => true,
'values' => array(
array(
'id' => 'label2_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'label2_off',
'value' => 0,
'label' => $this->l('Disabled')
)
)
),
and provide saved data to it right bellow
$helper->fields_value['my_data'] = Configuration::get('my_data');

bootstrap.widgets.TbDetailView, Yii app

Model--
enter code here
public function searchShop()
{
$criteria = new CDbCriteria();
$criteria->compare("name", $this->category, TRUE, "OR");
$criteria->compare("shopname", $this->category, TRUE, "OR");
$criteria->compare("category", $this->category, TRUE, "OR");
return Shops::model()->findAll($criteria);
}
code----
enter code here
<?php
foreach($models as $model):
$this->widget(
'bootstrap.widgets.TbDetailView',
array(
'type'=>'bordered condensed',
'data' => array(
'id' =>array('view', 'id'=>$model->ID),
'Shop Name' => $model->shopname,
'Category' => $model->category,
'ID' => CHtml::link(CHtml::encode($model->ID), array('view', 'id'=>$model->ID))
),
'attributes' => array(
array('name' => 'Shop Name', 'label' => 'Shop name'),
array('name' => 'Category', 'label' => 'Category'),
array('name' => 'ID', 'label' => 'ID'),
),
)
);
echo "<br><hr><br>";
endforeach;
?>
I want a link on ID by clicking on it it will render view file i.e view.php of shops model
I used CHtml::link(CHtml::encode($model->ID), array('view', 'id'=>$model->ID)) but it is showing path of that view as 1
help me...
thanks in advance
try
CHtml::link(CHtml::encode($model->ID),
CController::createUrl('site/view',array('id'=>$model->ID)))
here i have assumed that action view lies in site controller. If it lies under some other module name then you can write like this "moduleName/controllerName/actionName"
Edit:
Ok you have to try a few things. TbDetailView extends CDetatilView. Now you can use TbDetailView as
$this->widget(
'bootstrap.widgets.TbDetailView',
array(
'type'=>'bordered condensed',
'data' => array(
'id' =>array('view', 'id'=>$model->ID),
'Shop Name' => $model->shopname,
'Category' => $model->category,
),
'attributes' => array(
array('name' => 'Shop Name', 'label' => 'Shop name'),
array('name' => 'Category', 'label' => 'Category'),
array('label' => 'ID', 'value' => CHtml::link(CHtml::encode($model->ID), array('view', 'id'=>$model->ID))),
),
)
);
You can also do like
$this->widget(
'bootstrap.widgets.TbDetailView',
array(
'type'=>'bordered condensed',
'data' =>$model,
'attributes' => array(
array('name' => 'shopname', 'label' => 'Shop name'),
array('name' => 'category', 'label' => 'Category'),
array('value' => CHtml::link(CHtml::encode($model->ID), array('view', 'id'=>$model->ID))
),, 'label' => 'ID'),
),
)
)
;

YiiBooster Navbar target _blank

I am using the navbar of the extension Yiibooster, but I am having problems with a subitem of the menu, I need to go to a new tab, but I can't make it.
<?php $this->widget(
'bootstrap.widgets.TbNavbar',
array(
'brand' => '<img src ="' . Yii::app()->request->baseUrl . '/images/FAVICON.png" />Inicio',
'fixed' => false,
'collapse' => true,
'items' => array(
array(
'class' => 'bootstrap.widgets.TbMenu',
'items' => array(
array(
'label' => 'Informes',
'items' => array(
array('label' => 'Listado de Activos', 'url' => Yii::app()->baseUrl.'/ZfInmuebles/verpdf',
'itemOptions' => array('target' => '_blank')),
)),
If you want go to blank only in one item of the menu, this is the best option:
'items' => array(
array(
'label' => 'Informes',
'items' => array(
array(
'label' => 'Listado de Activos',
'url' => Yii::app()->baseUrl.'/ZfInmuebles/verpdf',
'linkOptions' => array(
'target' => '_blank'
),
),
),
),
)
Yii CMenu docs: http://www.yiiframework.com/doc/api/1.1/CMenu#items-detail
add class=>'bootstrap.widgets.TbMenu' like
'items' => array(
array(
'class' => 'bootstrap.widgets.TbMenu',
'submenuOptions' => array('target' => '_blank'),
'items' => array(
array('label' => 'Listado de Activos',
'url' => Yii::app()->baseUrl.'/ZfInmuebles/verpdf',
),
)
)
You need to use $brandOptions:
NavBar::begin([
'brandLabel' => 'Label text',
'brandUrl' => '/'.Yii::$app->homeUrl,
'brandOptions' => [
'target'=>'_blank'
],
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);

Yiibooster modal from navbar

I need to call my modal widget from a navbar using Yiibooster
<?php $this->widget(
'bootstrap.widgets.TbNavbar',
array(
'brand' => 'Inicio',
'fixed' => false,
'items' => array(
array(
'class' => 'bootstrap.widgets.TbMenu',
'items' => array(
array('label' => 'Home', 'url' => '#', 'active' => true),
array('label' => 'Filtro', 'htmlOptions' => array('data-toggle' => 'modal','data-target' => '#myModal'),
array('label' => 'Nuevo', 'url' => Yii::app()->baseUrl.'/ZfInmuebles/create'),
)
)
)
)
));?>
htmlOptions doesnt work there, what can i do?
Use linkOptions instead of htmlOptions
...
array('label' => 'Filtro', 'linkOptions' => array('data-toggle' => 'modal','data-target' => '#myModal')),
...
<?php $this->widget(
'bootstrap.widgets.TbNavbar',
array(
'brand' => 'Zona Franca',
'fixed' => false,
'collapse' => true,
'items' => array(
array(
'class' => 'bootstrap.widgets.TbMenu',
'items' => array(
array('label' => 'Inicio', 'url' => Yii::app()->baseUrl),
array('label' => 'Nuevo', 'url' => Yii::app()->baseUrl.'/ZfArrendatarios/create'),
)),
array(
'class' => 'bootstrap.widgets.TbMenu',
'htmlOptions' => array('data-toggle' => 'modal','data-target' => '#myModal'),
'items' => array(
array('label' => 'Filtro', 'url' => '#myModal'),
)),
)
)
)
;?>
There you have the answer :D

Zend Framework 2: Identical validator

I have problem with Identical validator in ZF2. I have created following method:
public function getInputFilter()
{
if(!$this->inputFilter){
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'username',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim')
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 3,
'max' => 32
)
)
)
)));
$inputFilter->add($factory->createInput(array(
'name' => 'password',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim')
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 3,
'max' => 32
)
)
)
)));
$inputFilter->add($factory->createInput(array(
'name' => 'retype-password',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim')
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 3,
'max' => 32
),
array(
'name' => 'Identical',
'options' => array(
'token' => 'password' //I have tried $_POST['password'], but it doesnt work either
)
)
)
)
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
Everything works fine except Identical validator which doesn't show anything(no msg, no error).
Tips are welcome, thanks in advance.
Actually it appears your array syntax is mis-placed is all. This should fix it for you:
public function getInputFilter()
{
if(!$this->inputFilter){
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'username',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim')
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 3,
'max' => 32
)
)
)
)));
$inputFilter->add($factory->createInput(array(
'name' => 'password',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim')
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 3,
'max' => 32
)
)
)
)));
$inputFilter->add($factory->createInput(array(
'name' => 'retype-password',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim')
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 3,
'max' => 32
),
),
array(
'name' => 'Identical',
'options' => array(
'token' => 'password' //I have tried $_POST['password'], but it doesnt work either
)
)
)
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}