zend framework 3 ajax adjust select options - zend-form

I just started working with Zend Framework 3. I'm struggling with a more advanced issue.
I have selectlist one with options 1 and 2
And I have selectlist two with options 1, 2, 3 and 4.
When option 1 is selected in selectlist one only option 1 and 2 have to be visible in selectlist two. When option 2 is selected, then option 3 and 4.
How to do that in a Zend Form?

You can use "disable_inarray_validator" option so form doesnt check if value in value_options array. Also you can check post value and modify form. It depends on actual problem.

I solved it with JQuery:
//array categorieen en rubrieken
var rubrieken = [
//Nog geen keuze gemaakt
'<option value="0">Selecteer een optie</option>',
//Categorie 1
'<option value="10">Rubriek 10</option><option value="20">Rubriek 20</option>',
//Categorie 2
'<option value="30">Rubriek 30</option><option value="40">Rubriek 40</option>'
];
$( document ).ready(function() {
//Alleen stap 1 laten zien (als die niet al gevuld is)
$( "#stap2" ).hide();
//Bij keuze categorie de rubrieken vullen
$("#select1").change(function(){
var selectedValue = $(this).val();
if(selectedValue){
var getOptions = rubrieken[selectedValue];
$('select#select2').html(getOptions);
$('#select2').hide().fadeIn('slow');
}
});
});

Related

Gravityforms - Add category mid-radio-button field

I am populating Gravityforms fields using the functions.php from my template and it works like a charm, but I have one field that is ... a challenge. I have been able to populate the choices from my database just fine, but with the functions.php I cannot control the content of the display area of the field so that I can, for example, add a title or header for each category. Is there a way to programattically adjust the display here's an example of what im hoping to accomplish
RadioButton Choice (Field ID 21)
Dark Colors (category title)
maroon (choice 1)
navy blue (choice 2)
black (Choice 3)
Standard Colors (Category Title)
Red (choice 4)
blue (choice 5)
gray (Choice 6)
Light Colors )Category Title)
pink (choice 7)
sky blue (choice 8)
white (Choice 9)
I am just looking for a way to add the category title between the choices. My DB Query has the categories as part of the response, but the only option I have to populate choices to to feed an array.
I have seen where I can add additional Gravityform fields and have them controlled by the same "single select" radio button option, but the categories involved change based on the DB query I call to dynamically populate the choices and could range from 1 category to 10, which will not have correlating fields in the form itself, as this is all under a single radio-button field.
Any thoughts would be appreciated.
I was able to use the link I posted in my comment to provide a solution. As part of my "choices"
here is the function for the Pre-Render to populate choices. In this case I am populating a radio-button field with product images instead of default radio buttons
add_filter('gform_pre_render_1', 'populate_choices');
function populate_choices($form) {
global $wpdb;
// Now to get a list of the products I want to include on this radio-button
$dataQ = "
SELECT category, product_id, product_name
FROM product_table
WHERE product_type = 'whatever I am looking for'
ORDER BY category, product_name ASC
";
$dataR = $wpdb->get_results($dataQ);
$dataList = array();
// get current protocol to use in Product Images
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
// Rebuild URL for use with product image graphics
$productImageUrl = $protocol.$_SERVER['HTTP_HOST'].'/wp-content/product_images/';
// Generate list of choices for the radio-button, along with category headers
foreach ($form['fields'] as $field) {
if ($field->id == '32') { // 32 My specific radio-button field
$category = "";
foreach ($dataR as $data) {
if ($category != $data->category) {
// If I hit a new category, add the "header" item to the "choices" list with a unique search item to identify it, in this case "###"
$category = $data->category;
$dataList[] = array(
'text' => '#'.$category."#",
'value' => ''
);
}
$productImage = str_replace(" ","",strtolower($data->product_name)).".jpg";
$productID = $data->product_id;
$dataList[] = array(
'text' => $data->product_name,
'value' => $productID,
'isSelected' => false,
'price' => '0.00',
'imageChoices_image' => $productImagehUrl.$productImage
);
}
$field->choices = $dataList;
}
}
}
I then added a specific field modifier to update the "#category#" choice elements with html to make the category names show up.
// numbers after filter = "1" for form ID 1, "32" for field ID 32
add_filter('gform_field_choice_markup_pre_render_1_32', function( $choice_markup, $choice) {
if ( strpos( $choice['text'], '#' ) === 0 ) {
$categoryName = str_replace("#","",$choice['text']);
$choice_markup = '
<li style="width:100%;text-align:left;">
<span style="color:#000000;font-weight:bold;font-size:18px;">
<br>
'.$categoryName.'
</span>
</li>
';
}
return $choice_markup;
}, 10, 2);

Checkboxlist in YII AJAX filter deselecting items

I have an ajaxfilter on a YII listview. When I select an item in the checkboxlist everything works properly: ajax calls vacature/index and gives 'vacature' the right ID which I handle in my controller, so this works fine. (vacature/index?vacature=13)
Now when I uncheck the same item it does exactly the same call, so it passes the id of the changed item. I only need it to not pass the id of the de-selected item and the string to be empty.
Thanks in advance!
echo CHtml::beginForm(CHtml::normalizeUrl(array('vacature/index')), 'get', array('id'=>'filter-vacature'))
echo CHtml::checkBoxList('niveau', (isset($_GET['niveau'])) ? $_GET['niveau'] : '',
CHtml::listData(Lov::model()->findAllByAttributes(array('wat'=>'opleiding')), 'id', 'item'),
array('class'=>'niveau','template'=>'<span class="checkbox-columns">{input} {label}</span>', 'separator'=>''))
$('.niveau').change(function(){
niveau = $('.niveau').serialize();
$.fn.yiiListView.update(
'ajaxListView',
{data: niveau}
);
});
I added a comparison to check if the string is empty, and pass the working empty value:
$('.niveau').change(function(){
niveau = $('.niveau').serialize();
if(empty(niveau))
{
niveau = 'niveau%5B%5D=0';
}
$.fn.yiiListView.update(
'ajaxListView',
{data: niveau}
);
});

Using a list with MVC radiobuttonfor

I am using radiobuttonfor in MVC 4 and I am trying to give the user a selection of items that he can select. My problem is when one of the items is selected and it's posted to the controller, the string value is a .net system.guid rather than the actual guid value.
#foreach (var person in Model.InterviewDates[i].Interviewers) // all interviewers - not filtered
{
var list = Model.InterviewDates[i].Interviewers.Select(p => p.InterviewerID).ToList();
#Html.Raw(person.InterviewerName + " ")
// TODO: Lambda for getting all Chairs based on interview type and interview date
#Html.RadioButtonFor(m => m.InterviewSchedules[i].Chair, list, new { #style = "width:auto;background:none;border:none" })
<br />
}
I have a list of people and it contains their user ids. I then want the user to select a radio button to bind that value to the model.
- list Count = 3 System.Collections.Generic.List<System.Guid>
+ [0] {5fb7097c-335c-4d07-b4fd-000004e2d221} System.Guid
+ [1] {5fb7097c-335c-4d07-b4fd-000004e2d222} System.Guid
+ [2] {5fb7097c-335c-4d07-b4fd-000004e2d223} System.Guid
+ Raw View
When I post it goes here:
[HttpPost]
public ActionResult SubmittedInterviews(InterviewManagement InterviewManagement)
{
return View();
}
This is what I can see in quick watch
- InterviewManagement {IMecheAdmin.Models.InterviewManagement} IMecheAdmin.Models.InterviewManagement
+ InterviewDates Count = 0 System.Collections.Generic.List<IMecheAdmin.Models.InterviewDate>
- InterviewSchedules Count = 1 System.Collections.Generic.List<IMecheAdmin.Models.InterviewSchedule>
- [0] {IMecheAdmin.Models.InterviewSchedule} IMecheAdmin.Models.InterviewSchedule
Chair "System.Collections.Generic.List`1[System.Guid]" string
Cofacilitator null string
Facilitator null string
+ InterviewDate {01/01/0001 00:00:00} System.DateTime
Location null string
Observer null string
Preference false bool
Site null string
+ Raw View
+ InterviewTypes Count = 0 System.Collections.Generic.List<IMecheAdmin.Models.InterviewType>
SelectedMembershipType null string
And this is not what I want:
Chair "System.Collections.Generic.List`1[System.Guid]" string
Doesn't say actual GUID. Any ideas?
Radio button is normally used where we want user to only select only one option from the provided item.
So, you need to pass single object instead of list:
foreach(var item in list)
{
#Html.RadioButtonFor(m => m.InterviewSchedules[i].Chair,
item.InterviewerID,
new { #style = "width:auto;background:none;border:none" })
}

How to put contrasting information into a CGridView column based on a condition?

I'm looking into showing/hiding specific column data on a CGridView widget for the Wii Framework.
I have a CButtonColumn which contains 3 buttons. However, on certain conditions, I want to display something different for a particular row.
I have 3 different conditions which determin what gets displayed for particular row.
The following illustrates what I want to do:
| 1 | Title A | [hide][view][update] <-- if (condition == 'a')
| 2 | Title B | [hide][view][update] <-- if (condition == 'a')
| 3 | Title C | display text or link or button <-- if (condition == 'b')
| 4 | Title D | display alternative buttons <-- if (condition == 'c')
What is my best approach to take here?
I can't use 'visible'=> $model->processingStatus != "processed" on the column because this will remove the whole column. I need to target each row insatead.
Should I use the 'visible' parameter on each individual button? I have tried this using the commented out code below but it breaks the page.
FYI: I have successfully tried the 'visible' parameter on the CButtonColumn itself, but its not what I need. Plus not sure which row's status it is reading.
Or should I add a function to the controller? Have it do the if/else statements and return back what is to be displayed. How would this work?
Here is my code:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'my-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name'=>'myid',
'header'=>'ID',
),
'Title',
array(
'class'=>'CButtonColumn',
'visible'=> $model->status != "done",
'template'=>'{hide}{view}{update}',
'buttons'=>array(
'hide'=>array(
'label'=>'Hide', //Text label of the button.
'imageUrl'=>Yii::app()->request->baseUrl . '/img/icons/bulb-off.png' //Image URL of the button.
//'click'=>'function(){alert("Toggle Hide!");}', //A JS function to be invoked when the button is clicked.
//'options'=>array(), //HTML options for the button tag.
//'url'=>'javascript:void(0)', //A PHP expression for generating the URL of the button.
//'visible'=> $model->status == "done", //A PHP expression for determining whether the button is visible.
),
'view'=>array(
//Text label of the button.
'label'=>'View',
//Image URL of the button.
'imageUrl'=>Yii::app()->request->baseUrl . '/img/icons/view-record.png'
),
'update'=>array(
'label'=>'Update/Edit',
'imageUrl'=>Yii::app()->request->baseUrl . '/img/icons/edit-pencil.png',
'url'=>'Yii::app()->createUrl("metadataandchapters/create?bookid=" . $data->bookid)',
)
)
)
)
)); ?>
Hope I am making good enough sense here!
You should use visible button option, but it should be a PHP expression string, e.g. :
'visible'=> '$data->status == "done"',
http://www.yiiframework.com/doc/api/1.1/CButtonColumn#buttons-detail
Extend CButtonColumn with your own class, then you should be able to change this function to whatever you need to render or hide buttons or do any changes you want.
/**
* Renders a link button.
* #param string $id the ID of the button
* #param array $button the button configuration which may contain 'label', 'url', 'data-icon', 'imageUrl' and 'options' elements.
* #param integer $row the row number (zero-based)
* #param mixed $data the data object associated with the row
*/
protected function renderButton($id, $button, $row, $data)
More details about the function http://www.yiiframework.com/doc/api/1.1/CButtonColumn#renderButton-detail

Select a default value in dropdownlistfor MVC 4

I'm trying to make a dropdownlistfor with a selected value but it doesn't work :/ And I search on the web but I don't find the solution :/
For the moment, I'm doing this :
In C# :
ViewBag.ID_VEH = new SelectList(db.VEHI, "ID_VEH", "COD_VEH", 4); // 4 is an example
In my cshtml :
#Html.DropDownListFor(model => model.ID_VEH, ViewBag.ID_VEH as SelectList)
The dropdownlist is well complete but the default value is not selected :/ do you have an idea please ?
What I like to do is add a list of the items to display in the dropdownlist to my model, so I don't have to pass that list via a viewbag.
Also i like to add a field to my model that contains the SelectedValue, that I fill in the controller
Then you can do
#Html.DropDownListFor(model => model.ID_VEH, new SelectList(Model.listVEH, "ID_VEH", "COD_VEH", Model.SelectedVEH_ID), "--Select VEH--")
just set the initial value of model.ID_VEH to 4:
In the controller:
model.ID_VEH = 4;
Just in case someone has similar problems finding the answer:
I want to have view with the dropdown boxes have focus on the items i give (hardcoded) in the controller:
Controller:
SGLDataRegistration.Models.DataRegistrationModel mdl = rwd.GetData(DateTime.Now.Year, currentWeek, DateTime.Now, 139, 1);
View:
<div id="tempCustomerselect">
#Html.LabelFor(m => m.CustomerName)
#Html.DropDownListFor(m => m.PitchID, new SelectList((new SGLDataRegistration.Models.CustomerModel().GetRoles()).OrderBy(x => x.CustomerName), "PitchID", "CustomerName"), new {id = "ddlCustomer", #class="jsddlCustomer"})
</div>
In this GetData, i setthe desired values hardcoded:
public SGLDataRegistration.Models.DataRegistrationModel GetData(int year, int weekNumber, DateTime datum, int pitchID, int parameter)
{
try
{
DataRegistrationParameters drp = GetParameter(parameter);
//vul een instantie van het dataregistrationmodel
SGLDataRegistration.Models.DataRegistrationModel drm = new Models.DataRegistrationModel();
drm.WeekNumber = weekNumber;
drm.BeginDay = datum;
drm.Parameter = parameter;
drm.Year = year;
drm.PitchID = pitchID;