【发布时间】:2019-09-30 18:49:50
【问题描述】:
所以我构建了一个应用程序来跟踪我的每月预算 - 费用、收入、百分比等。我在这里试图解决的问题是,当我将我的项目添加到用户界面(前端)时我希望他们在我重新加载页面后保留总预算,因为现在他们在 HTML 代码中有值,我将其更改为零。有人告诉我这里与 JSON 有关,但我不知道如何使用它。如果有人可以帮助我,将不胜感激。我无法粘贴整个代码,因为它是 450 行 javascript。
我尝试了不同的方法,但都没有奏效。
// GLOBAL APP CONTROLER
var controller = (function(budgetCtrl, UICtrl) {
var setupEventListeners = function() {
var DOM = UICtrl.getDOMstrings();
document.querySelector(DOM.inputBtn).addEventListener('click', ctrlAddItem);
document.addEventListener('keypress', function(event) {
if (event.keyCode === 13 || event.which === 13) {
ctrlAddItem();
}
});
document.querySelector(DOM.container).addEventListener('click', ctrDeleteItem);
document.querySelector(DOM.inputType).addEventListener('change', UICtrl.changedType);
};
var updateBudget = function() {
// 1. Calculate the budget
budgetCtrl.calculateBudget();
// 2. Return Budget
var budget = budgetCtrl.getBudget(); // the getBudget method only returns the budget,inc,exp and %
// 3. Display the budget on the UI
UICtrl.displayBudget(budget);
};
var updatePercentages = function() {
// 1. Calculate the percentages
budgetCtrl.calculatePercentages();
// 2. Read percentages from budget controller
var percentages = budgetCtrl.getPercentages();
// 3. Display the percentages on the UI
UICtrl.displayPercentages(percentages);
};
var ctrlAddItem = function () {
var input, newItem;
// 1. Get the field input data
input = UIController.getInput();
// we check if there is an input description or value so the code runs
if (input.description !== '' && !isNaN(input.value) && input.value > 0) {
// 2. Add the item to the budget controler
newItem = budgetCtrl.addItem(input.type, input.description, input.value);
// 3. Add the new item to the UI
UICtrl.addListItem(newItem, input.type);
// 4. Clear the fields
UICtrl.clearFields();
// 5. Calculate and update Budget
updateBudget();
// 6. Calculate and update percentages
updatePercentages();
}
};
var ctrDeleteItem = function(event) {
var itemID, splitID, type, ID;
itemID = event.target.parentNode.parentNode.parentNode.parentNode.id; // parentNode gets the parent of the target of the event
if (itemID) {
// inc-1
splitID = itemID.split('-'); // split returns an array with the other items except the '-'
type = splitID[0];
ID = parseInt(splitID[1]);
// 1. Delete the item from the data structure
budgetCtrl.deleteItem(type, ID);
// 2. Delete the item from the UI
UICtrl.deleteListItem(itemID);
// 3. Update and show the new budget
updateBudget();
// 4. Calculate and update percentages
updatePercentages();
}
};
return {
init: function() {
console.log('Application has started.');
UICtrl.displayMonth();
// 3. Display the budget on the UI
UICtrl.displayBudget({
budget: 0,
totalInc: 0,
totalExp: 0,
percentage: -1
});
setupEventListeners();
}
}
})(budgetController, UIController);;
我目前将数据存储在数据控制器中
// BUDGET CONTROLLER
var budgetController = (function() {
// we create a constructor
var Expense = function(id, description, value) {
this.id = id;
this.description = description;
this.value = value;
this.percentage = -1;
};
// this method in the prototype of expense calculates %
Expense.prototype.calcPercentage = function(totalIncome) {
if (totalIncome > 0) {
this.percentage = Math.round((this.value / totalIncome) * 100);
} else {
this.percentage = -1
}
};
// this method in the prototype of expense returns the %
Expense.prototype.getPercentage = function() {
return this.percentage;
};
var Income = function(id, description, value) {
this.id = id;
this.description = description;
this.value = value;
};
var calculateTotal = function(type) {
var sum = 0;
data.allItems[type].forEach(function(cur) {
sum += cur.value;
});
data.totals[type] = sum;
};
// we store the incomes' description in all items array and the price in the totals
var data = {
allItems: {
exp: [],
inc: []
},
totals: {
exp: 0,
inc: 0
},
budget: 0,
percentage: -1
};
【问题讨论】:
-
目前,你在哪里存储值?
-
您尝试过 Web 会话或本地存储吗?
-
我目前将值存储在数据控制器中
标签: javascript html css json