Form: How to properly connect Live Preview with controller? (AngularJS) - forms

Like everyone else who has asked this question about Angular JS, my problem goes deeper than the simple "how to fix blank option in drop down menu". Basically, I am creating a live preview WITH the form that I am creating. Here is what I mean:
The initial blockquote in the HTML is basically the actual reviews that will be there. The second blockquote is the actual LIVE preview. Finally, the third part is the piece of dilemma I am having.
DILEMMA:
Here is the dilemma I am having:
By removing: ng-model="review.stars" from the select tag, my page will load the 5 stars as expected. However, since I need to bind the ratings already posted up, the live preview AND the initial selection of 5 stars, I have to use ng-model="review.stars" to bind everything together.
BUT, now what happens is that by adding ng-controller=starsController along with the ng-model, the whole thing simply doesn't work. I have tried using a few formulas (and one of those was looking promising, one using an orderProp), but because I NEED to bind the three aforementioned things, it breaks the code and that specific piece.
I can still select an option from the list, but the preview fails to show it. Furthermore, it will NOT automatically show up the 5th star in the drop down (I have to manually select it).
In case you would like a visual aid to understand better what I'm explaining, let me post a picture of the two situations I am referring to. This is the image via a link - I don't have enough rep to post it directly here :/
HTML
<blockquote ng-repeat="review in product.reviews">
<b>Stars: {{review.stars}}</b>
{{review.body}}
<cite>by: {{review.author}}</cite>
</blockquote>
<form name="reviewForm">
<blockquote>
<b> Stars: {{review.stars}}</b>
<br/>
<b> Review: {{review.body}}</b>
<br/>
<cite>by: {{review.author}}</cite>
</blockquote>
<select ng-model="review.stars" ng-controller="starsController" name="stars" id="stars">
<option style="display:none" value=""></option>
<optgroup label="Rate the product">
<option value="1 star" name="1 star">1 star</option>
<option value="2 stars" name="2 stars">2 stars</option>
<option value="3 stars" name="3 stars">3 stars</option>
<option value="4 stars" name="4 stars">4 stars</option>
<option value="5 stars" name="5 stars" selected="selected">5 stars
</option>
</optgroup>
</select>
<br/>
JS
app.controller('starsController', ['$scope', function($scope) {
$scope.options = [
{ name: '1 star', value: '1 star' },
{ name: '2 stars', value: '2 stars' },
{ name: '3 stars', value: '3 stars' },
{ name: '4 stars', value: '4 stars' },
{ name: '5 stars', value: '5 stars' }
];
$scope.orderProp = options[4];
}]);

Updated Plunker
To fix this, add ng-init="review={}" to your form tag.
<form name="reviewForm" ng-init="review={}">
Or, even better - move ng-controller higher up the DOM tree (maybe on your form element)
<form name="reviewForm" ng-controller="starsController">
Or the best option, create a custom reviewStars directive:
app.directive('reviewStars', function() {...});

Related

Knockout js visible binding not working for secondary variable, depending on which drop down is selected should show message. First instance works

I have a form which has 2 dropdowns questions.
Depending what the user answers, depends what will happen.
So for example
Are you human? The person answers yes and then another question show asking if they are employed, if they say yes to this then a sign up form will show.
If they say no to either question then some sorry cant sign you up text would show, with a form reset option ideally.
The first question seems to work fine, The issue is, it shows all messages for the second question which should be hidden until the value is selected and only one message should show.
Are you human?<br><select data-bind='value:thisSelect'>
<option value='none'>Select answer</option>
<option value='yes'>Yes</option>
<option value='no'>No</option>
</select>
<p data-bind="visible:thisSelect() === 'yes'">
Are you employed?<br>
<select data-bind='value:currentSelect'>
<option value='blank'>none</option>
<option value='form'>show form</option>
<option value='sorry'>Something else</option>
</select></p>
<br><br>
<p data-bind="visible:currentSelect() === 'blank'"> </p>
<p data-bind="visible:currentSelect() === 'form'">Hello, now display the sign up form</p>
<p data-bind="visible:currentSelect() === 'sorry'">Goodbye</p>
And my Knockout JS
var testing = {
thisSelect: ko.observable()
};
ko.applyBindings(testing);
var test = {
currentSelect: ko.observable()
};
ko.applyBindings(test);
My Js fiddle is here https://jsfiddle.net/Chazlie/sdpayfo7/12/
Another version I tried is here http://jsfiddle.net/Chazlie/2exnjm4t/24/ but this just replaces the message from the first question so is not what I was hoping it would do.
Thank you
For anyone else who is struggling with the same issue, I have solved this with the following, I believe the issue was that I was trying to call the bindings twice, instead of creating one variable and controlling it all there.
Are you human?<br><select data-bind="value: selectedChoice">
<option value="none">Select answer</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<p data-bind="visible: selectedChoice() === 'yes'">
Are you employed?<br>
<select data-bind="value: currentSelect">
<option value="blank">none</option>
<option value="form">show form</option>
<option value="sorry">Something else</option>
</select></p>
<br><br>
<p data-bind="visible:currentSelect() === 'blank'"> </p>
<p data-bind="visible:currentSelect() === 'form'">Hello, now display the sign up form</p>
<p data-bind="visible:currentSelect() === 'sorry'">Goodbye</p>
<script>
var viewModel = {
selectedChoice: ko.observable("none"),
currentSelect: ko.observable("none")
};
ko.applyBindings(viewModel);
</script>

Can the values in local storage be used to verify age?

I am looking for some help with my verification code. I am looking for a simple method to verify two ages. There is a "from age" and a "to age". I need to make sure that the user selects the ages properly. The "from age" should be younger than the "to_age. I made a script and it kind of works. The problem is it does not work consistently. I need it to check, when you select the "from_age" dropdown as well as the "to_age" dropdown. This one seems to only work with one of the dropdowns and one time through. It does not work when the page loads and it sometimes doesn't work at all. I tried two options but neither worked. I tried to use localstorage values since another script I have loads the keys and values on change very well. Or I need to try to fix this version which uses the values in the options dropdown. I am not sure which would work better, but I am thinking the localstorage values are the better choice.
I have tried to use the values associated with the keys in localstorage but I failed on getting it to work. The method using the values in the select/options works but works poorly. Please excuse me if I made any mistakes condensing the code for this question. Thanks
jQuery
$(function() {
$('.agefrom_selection').change(function () {
var quantity_1 = parseInt($('.agefrom_selection').val());
var quantity_2 = parseInt($('.ageto_selection').val());
if ( quantity_1 > quantity_2 ) {
$('.age_warning').addClass('show_age_warning')
}
else {
$('.age_warning').removeClass('show_age_warning')
}
});
});
CSS
.age_warning {
display: none;
}
.age_warning.show_age_warning {
display: block;
position: relative;
float: right;
font-size: 12px;
color: #ff0000;
}
html
<form id="ageselection" method="post" autocomplete="off">
<fieldset>
<ul class="age_preference_text_box fl">
<li><label class="from_to_age_text">from </label>
<select class="age_text agefrom_selection">
<option value="18" selected>18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
</select>
</li>
<li><label class="from_to_age_text">from </label>
<p class="age_warning"> "to" age must be equal <br>or higher than "from" age</p>
<select class="age_text ageto_selection">
<option value="18" selected>18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
</select>
</li>
</ul>
</fieldset>
</form>
I expected the warning to show on every instance(or disappear). If you change the from_age dropdown, when you change the to_age dropdown, when you modify either dropdown, if you refresh the page, and if you manipulate any dropdown after a page refresh. My code is poorly written and does not work as expected.
I figured it out. The function was only written on one of the dropdown selectors. It needed to be written for both dropdown selectors. The answer will be something like:
$(function() {
$('.agefrom_selection').change(function () {
var quantity_1 = parseInt($('.agefrom_selection').val());
var quantity_2 = parseInt($('.ageto_selection').val());
if ( quantity_1 > quantity_2 ) {
$('.age_warning').addClass('show_age_warning')
}
else {
$('.age_warning').removeClass('show_age_warning')
}
});
$('.ageto_selection').change(function () {
var quantity_1 = parseInt($('.agefrom_selection').val());
var quantity_2 = parseInt($('.ageto_selection').val());
if ( quantity_1 > quantity_2 ) {
$('.age_warning').addClass('show_age_warning')
}
else {
$('.age_warning').removeClass('show_age_warning')
}
});
If anybody else can come up with something better, I am very willing to listen. Thanks

Search from with select option for which domain

I have a mediawiki multi language site (on sub domains) and I am trying to set up a start page similar to wikipedia, where a user can enter a search term, and then select their language from a select list.
I have no idea where to start, not much experience building forms, So with this question I am at least looking for clues and directions to go.
My idea is a simple search form, and depending on which language was selected, when a user submits, it goes to the url (related to the select list), and appends the searched term.
A simple form to get you started (the form allows you to input an URL and select a language)
function myFunction() {
// get input form values
var url = document.getElementById("iUrl").value;
var langEl = document.getElementById("iLang");
var iLang = langEl[langEl.selectedIndex].value;
console.log("URL = ", url);
console.log("Lang = ", iLang);
window.open(url + "&lr=" + iLang);
}
<form id="form" action="javascript:myFunction();">
<input size="35" type="text" id="iUrl" name="text" value="https://www.google.com/search?as_q=" />
<br>
<select name="language" id="iLang">
<option value="lang_en">EN</option>
<option value="lang_es">ES</option>
</select>
<input type="submit" name="submit" value="Submit" />
</form>

Display options on top of choice

I'm using Select2 plugin for select element. I need to show select options over select choice so I set negative margin to select options. Options are showing over select choice but they did not stay opened. Maybe this is happen because of onkeyup event on select result item. But I have no clue how to resolve this problem.
This is example what I want:
HTML:
<select name="">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</select>
JS:
$(document).ready(function() {
$('select').select2({
minimumResultsForSearch: -1
});
});
CSS:
.select2-drop {
margin-top: -26px;
}
Code example: http://jsfiddle.net/quark2014/YC3kt/4/
Thanks
The first step i though is use the select2 event : select2-selecting
The code below
$(document).ready(function() {
$('select').select2({
minimumResultsForSearch: -1
}).on('select2-selecting',function(e){
e.preventDefault();
});
});
JSBIN
But we still have problem Orz..

How to Call Controller Action Through html Select options on change in Zend Framework?

i have the following html in my view
<span style="float:right"> <label> Pattern :</label>
<select>
<option value="3*3">3*3</option>
<option value="6*6">6*6</option>
<option value="12*12">12*12</option>
</select>
</span>
what i want to do is that i want to call my action onchange lets say
if(3*3) call this
public function 3by3Action(){}
if(6*6) call this
public function 6by6Action(){}
if(12*12) call this
public function 12by12Action(){}
In Zend Framework, Actions are methods within a Controller class and are accessible via URL. For example, if your Controller class is called "MathController" and it contains an action called "sixBySixAction", then you would trigger this action by navigating to a URL that looks something like:
http://baseUrl/math/six-by-six
Notice that the name of the action method is camel case within the controller class but it is separated by dashes in the URL. This is a formatting requirement for Zend Framework. Also note that the controller class is named "MathController" but you only have to put "math" in the URL.
So, you could use JavaScript to assign an onChange handler for your select box which simply redirects to a particular URL which handles the change by accessing a particular action method within a particular controller class.
For more information on this, check out this page in the Zend Framework Programmer's Reference Guide.
As for the JavaScript part, here is an example of how to redirect upon a select box being changed. You'll need to modify this, of course, but it'll get you started:
<Script language="JavaScript">
function goto(form) { var index=form.select.selectedIndex
if (form.select.options[index].value != "0") {
location=form.select.options[index].value;}
}
</SCRIPT>
<FORM NAME="form1">
<SELECT NAME="select" ONCHANGE="goto(this.form)">
<OPTION VALUE="">-------Choose a Selection-------</OPTION>
<OPTION VALUE="index.htm">Home</OPTION>
<OPTION VALUE="web_development.htm">Web Development</OPTION>
<OPTION VALUE="html_codes.htm">HTML Tips</OPTION>
<OPTION VALUE="html_codes_chart.htm">HTML Code Chart</OPTION>
<OPTION VALUE="javascript_codes.htm">JavaScript Codes</OPTION>
<OPTION VALUE="216_color_chart.htm">Color Code Chart</OPTION>
</SELECT>
</FORM>
You need to be more specific on why you want to call your actions like that.
You can use AJAX to fetch some data produced by your actions or you can do a simple redirect. It depends on what you want to achieve.
The AJAX solution:
attach an onchange event handler to select
the handler issues an AJAX request to the server, to the correct action. You can easily output JSON data from the controller using $this->_helper->json($data)
you get back information from server and inject to your HTML document
change your html to this
<select id="your_id">
<option value="Threes">3*3</option>
<option value="Six">6*6</option>
<option value="Twele">12*12</option>
</select>
in js write this
$("#your_id").change(function() {
var action = $("#your_id").val();
var href= "Controller/"+action +"/";
var data = your data;
$.ajax({ type: "GET",
url: href,
data: data,
success: function(response){
//do what u wana do
}
});
}):
hope it works