Angularjs version 1.5 select statement not showing selected value on refresh - html

I'm wondering why a string when used as the value in a <\select> tag (not ng-select) will load the saved drop down value on a page refresh but an integer will not display a value. Assuming that formData.maritalStatus equals "Married" or "1" depending on the example value below.
This works when formData.maritalStatus = "Married"
<div class="form-group form-group-sm">
<label class="col-sm-2 control-label" for="maritalStatus">Marital Status</label>
<div class="col-sm-4">
<select class="form-control" name="maritalStatus"
ng-model="formData.maritalStatus">
<option value=""></option>
<option value="Married">Married</option>
<option value="Single">Single</option>
<option value="Divorced">Divorced</option>
<option value="Widowed">Widowed</option>
</select>
</div>
</div>
​
This does not work when formData.maritalStatus = "1"
​
<div class="form-group form-group-sm">
<label class="col-sm-2 control-label" for="maritalStatus">Marital Status</label>
<div class="col-sm-4">
<select class="form-control" name="maritalStatus"
ng-model="formData.maritalStatus">
<option value=""></option>
<option value="1">Married</option>
<option value="2">Single</option>
<option value="3">Divorced</option>
<option value="4">Widowed</option>
</select>
</div>
</div>
This is purely educational as the string works for my needs.

Unfortunately this does not natively work with older versions of AngularJS. AngularJS 1.6.x and above will work work using the ng-value directive (this will give you what you are looking for).
<option ng-value="1">Married</option>
However, there is a solution that can be found in the AngularJS Docs that essentially uses a parser and formatter. The formatter converts the int-to-string when the model changes and the parser does the opposite (string-to-int).
https://code.angularjs.org/1.4.6/docs/api/ng/directive/select#binding-select-to-a-non-string-value-via-ngmodel-parsing-formatting
Take a look at the link above to view the solution for apps using anything before 1.6.x (taken directly from the AngularJS Documentation). If you haven't taken a look already, I think you could also benefit from using ng-options. It is used to for dynamic lists instead of hardcoded options.
Take a look if you'd like!
https://code.angularjs.org/1.5.11/docs/api/ng/directive/ngOptions

Related

Dropdown <option> isn't passing to Firestore Database Angular

I have a form and all data is passing to my Firestore Database as expected with the exception of my dropdown selection and I'm not sure why this is. Any ideas?
<div class="col-lg-6 col-md-12">
<div class="form-group">
<label>Name Of Company:</label>
<select type="text" formControlName="companyName" id="companyName">
<option > Company Name </option>
<option value="Company Name 1">Company Name 1</option>
<option value="Company Name 2">Company Name 2</option>
<option value="Company Name 3">Company Name 3</option>
<option value="Company Name 4">Company Name 4</option>
</select>
</div>
</div>
Found the Issue, Kind of.
So it turns out that the reason this form is not sending the dropdown information to the database is due to some inline css, more specifically a component set to **display: none;**. When I disable this in the Elements -> styles within the console it sends the data as expected.
However, I still haven't found where this element is located in the code or a way to disable it permanently, but it's a start.
Issue Resolved
The display:none issue I mentioned earlier had to do with bootstrap 4 properties when utilizing the nice-select plugin. I simply uninstalled the nice-select plugin and used the native select option with a little css and everything worked perfectly. I tried to find a workaround, but after several hours I decided it was faster to move forward without the plugin.
This part is working if this is not working for you then you need to look at migration table id/name in model or function.
<form action="">
<div class="col-lg-6 col-md-12">
<label>Name Of Company:</label>
<select formControlName="companyName" id="companyName">
<option > Company Name </option>
<option value="Company Name 1">Company Name 1</option>
<option value="Company Name 2">Company Name 2</option>
<option value="Company Name 3">Company Name 3</option>
<option value="Company Name 4">Company Name 4</option>
</select>
</div>
<input type="submit" value="Submit">
</form>

Ng-if on select element

I am trying to show content depending on the option selected from the user. I tried this way
<select class="form-control" id="Intervencion" >
<option selected disabled></option>
<option (click)="show">Yes</option>
<option>No</option></select>
<div *ngIf="show"><p>Text to show</p></div>
I do not understand the problem. There is a special directive for this?
... You don't seem to know how basic HTML works. Sorry to say that, but you have to be aware of what you're doing is completely barbaric.
Here is the solution for you :
<select [(ngModel)]="intervencion">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<div *ngIf="intervencion === 'yes'">Text to show</div>
That being given, you should really follow Angular's tutorials, because that's one of the first examples they give to explain how ngModel works.

customize multiple select won't work in chrome

I copied a code where it works good in mozilla but, when i go to chrome it doesn't work correctly. I have this in mozilla, where it works like it is an input type where when you type the start letter, the select box will show something like it suggest.
Here is the code :
<form class="form-horizontal">
<div class="form-group">
<label class="control-label col-sm-2" id="labelcheck-up" for="fname">Diagnosis:</label>
<div class="col-sm-2">
<select name="tdiagnosis[]" id="tdiagnosis" class="form-control select2" multiple="multiple" data-placeholder="Please input diagnosis here">
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
<option value="Goat">Goat</option>
</select>
</div>
</div>
</form>
Here is jsfiddle link:
https://jsfiddle.net/DTcHh/40338/
Any suggestions how to make this work in google chrome? Thanks.

Adding input capabilities to HTML select option dropdown

Right now the search_type which JS uses later is always "people" as can be seen in the hidden input field.
How can I make it so that the selected option's value is the value tied to the name "search_type"?
<input type="hidden" name="search_type" value="people"> <!-- This obviously needs to change-->
<div class="medium-4 columns">
<select>
<option value="default">All Categories</option>
<option value="people">People</option>
<option value="items">Items</option>
</select>
</div>
I have tried changing the name of all the options' names to search_type but this did not work. I have also tried other things, but can't figure it out. Any help would be much appreciated.
Here is the Javascript line that calls it:
search_type: $('input[name="search_type"]').val(),
Note: I am working in Zurb Foundation
Simply put, there is no need to add another input field when you already have a perfectly usable one! change your code so that the <select> includes the name attribute, like so, and get rid of the hidden input:
<div class="medium-4 columns">
<select name="search_type">
<option value="default">All Categories</option>
<option value="people">People</option>
<option value="items">Items</option>
</select>
</div>

Creating a SelectBox in intel XDK

I have just started coding in Intel XDK. Instead of showing select items in usual way (drop down menu), i want them to show in popup and select one of item from there. I have gone through several sites, but i could not find a single example which will give me the desired result.
<div data-role="fieldcontain">
<label for="select-choice-1" class="select">Choose shipping method:</label>
<select name="select-choice-1" id="select-choice-1" data-native-menu="false" >
<option value="standard">Standard: 7 day</option>
<option value="rush">Rush: 3 days</option>
<option value="express">Express: next day</option>
<option value="overnight">Overnight</option>
</select>
</div>
It seems easy to implement in Jquery. But my problem is , i can only use APIs exposed by Intel XDK. I see there exists javascript file in plugins folder named af.selectBox.js. But i was unable to trace out how to use that. I tried Intel XDK documentation also. But no luck.
If you were inclined to broaden your options, I came across this one, that did what you want. BUT it uses jQuery and Bootstrap:
<div class="form-group">
<label class="control-label col-sm-offset-2 col-sm-2" for="company">Company</label>
<div class="col-sm-6 col-md-4">
<select id="company" class="form-control">
<option>small</option>
<option>medium</option>
<option>large</option>
</select>
</div>
</div>
2nd Answer