I have list of students like as follows form from user input form:
//student form input for 1st time
var student={name:"a",roll:"9",age:13}
//student form input for 2nd time
var student={name:"b",roll:"10",age:14}
//student form input for 3rd time
var student={name:"c",roll:"11",age:15}
Actually, i am developing phonegap applications. Each time the user submit form-input i.e. student information, I want to save them into localstorage. Finally, when online, i want to sync them. I know i can store them in localstorage as follows:
localStorage.setItem("studentinfo", JSON.Stringfy(student));
But, this will remove the first student info in the local storage when i save second student info.
Infact, when i save first, second and third input respectively, i want to add them in localstorage array and finally the result in localstorage should be like
key=studentlist,
value=[
{name:"a",roll:"9",age:13},
{name:"b",roll:"10",age:14},
{name:"c",roll:"11",age:15}
]
How can it be done in localstorage or phonegap localstorage?
You want to hold all your students in an array like this:
var students = [];
students.push({name:"a",roll:"9",age:13});
students.push({name:"b",roll:"10",age:14});
students.push({name:"c",roll:"11",age:15});
And then store that in localStorage:
localStorage.setItem('studentsInfo', JSON.stringify(students));
The best way to do that would be with a function like this:
// When you get more student information, you should:
var addNewStudent = function (name, roll, age) {
// retrieve it (Or create a blank array if there isn't any info saved yet),
var students = JSON.parse(localStorage.getItem('studentsInfo')) || [];
// add to it,
students.push({name: name, roll: roll, age: age});
// then put it back.
localStorage.setItem('studentsInfo', JSON.stringify(students));
}
Related
I have a form with a lot of inputs and values. I want to push all the data to a firebase database. I could assign variables to each field value as:
let name = $("#field1").val();
let surname = $("#field2").val();
etc etc, but this feels very inefficient.
I want to create a object and loop through all input fields and map their name and value, something like this:
const collection = {};
$('form input').each(function () {
collection[$(this).attr('name')] = $(this).val();
});
Then I want to push all the data to firebase. But how do I push the entire object to firebase?
That's as simple as:
firebase.database().ref().push(collection);
Of if you want it under a specific location/path in the database:
firebase.database().ref("specific/place").push(collection);
Btw: I highly recommend reading the Firebase documentation on reading/writing lists of data.
I need to compare some values in my Firebase Database using JavaScript, but I'm having a hard time doing that. See image above.
The way that is suppose to work is I need to check if the user likes the same id as the other user. So for example:
User XbsX0IskrHVcaEmEBgyeok9isiM2 liked 4 items with unique ID's. Now I need to check if user jBc2Ls32DgMUSgzKUkVSw38UjQD2 liked the same thing to see if it's a match.
I have this code:
var check = ref.child('likes').child(uid2).child(uid2);
but it's not working.
You could iterate over the likes of the first user and make sure that everyone exists on the list of likes of the second user:
// Gets the list of likes from both users
var likes_user_1 = ref.child('likes').child(uid1);
var likes_user_2 = ref.child('likes').child(uid2);
// Iterates over the list of likes of the first user
var isAMatch = Object.keys(likes_user_1).every(function(like) {
// Returns true if user_2 has this like
return likes_user_2[like];
});
Hi i have add to cart button in my site. When button is clicked then the value in the form is store to a variable . var cartData = $('form').serialize();
My requirement is when the user select select next product in the page without refreshing , the form data need to save in var cartData without losing previous data,i.e i need to save this data as array. How can i do that. when i use var cartData = $('form').serializeArray(); then the cartdata only contain new valus , not previous values of form serialize.
form data need to save in var cartData without losing previous data,i.e i need to save this data as array
So you can push serialize result to actual array. For example:
var cartData = [];
// Save first form
cartData.push($('form').serialize());
// Later next form
cartData.push($('form').serialize());
This way you can store all forms you need in one data array.
var btn = document.getElementById('insert'),
s = 0; //To increment the key(+1) each time the button is clicked and the user inputs the values so the first object's key is 1 then the second one is 2 and so on
btn.addEventListener('click', function() {
var ModelMake = prompt("Enter The Model Make"),
ModelYear = prompt("Enter The Model Year"),
Km = prompt("Enter The Amount Of Km"),
Price = prompt("Enter The Price"),
Status = prompt("Enter The Car's Status"),
table = document.getElementById('table');
var tableout = document.getElementById('tableout'),
FinalPrice,
Details;
localStorage.setItem(s += 1, JSON.stringify({
ModelMake: ModelMake,
ModelYear: ModelYear,
Km: Km,
Price: Price,
Status: Status,
FinalPrice: FinalPrice,
Details: Details
}));
This code inserts the following variables values(user inputs them) in a row and each value is in a column (cell), when i refresh the object IS stored in the localStorage even when i close my browser obviously but i want a way to keep it in the html whether i refresh or close the browser without having to extract it from the localStorage because i don't know how and it's not really what i want.
You could store your data in a cookie. Here is a tutorial that may be of use: Javascript and Cookies
If you want to do exactly, what you are asking I see few options for you:
on page load you can go thru all localStorage keys, find max number and assign it to S(Here is answer how to go thru all keys: Get HTML5 localStorage keys). I don't really like this way, but...
Store s value as separate key/value pair and extract it on page load
I have create a add record form in extjs which stores user entered data into grid.
Well , if found following method to get form values from formPanel
var formPanel = new Ext.form.FormPanel({...});
var form = formPanel.getForm();
var firstName = form.findField('firstname').getValue();
but i want to covert all the user input value into JSON and want to store into Grid panel and want to send it to the server also. but using findField i have to manually create the array and then need to encode it into JSON , so is there any alternate way to directly read values from form and convert it into JSON and store it into Grid Panel.
When you say "want to store in the GridPanel" would you be updating an existing record in the Grid's store or would you be inserting a new one or both? (depending on whether its add or update probably?)
For such situations, BasicForm (var form in your snippet above) provides updateRecord( Record record ) method.
So your steps would be -
var record = Record.create(...) // in case of insert
OR
var record = //obtain record from grid.getStore() in case of update
Then,
formPanel.getForm().updateRecord(record); //update the passed record with values from the form
Followed by committing the record back to the store-
grid.getStore().add(record); //in case of insert
grid.getStore().commitChanges(); //incase of update
Reference - Ext.form.BasicForm , Ext.data.Record
Define your record type -
MyRecordType = Ext.data.Record.create(['id', 'field1', 'field2']);
var myrec = new MyrecordType();
Now pass myrec to updateRecord as mentioned above.