JSON user search - javascript

I have the following gist with a JSON database and an XHR object.
The user parameter from getUsers(user) method is an input value.
I want select an user from the db via input search value. But i don't know how to set the http url or the searching algorithm.
I can select an user via: user.Name1 but how can i modify the entries after the dot .Name1 so it can be selected with the input value.
E.g: user. + "input value". I can't figured out.

I hope I inderstood you right, you want to access it via square brackets:
user[userValue] where userValue is your input variable

You can access on object like this user["Name1"] and as such can do this : user[input_value_variable]

Related

Trying to grab the 'save' from a dropdown option from a clone in Bootstrap and save the input to Local Storage

trying to save the input provided from the user in an input form that is cloned, so i want it to work for all the 'save' options, keep that text in local storage, and lastly to maintain that text in the input form even when the user refreshes the page, i've tried a bunch of different ways at my wits end, ha. here is fiddle: https://jsfiddle.net/dalejohn33/24u3vxmp/13/
$save.click((f) => {
var task = $("input:selected").val();
var getTask = JSON.parse(localStorage.getItem("input"));
var setTask = JSON.stringify(localStorage.setItem("input"));
console.log("text input has been saved");
}
thanks for any help!
There are some tasks here:
Get the right input's value
var task = $(f.target).closest('.dropdown-menu').next().val();
$(f.target).closest('.dropdown-menu') gets you to the dropdown and the input is next() to it.
localStorage.setItem accepts 2 arguments: (1) The key name (2) The data (You pass only the key).
In order to set the input's value to the value stored in localStorage, you need to store each input as a different key (not just input).
You need to iterate the inputs and set its value from localStorage.
Since the value you store is string, you don't need to stringify / parse.
Another point is the inconsistency name and id. The first element (the one you clone later) has none. Also the id and name are different values because you increase it between (length++)
https://jsfiddle.net/moshfeu/kw8jfg1m/32/

Storings some fields in local storage

I want to store every created field in localStorage to get the ID's for later use.
This is a cordova app.
I'm creating field with javascript. An outside DIV, and input field and a button.
This input field and button has to be stored in localStorage for later use.
I have stored the div group ID, input Field ID and button ID in localStorage. I do not know if this is the best solution, but if not, then I would appreciate some pointers.
This is how I store it:
localStorage.setItem('inputGroup', 'input_area_group_' + id + '');
localStorage.setItem('inputField', 'input_field_' + id + '')
localStorage.setItem('delButton', 'delBtn_' + id + '');
Now.
This is the first time I'm acctualy using localStorage, so I do not have that big clue about it, but at least I'm trying.
When I store these 3 values into local storage and create another field with the excact same values. Will those then overwrite the other results or will there be an array with all the results in "inputGroup" f.eks? Like if I have 40 fields, I would like to have 40 values in "inputGroup" of localstorage.
This is because when I have created these fields, I would like to delete one of them if I want to. And then I tought that I could fetch the ID in localstorage that match the ID of the button that I pressed.
Is this a viable solution?
If not, then I'm happy with some pointers to what I could use that would make this much better.
LocalStorage is a dictionary, so the first part (the key) is unique. The second part, the value is tied to the key. Changing the value portion will replace, not add a new one. You could change the key value if you wanted instead.
Or you, can store the value as a delimited string, concatenating the new string each time. (However, concatenation is generally a slow operation). In this case, you could take the concatenated string and convert it to a JavaScript array.

Syntax for using a cookie as an object

Trying to set a field name in a form.
SnapEngage.setCustomField(fieldName, fieldValue);
fieldName is the name of field on the form, I want to fill the fieldValue with the visitor's location_ID in their cookies using JavaScript. What would be the syntax for that object? I'm thinking something like self.location_ID but obviously it's not that.
I am not trying to set the cookie, rather use the cookie that is already set to fill in a form field.

CakePHP omitting to send an input field and changing input value onSubmit?

I am on CakePHP 1.2 at the office and, following my last question, I would like to send the array key of the selected option in a SELECT input instead of sending its actual value. I have tried a few things with the Model::beforeSave() function, without success.
I am aware that the data posted by CakePHP does not include the whole array, but only the selected value.
Here is what the function looks like at the moment:
function beforeSave(){
$this->Post->set('category_id', = array_keys($this->data['Annonce']['category_id']);
# debug($this->data);
}
Would there be a way to store the array keys into an hidden input and changing this input value depending on the user's selected item in the SELECT input, and to also omit sending the user's input but still send the hidden value?
$categories = Set::combine($categories,'{n}.categories.id', '{n}.categories.nom');
This did it for me... CakePHP assigns the array_keys() values automatically to the value field of the input.

Binding from Html Dom elements to json objects

I am working on a search panel, where I want to bind the data entered in these input elements to a search parameter json object. This search parameter will then be used for, you guessed it right, searching.
For example, user when searching for another person and he can specify the name, age or sex of the person on the page. I have json object which has, as its members Name, Age and Sex. I want to bind the inputs entered in the corresponding input elements on the page to this JSON object automatically, so when the user clicks on the Search I will just use whatever the json object has as a search param.
This is primarily to avoid having to - first find the corresponding element and then assign the corresponding member of the JSON object to the input in this field.
I could find jquery plugins (Databind) , which do the other way round i.e. transfer the values of a JSON object to the input elements.
Thanks in advance!!
I think you are confusing your terminology: presumably you mean a Javascript object.
Anyway, your object or json string need to be constructed when the user clicks "Search". It will be difficult/fiddly to assign these as the user enters text into the input fields. You could use the onblur event but you're just making unnecessary work for yourself.
Far easier is just to give each input field an id, and when the user clicks "Search" you build your object, then JSONify it. Here's how you might do it (not tested!):
<input type="text" id="name" />
<input type="text" id="age" />
...
var obj = { };
obj.name = document.getElementById('name').value;
obj.age = document.getElementById('age').value;
...
var json = JSON.stringify(obj);

Categories