【问题标题】:Why total Budget is not getting updated?为什么总预算没有更新?
【发布时间】:2018-12-09 16:21:59
【问题描述】:

有人可以在 codePen 中检查我的代码吗? 将项目添加到列表后,它应该在控制台中记录总预算。

它只显示 0。请查看 codePen 控制台中的代码。 https://codepen.io/crazydeveloper/pen/KbKvMp

budCalc: function() {
var budget, percent, totalInc, totalExp;

totalInc = data.totals.inc;
totalExp = data.totals.exp;

budget = data.totals.inc - data.totals.exp;
percent = (data.totals.exp / data.totals.inc)*100
console.log(budget);


///////// BUDGET CONTROLLER ////////
var budgetController = (function() {

var Expence = function(id, des, value) {
this.id = id;
this.des = des;
this.value = value;
}

var Income = function(id, des, value) {
this.id = id;
this.des = des;
this.value = value;
}

var data = {
allItems: {
    inc: [],
    exp: []
},

totals: {
    inc: 0,
    exp: 0,
    budget: 0,
    percent: 0
}

}

return {
addItem: function(type, des, val) {
    var newItem, id;

    if (data.allItems[type].length > 0) {
        var id = data.allItems[type][data.allItems[type].length - 1].id + 
1;
    } else {
        id = 0;
    }

    if (type === "exp") {
        newItem = new Expence(id, des, val);
    } else if (type === "inc") {
        newItem = new Income(id, des, val);
    }

    data.allItems[type].push(newItem);
    return newItem;
},

calcTotal: function(type) {
    sum = 0;
    data.allItems[type].forEach(function() {
        sum += data.totals[type];
        data.totals[type] = data.totals[type] + sum;
    }
)
},

budCalc: function() {
var budget, percent, totalInc, totalExp;

totalInc = data.totals.inc;
totalExp = data.totals.exp;

budget = data.totals.inc - data.totals.exp;
percent = (data.totals.exp / data.totals.inc)*100
console.log(budget);

var getBudget = function() {
    return {
        totalInc: totalInc,
        totalExp: totalExp,
        totalBudget: budget,
        percent: percent
    }
}
},
    testing: function() {
    console.log(data);
}
}
}());




////////// UI CONTROLLER //////////////
var UIController = (function() {
var DOMs = {
inpType: ".add__type",
inpDes: ".add__description",
inpVal: ".add__value",
inpBtn: ".add__btn",
incCon: ".income__list",
expCon: ".expenses__list"
}
return {
    getInp: function() {
        return {
            type: $(DOMs.inpType).val(),
            des: $(DOMs.inpDes).val(),
            val: parseFloat($(DOMs.inpVal).val())
        }
    },

    addListItem: function(obj, type) {
        var html, newHTML, ele;

        if(type === "inc") {
            ele = DOMs.incCon;
            html = '<div class="item clearfix" id="income-%id%"><div 
class="item__description">%des%</div><div class="right clearfix"><div 
class="item__value">+ %val%</div><div class="item__delete"><button 
class="item__delete--btn"><i class="ion-ios-close-outline"></i></button> 
</div></div></div>';
        } else if(type === "exp") {
            ele = DOMs.expCon;
            html = '<div class="item clearfix" id="expense-%id%"><div 
class="item__description">%des%</div><div class="right clearfix"><div 
class="item__value">- %val%</div><div class="item__percentage">21%</div> 
<div 
class="item__delete"><button class="item__delete--btn"><i class="ion-ios- 
close-outline"></i></button></div></div></div>';
        }

        newHTML = html.replace("%id%", obj.id);
        newHTML = newHTML.replace("%des%", obj.des);
        newHTML = newHTML.replace("%val%", obj.value);

        $(ele).append(newHTML);

        this.clearFields();

    },

    clearFields: function() {
        $(DOMs.inpDes).add(DOMs.inpVal).val("");
        $(DOMs.inpDes).focus();
    },

    getDOM: function() {
        return DOMs;
    }
    }
}
());


////////// MAIN CONTROLLER /////////
var controller = (function(budgetCtrl, UICtrl) {

var eventLis = function() {
    var DOM = UICtrl.getDOM();
    $(DOM.inpBtn).on("click", eventBtn);

    $("html").on("keypress", function() {
    if (event.keyCode === 13 || event.which == 13) {
    eventBtn();
    }
})
}

function eventBtn() {
var input = UICtrl.getInp();

if(input.des !== "" && !isNaN(input.val) && input.val > 0) {
    var newItem = budgetCtrl.addItem(input.type, input.des, input.val);
    UICtrl.addListItem(newItem, input.type);
}
budgetCtrl.budCalc();
}

return {
    init: function() {
        console.log("Application started!");
        eventLis();
    }
}

})(budgetController, UIController);

controller.init();

这是所有的 JavaScript 代码。但我可以弄清楚为什么我的预算没有更新?

【问题讨论】:

标签: javascript error-handling


【解决方案1】:

如果此时检查完整的数据结构对象

budCalc: function() {
var budget, percent, totalInc, totalExp;

//here
console.log(data);

totalInc = data.totals.inc;
totalExp = data.totals.exp;

budget = data.totals.inc - data.totals.exp;
percent = (data.totals.exp / data.totals.inc)*100
}

您会看到 data.total 属性每次都设置为零

【讨论】:

  • 我该如何解决这个问题?
  • 取决于你想要什么,这里的值设置为零并且从不修改,你需要在 calcTotal function() 中进行吗?
猜你喜欢
  • 2019-09-22
  • 1970-01-01
  • 2020-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多