ionic localStorage select - ionic-framework

I want to create setting page in my ionic app
HTML :
<select class="selectCountry">
<option value="fr">France</option>
<option value="usa">USA</option>
</select>
Controler.js
angular.module('starter.controllers', ['ionic', 'ngStorage'])
.controller('SettingsCtrl',function($scope, $localStorage){
// what to do to save and data from <select> & use if condition
});

you can put ng-model on the select and trigger onchange function
<select class="selectCountry" ng-model="selectCountry" ng-change="save()">
<option value="fr">France</option>
<option value="usa">USA</option>
</select>
and in the controller
angular.module('starter.controllers', ['ionic', 'ngStorage'])
.controller('SettingsCtrl',function($scope, $localStorage){
// what to do to save and data from <select> & use if condition
$scope.save = function(){
$localStorage.selectCountry= $scope.selectCountry; // set
alert($localStorage.selectCountry); // get
}
$localStorage.selectCountry = $localStorage.selectCountry; // get
});
Check the docs for it NgStorage
i created this pen Here

.service('storage', function() {
var myjsonObj = "nothing";//the object to hold our data
return {
setJson:function(name, item){
window.localStorage.setItem( name, item );
},
getJson:function(name){
return window.localStorage.getItem( name );
}
}
})
if you wanna store and retrieve the variable.
storage.setJson("nameOfSetting", "theVariableYouWannaStore");
var theVariableYouStored = storage.getJson("nameOfSetting");

Related

How to set focus to another MUI element on Selection?

I have two MUI Select elements. After the user makes a selection on the first, I'd like the second to automatically get activated.
Here's the pseudo-code:
<Select native value={region} onChange={selectRegion()}>
{regions.map((r,i) => <option value={r.value} key={`${r.value}-${r.index}`}>)
</Select>
<Select multiple value={zone}>
{zones.map((z,i) => <option value={z.value} key={`${z.value}-${z.index}`}>)
</Select>
...
const selectRegion(e) => {
setRegion(e.target.value)
let z = getZones(e.target.value)
setZones(z)
// This is where I want to focus on the Zone input
}
Can you add a ref to the select component?
const selectRef = useRef(null)
<Select inputRef={selectRef} multiple value={zone}>
{zones.map((z,i) => <option value={z.value} key={`${z.value}-${z.index}`}>)
</Select>
const selectRegion(e) => {
setRegion(e.target.value)
let z = getZones(e.target.value)
setZones(z)
// This is where I want to focus on the Zone input
selectRef.current.focus()
}

Target the current value of an option list using js, and add that value to an object?

HTML
<div class="options-container">
<select id="all-options">
<option value="0" selected>NY</option>
<option value="1">LA</option>
<option value="2">Toronto</option>
<option value="3">Paris</option>
</select>
</div>
JS
let selectedOption = {};
let allOptions = document.getElementById('all-options');
let optionValue = allOptions.options[all-options.selectedIndex].value;
if (optionValue == "0") {
selectedOption.value === "0"
} else if (optionValue === "1") {
selectedOption.value === "1"
} else if (optionValue === "2") {
selectedOption.value === "2"
} else if (optionValue === "3") {
selectedOption.value === "3"
};
I'm attempting to target the current value of an option list element in my HTML file using JS so that I can add that value to the selectedOption object. The value can be added either as a string or number object.
Defining the Event Listener
You're going to want to define an event listener in your <select>. For example:
function onSelectItem(evt)
{
let value = evt.target.selectedOptions[0].value;
let text = evt.target.selectedOptions[0].text;
// Assign it how you want here:
selectedOption = value;
}
You can't really "push" to an object. If you meant an array, change the selected option to an array ([]) and do selectedOption.push(value). If you want a useful object, however, you could just assign it to evt.target.selectedOptions[0].
Attaching the Listener
After that, you can attach it in html, and pass the event object via the default event variable:
<select id="all-options" onchange="onSelectItem(event)">
<option value="0">NY</option>
<option value="1">LA</option>
<option value="2">Toronto</option>
<option value="3">Paris</option>
</select>
or more Javascript when the page loads (by setting the onload attribute to "init()" in <body>):
function init()
{
let menu = document.getElementById('all-options');
menu.addEventListener('change', onSelectItem);
// Since it seems that you also want a default selected value when the
// page loads, you should probably also initialize your `selectedOption` to `0`.
selectedOption = menu.selectedOptions[0].value;
}

Dynamically EasyDropDown.js select

I use easydropdown.js to style the HTML select.
The problem is that populating dynamically select via $.post jquery, the script does not work.
You know how to help me? Thanks.
We can get select object, modify its value, and then destroy and re-init as code below:
<select id="select-to-easydropdown" class="dropdown">
<option value="option1">option 1</option>
<option value="option2">option 2</option>
</select>
/* in js code with jquery */
var select = $("#select-to-easydropdown");
// process with select object (add/remove option)
select.append('<option value="option3">option 3</option>');
// destroy if existing easydropdown object
select.easyDropDown('destroy');
// init or re-init easydropdown
select.easyDropDown({
onChange: function(selected){
// add change event listener
...
}
});
Hope it helps!
var choose = '<option value="0">choose...</option>';
var wait = '<option value="0">wait...</option>';
$("#select_2").html(choose);
$("#select_3").html(choose);
$("#select_1").change(function(){
var val_select_1 = $("#select_1 option:selected").attr('value');
$("#select_2").html(wait);
$("#select_2").attr("disabled", "disabled");
$("#select_3").html(choose);
$("#select_3").attr("disabled", "disabled");
$.post("select.php", {id_select_1:value_select_1}, function(data){
$("select#select_2").removeAttr("disabled");
$("select#select_2").html(data);
});
});
$("#select_2").change(function(){
$("#select_3").attr("disabled", "disabled");
$("#select_3").html(wait);
var val_select_2 = $("#select_2 option:selected").attr('value');
$.post("select.php", {id_select_2:value_select_2}, function(data){
$("#select_3").removeAttr("disabled");
$("#select_3").html(data);
});
});
and select style this https://github.com/patrickkunka/easydropdown

Knockoutjs (version 2.1.0): bind boolean value to select box

I want to bind boolean value to select element using KO v2.1.0, but obviously it doesn't work as expected.
HTML code:
<select data-bind="value: state">
<option value="true">On</option>
<option value="false">Off</option>
</select>
JavaScript code:
var model = {
state: ko.observable(false)
};
ko.applyBindings(model)
So I expect the select box goes to "Off" position with the initial value false but it was at "On". If I put state: ko.observable("false") it will be correct but that's not I wanted. Anyone know how to bind the boolean value to select box with KO?
Jsfiddle: https://jsfiddle.net/greenlaw110/Ajm58/
Here is an option that we explored for this one from this forum post:
ko.bindingHandlers.booleanValue = {
init: function(element, valueAccessor, allBindingsAccessor) {
var observable = valueAccessor(),
interceptor = ko.computed({
read: function() {
return observable().toString();
},
write: function(newValue) {
observable(newValue === "true");
}
});
ko.applyBindingsToNode(element, { value: interceptor });
}
};
So, we use a custom binding to inject a writeable computed observable between the UI and our view model. This is just an alternative to doing it directly in your view model.
Sample here: https://jsfiddle.net/rniemeyer/H4gpe/
This happens because the select is working with strings, and not booleans at any stage.
You should try a ko.computed( ... ) value instead.
Check here for details: https://jsfiddle.net/Ajm58/3/
<select id="testSelect" data-bind="value: stateString">
<option value="true">true</option>
<option value="false">false</option>
</select>
​
var model = {
state: ko.observable(false)
};
model.stateString = ko.computed({
read: function() { return (this.state() ? "true" : "false"); },
write: function(value) { this.state(value == "true"); }
}, model);
ko.applyBindings(model);
setTimeout(function() {
model.state(true);
}, 1500);
setTimeout(function() {
$("#testSelect").val("false");
}, 3000);
Much easier than the other answers: use the options-Binding.
The answer is in the kind of lengthy expression below:
<select data-bind="options: [{text: 'On', value: true}, {text: 'Off', value: false}], value: lala1, optionsValue: 'value', optionsText: 'text'">
By using the options binding, you're able to assign virtually everything to the value of an option. It's rather uncommon, I guess, to bind an explicit value to the options binding, but in this 2-way case, it is reasonable.
I think I got the answer, put the number "1" and "0" to the option value respectively:
<select data-bind="value: state">
<option value="1">On</option>
<option value="0">Off</option>
</select>
See https://jsfiddle.net/greenlaw110/Ajm58/2/

Javascript to select first option of select list

I need to create a reset button which on click will set all select lists in a form to index 0. I have tried this code for function, but it gives a syntax error on myForm.length;
<script type="text/javascript">function ResetForm(form) {
var myForm = document.forms[form];
for( var i=0; i < myForm.length; i++ ){
myForm.select[i].selectedIndex =0; } }
</script>
There is no such property as someForm.select, try this instead:
selectTags = myForm.getElementsByTagName("select");
for(var i = 0; i < selectTags.length; i++) {
selectTags[i].selectedIndex =0;
}
A quick note to answer from Ron Royston
(I've only tried this on Chrome 74.0)
Setting mySelect.selectedIndex = 0; or $('#mySelect').prop("selectedIndex", 0); won't work if the option if it's disabled.
Hope this saves someone some time and frustration!
You simply need to set the .selectedIndex property, as in mySelect.selectedIndex = 0;. Try out the example below.
var doc = document;
var myCheckbox = doc.getElementById('my-checkbox');
var mySelect = doc.getElementById('my-select');
myCheckbox.addEventListener("click", function(e){
if (this.checked){
mySelect.disabled = false;
} else {
mySelect.selectedIndex = 0;
mySelect.disabled = true;
}
});
<label><input type="checkbox" id="my-checkbox" value=""> Enable Dropdown</label><br>
<select id="my-select" disabled>
<option class="placeholder" selected disabled value="">Select credential</option>
<option value="apples">apples</option>
<option value="oranges">oranges</option>
<option value="bananas">bananas</option>
</select>
This will set every selection directly after changing to the first option:
<select onchange="your_selection_func(); this.selectedIndex=0;">
You may find jquery useful here. This SHOULD do the trick...
$(function()
{
$('.resetButton').click(function()
{
$(this).closest('form').find('select').each(function()
{
$(this)[0].selectedIndex=0;
});
});
});
<input type="reset" class="resetButton" />
I found some hack:
<select onchange="doSomething()">
<option value="a" selected disabled hidden>a</option>
<option value="a">a</option>
<option value="b">b</option>
<option value="…">…</option>
</select>
combination of selected disabled hidden causes that (visualy) first option responds with change event (even with same value!).
Working with IE, Edge, Opera, Chrome, but not working in FF :(
I realize this is kinda old, but wanted to improve on teh1's answer. There's no reason to create a jQuery object from 'this' when you're just going to grab the DOM element back from it:
$(this).closest('form').find('select').each(function() {
this.selectedIndex = 0;
});