1
I have the following problem: I can save the user inputs in the firebase database, but I cannot sum the values in Javascript and make available in real time on the page.
I want to add the data of "age" and save in "total"
var usersList = document.getElementById('usersList');
var usersNum = document.getElementById('usersNum');
var nameInput = document.getElementById('nameInput');
var ageInput = document.getElementById('ageInput');
var addButton = document.getElementById('addButton');
// Ao clicar no botão
addButton.addEventListener('click', function () {
create(nameInput.value, ageInput.value);
});
// Função para criar um registro do pedido no Firebase
function create(name, age) {
var data = {
name: name,
age: age
};
return firebase.database().ref().child('users').push(data);
}
firebase.database().ref('users').on('value', function (snapshot) {
usersList.innerHTML = '';
snapshot.forEach(function (item) {
var li = document.createElement('li');
li.appendChild(document.createTextNode(item.val().name + ': ' + item.val().age));
usersList.appendChild(li);
});
});
// Função para criar o total no Firebase
function create(totaldisp) {
var datadisp = {
totaldisp: totaldisp,
};
return firebase.database().ref().child('users').push(datadisp);
}
firebase.database().ref('users').on('value', function (snapshot) {
usersNum.innerHTML = '';
snapshot.forEach(function (item) {
var li = document.createElement('li');
li.appendChild(document.createTextNode(item.val().totaldisp + "disponíveis"));
usersNum.appendChild(li);
});
});
Trecho do HTML
<h1>Status dos Pedidos</h1>
<br>
<ul id="usersList"></ul>
<hr>
<h1>Total de Reservas</h1>
<br>
<ul id="usersNum"></ul>
<hr>