課程連結,內容的製作在課程中原本為使用JS vanilla,而自己在專案上的製作則使用jQuery來練習。
完成後的成品
$(document).ready(function () {
$('.btn').click(function (e) {
e.preventDefault();
console.log('click');
addTransactions('cash', '200');
})
});
function addTransactions(name, amount) {
var Transaction_str = `<li class='plus' >${name}<span> ${amount}</span><button class="delete-btn">x</button></li>`
$('#list').append(Transaction_str);
}
//
$('#text').val('');
$('#amount').val('');
val()
取得輸入框的值,帶入addTransactions()
函式將資料顯示畫面$(document).ready(function () {
$('.btn').click(function (e) {
e.preventDefault();
console.log('click');
//取得表格中的值
const text_val = $('#text').val();
const amount_val = $('#amount').val();
addTransactions(text_val, amount_val);
})
});
$('.delete-btn').last().click(function () {
console.log('del');
})
}
$('.delete-btn').last().click(function () {
console.log(this);
})
}
$('.delete-btn').last().click(function () {
$(this).parent().remove();
})
從此實作,可以看到它是一個陣列,裡面是物件
//預設值的設置,取得存在本地的資料或是空陣列
var transactions = JSON.parse(localStorage.getItem('Transactions')) || [];
//推入陣列
transactions.push({
name: text_val,
amount: amount_val
})
localStorage.setItem('Transactions', JSON.stringify(transactions));
//init
function initHistory(transactions) {
transactions.forEach(transaction => {
addTransactions(transaction.name, transaction.amount);
});
}
//將此函式放於 $(document).ready()之中
//陣列初始
if (transactions.length > 0) {
initHistory(transactions);
}
// Generate random ID
function generateID() {
return Math.floor(Math.random() * 100000000);
}
//floor:無條件捨去
//ceil:無條件進位
const id = generateID();
addTransactions(id, text_val, amount_val);
//推入陣列
transactions.push({
id: id,
name: text_val,
amount: amount_val
})
function addTransactions(id, name, amount) {
console.log(id, name, amount);}
和資料有關的時候:data-原來欄位名稱
const Transaction_str = `<li class='plus' >${name}<span> ${amount}</span><button class="delete-btn" data-id="${id}">x</button></li>`
function deleteFromLocalstorage(transactions, id) {
transactions.forEach(function (item, index, arr) {
//console.log('item', item);
//console.log('index', index);
//console.log('arr', arr);
if (item.id === id) {
arr.splice(index, 1);
}
});
//迴圈刪除後,要儲存到localStorage才有確實刪去
localStorage.setItem('Transactions', JSON.stringify(transactions));
}
localStorage.setItem('Transactions', JSON.stringify(transactions));
addTransactions()
函式之中,判斷 amount 的正負值,再給予相應樣式const Transaction_str = $('<li></li>').appendTo('#list');
Transaction_str.addClass(amount < 0 ? 'minus' : 'plus');
Transaction_str.html(`${name}<span> ${amount}</span><button class="delete-btn" data-id="${id}">x</button>`);
$('#list').append(Transaction_str);
function updateValue(transactions) {
const amounts_arr = transactions.map(function (transaction) {
return transaction.amount
})
console.log(amounts_arr);
//計算加總
var total = 0;
$.each(amounts_arr, function () { total += parseFloat((this)) || 0; });
//傳回YOUR BALANCE
$('#balance').text(`$${total}`);
console.log(total);
//*************************** */
//從陣列找出>0的值,放置income
//console.log(amounts)
var income = amounts_arr.filter(
function (item) {
return item > 0
}
)
console.log(income);
var totalIncome = 0;
$.each(income, function () { totalIncome += parseFloat((this)) || 0; });
//console.log(totalIncome); //回傳220
//傳回到income
$('#money-plus').text(`$${totalIncome}`);
//***************************** */
//從陣列找出<0的值,放置income
var expense = amounts_arr.filter(
function (item) {
return item < 0
}
)
console.log(expense);
var totalExpense = 0;
$.each(expense, function () { totalExpense += parseFloat((this)) || 0; });
console.log(totalExpense);
//傳回到expense
$('#money-minus').text(`$${totalExpense}`)
}
const amounts_arr = transactions.map(function (transaction) {
return transaction.amount
})
console.log(amounts_arr);
var total = 0;
$.each(amounts_arr, function () { total += parseFloat((this)) || 0; });
//傳回YOUR BALANCE
$('#balance').text(`$${total}`);
console.log(total);
//從陣列找出>0的值,放置income
//console.log(amounts)
var income = amounts_arr.filter(
function (item) {
return item > 0
}
)
console.log(income);
var totalIncome = 0;
$.each(income, function () { totalIncome += parseFloat((this)) || 0; });
//console.log(totalIncome); //回傳220
//傳回到income
$('#money-plus').text(`$${totalIncome}`);
//從陣列找出<0的值,放置expense
var expense = amounts_arr.filter(
function (item) {
return item < 0
}
)
console.log(expense);
var totalExpense = 0;
$.each(expense, function () { totalExpense += parseFloat((this)) || 0; });
console.log(totalExpense);
//傳回到expense
$('#money-minus').text(`$${totalExpense}`)