Remove empty option from listData at CGridView filter - yii

I would like to remove the empty or first option of list data value.
I have Bankaccount model and it has a list, so I need to prevent from users to select all.
I already selected the first one of that list as a default, but now the problem is the empty option that can let user to select all Bankaccount still exist, so how can I remove.
This is my code
array(
'name' => 'bank_account_id',
'type' => 'raw',
'value' => '$data->bankaccount->BankAccountName',
'filter' => Chtml::listData(Bankaccount::model()->findAll(array('order' => 'name DESC')), "id", "BankAccountName"),
),

If you look at this method that generates filter, you will see that it always adds empty option when it gets an array on input. So, the only way to hide empty option is to generate selectbox manually:
'filter' => CHtml::activeDropDownList(Bank::model(), 'bank_account_id',
Chtml::listData(Bankaccount::model()->findAll(array('order' => 'name DESC')), "id", "BankAccountName"),
),
),
Using CHtml::activeDropDownList will give you Bank[bank_account_id] in the selectbox name.

for the filter part,
'filter' => Chtml::dropdownList('Bank[bank_account_id]' , 'selectedDefault',
Chtml::listData(Bankaccount::model()->findAll(array('order' => 'name DESC')), "id", "BankAccountName"),
),
),

Related

how to keep selected filter option dropdown selected after CGridView update in Yii 1.1.14?

One of the filter options in my CGridView in Yii 1.1.14, has this
array(
'header' => 'Status',
'name' => 'status',
'filter' => CHtml::dropDownList('MyModel[status]','status', array(
'' => '',
'0' => 'Approved',
'1' => 'Pending',
'2' => 'Rejected'
)),
'type' => 'raw',
'value' => 'MyHelper::model()->getStatus($data->status)',
'htmlOptions' => array('width' => '8%')
),
My problem is, whenever I select one from the dropdown filter, the CGridView updates the result which is right, but then the selected option from the dropdown disappears, I mean it doesn't remain selected. How to keep it selected?
You have to pass selected value to dropDownList. like below
CHtml::dropDownList('MyModel[status]', MyModel->status, array(
'' => '',
'0' => 'Approved',
'1' => 'Pending',
'2' => 'Rejected'
)),
I have given default Approved status. i.e second parameter of dropDownList function.
Whenever your gridview reloads, firstly the call goes to you controller's action there you must have declared you model's variable/object, so in your controller's action you can set this variable to your model like this :
$myModel->status = $_GET['status'];
and when call returns to your view, there you can check for the value, that is set to that 'status' variable.

Changing CForm dropdown name attribute in Yii

I'm using Yii and I'm having a little problem with some dropdowns.
Basically I'm using CForm to display some dropdown menus of courses. A student can select up to two courses and for each course choice the student can select a 1st choice and second. It is a requirement that each course choice is inserted separately into the database. For example, it a student wants to study 2 courses and wants to have a 1st and 2nd priority course, they would choose like this:
Course one - 1st Priority
Course one - 2nd Priority
Course two - 1st Priority
Course two - 2nd Priority
This would put 4 new rows into the database. The administrators of the courses want this displayed as 4 dropdown menus containing the courses.
At the moment, I'm testing with just the 1st and 2nd priorities of course one, but the problem is that course one - priority one is always empty unless a value is selected for priority two. Then priority one gets the same value as priority two, even though two different courses are selected. I've been following this tutorial Form Builder as I am using the Wizard Behavior which uses CForm to build the forms.
Here is my code so far, again only dealing with "course one":
This is a snippet of the relevant code from the controller:
// inside controller
$model = new CourseChoice();
$form = new CForm('application.views.wizard.ccForm', $model);
$form['courseOneP1']->model = new CourseChoice();
$form['courseOneP2']->model = new CourseChoice();
$c1p1 = $form['courseOneP1']->model;
$c1p2 = $form['courseOneP2']->model;
// Here I am just reading the attributes and exiting for testing
if ($form->submitted()&& $form->validate()) {
echo '<pre>';
print_r($c1p1->attributes);
print_r($c1p2->attributes);
echo '</pre>';
exit;
..........
And here is code in the form in ccForm
return array(
'showErrorSummary' => true,
'title' => 'Course Choice 1',
'elements' => array(
// Course 1 - 1st Priority
'courseOneP1' => array(
'type' => 'form',
'elements' => array(
'course' => array(
'label' => '1st Priority',
'type' => 'dropdownlist',
'id' => 'c1p1',
'prompt' => 'Select 1st Priority Course',
'items' => CHtml::listData(CoursePeriod::model()->with('course')->findAll("year = 2014"), 'id', 'course.course_name'),
)
),
),
// Course 1 - 2nd Priority
'courseOneP2' => array(
'type' => 'form',
'elements' => array(
'course' => array(
'label' => '2nd Priority',
'type' => 'dropdownlist',
'id' => 'c1p2',
'prompt' => 'Select 2nd Priority Course',
'items' => CHtml::listData(CoursePeriod::model()->with('course')->findAll("year = 2014"), 'id', 'course.course_name'),
)
),
),
),
'buttons' => array(
'previous' => array(
'type' => 'submit',
'label' => 'Previous'
),
'submit' => array(
'type' => 'submit',
'label' => 'Next'
)
)
);
So lets say I choose 2 courses, one with an id of 15 and the other with an id of 86, I get the following when I print_r() both dropdowns:
Array // Dropdown 1
(
[course] => 86
.... // other irrelevant attributes
)
Array // Dropdown 2
(
[course] => 86
.... // other irrelevant attributes
)
Update
I've been looking further into this and when I look at firebug, I see that both dropdowns have the same name:
<div class="row field_course">
<label for="c1p1">1st Priority</label>
<select id="c1p1" name="CourseChoice[course]">
</div>
<div class="row field_course">
<label for="c1p2">2nd Priority</label>
<select id="c1p2" name="CourseChoice[course]">
</div>
So the 2nd menu is overwriting the first. But how can I change this? If I change 'course'=>array(.... in the CForm for either subform, the applicable dropdown does not render. I have already tried adding 'name'=>'course1' in the form but it makes no difference.
Couldn't you just set the name of the 2nd priority input element?
'course' => array(
'label' => '2nd Priority',
'name' => 'course2',
'type' => 'dropdownlist',
'id' => 'c1p2',
'prompt' => 'Select 2nd Priority Course',
'items' => CHtml::listData(CoursePeriod::model()->with('course')->findAll("year = 2014"), 'id', 'course.course_name'),
)
Just to answer my own question and close it as it is pretty old now, CForm does not support tabular input and would need to extended to achieve this. Probably not a big job but in the end I convinced management that the four dropdowns design was horrible. :-) I went with a more flexible design showing a gridview of courses in a pop-up to choose courses instead which works well and is less confusing for the user.
Anyone interested in this issue can see the open issue here. There is a link in there to view a possible implementation of extending CForm, though this was posted at the end of 2009.

how to handle an array as a field in CGridView

I'm using CGridView but one of the field is an array ['xx' ,'yy' , 'zz',...]
How can I display that i searched for an answer but did not find
is it possible to use something like DropDownlist to display the values in the array
I have a static array special_offer in a model;
It will filter by the value; This is a drop down list example;
I replaced a field within the cgridview with a array as follows:
array(
'name' => 'special_offer',
'value' => 'Package::$special_offer[$data->special_offer]',
'filter' => Package::$special_offer,
),
In order to use dropDownList in a CGridView column, your array needs to be associative. I recommend that you create a method in your model which converts it into an associative array, something like:
public function getAssociativeArray()
{
$array = array('xx', 'yy', 'zz'); // or use an attribute value
return array_combine(array_values($array), $array);
}
Then in your CGridView, add this as a column, replacing the model/field names with your own:
array(
'name' => 'yourFieldName',
'type' => 'raw',
'value' => 'CHtml::activeDropDownList($data, "yourFieldName", YourModel::model()->associativeArray)',
),

Restricting a category for a certain country in Prestashop 1.5

I need to restrict a category to a set of countries in Prestashop 1.5.
This restriction would prevent the shipping of a product belonging to such a category; as such, the users would still be able to see the products but they would not be able to buy them.
Ideally, I wanted to develop a module that would insert a list of countries (checkbox style, as in the Modules -> Payment page (AdminPayment)) inside a category's edit page, but I haven't been able to do so.
Why can't i simply paste the following code inside the renderForm() function?
Only the description is visible if i do so...
array(
'items' =>Country::getCountries(Context::getContext()->language->id),
'title' => $this->l('Country restrictions'),
'desc' => $this->l('Please mark the checkbox(es) for the country or countries for which you want to block the shipping.'),
'name_id' => 'country',
'identifier' => 'id_country',
'icon' => 'world',
),
EDIT:
I managed to get the list of countries working:
array(
'type' => 'checkbox',
'label' => $this->l('Restricted Countries').':',
'class' => 'sel_country',
'name' => 'restricted_countries',
'values' => array(
'query' => Country::getCountries(Context::getContext()->language->id),
'id' => 'id_country',
'name' => 'name'
),
'desc' => $this->l('Mark all the countries you want to block the selling to. The restrictions will always be applied to every subcategory as well')
),
Now, I can save these values by checking if the value "submitAddcategory" is being submitted in the postProcess function and by running an insert query there. Similarly, I can also load the IDs of the blocked countries from the database, but how can I tick the respective select boxes in the list of countries?
My initial "quick and dirty" idea was to use jQuery selectors inside a document.ready(), but the code gets inserted before everything else and, as such, it won't work because jQuery isn't even loaded yet.
How can this be done?
Cheers
I solved it by using the following code right before the end of the renderForm() function.
The Pièce de résistance was $this->fields_value, as sadly I didn't known of its existence.
public function getRestrictedCountries($obj)
{
// Loading blacklisted countries
$country = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT DISTINCT id_country
FROM `'._DB_PREFIX_.'category_country_restriction`
WHERE id_category = ' . (int)Tools::getValue('id_category') . ';');
$blacklisted_countries = array();
if (is_array($country))
foreach ($country as $cnt)
$blacklisted_countries[] = $cnt['id_country'];
// Global country list
$c_todos = Country::getCountries(Context::getContext()->language->id);
// Crossmatching everything
foreach ($c_todos as $c)
$this->fields_value['restricted_countries_'.$c['id_country']] = Tools::getValue('restricted_countries_'.$c['id_country'], (in_array($c['id_country'], $blacklisted_countries)));
}
PS: The table I am reading from is basically an associative table between 'category' and 'country'

yii tokeninput extension cursor focus on wrong place after same match?

I am having problem with yii tokeninput extension. When i search name it gives the user list and if i select any name and if that name is also selected previous than the cursor point after the selected item, it does not point at the end of all the the item in the input box.
I am using this configuration.
$this->widget('ext.tokeninput.TokenInput', array(
'model' => $model,
'attribute' => 'USER_ID',
'url'=>$this->createUrl('user/searchUserNames'),
'options' => array(
'allowCreation' => false,
'preventDuplicates' => true,
// 'resultsFormatter' => 'js:function(item){ return “<li><p>” + item.name + “</p></li>” }',
'theme' => 'facebook',
//'hintText' => 'Type',
'prePopulate' => $prePopulate,
'processPrePopulate' => $processPrePopulate,
)
));
I have also lookout at the examples but does not find the solution. can any one help me ?
Loopj: jquery token input demo
plz comment the line number 509 in **jquery.tokeninput.js**
input_token.insertAfter(found_existing_token);
that line insert cursor after that selected item so if you comment
this line cursor is at the end of all names