
本文档将指导你如何使用 jQuery 将表格数据保存到浏览器的 localStorage 中。localStorage 允许你将数据以键值对的形式存储在用户的浏览器中,即使关闭浏览器后数据仍然存在。本文将提供详细的代码示例,帮助你理解如何读取、存储和更新 localStorage 中的数据,并将其应用于 CRUD 操作的表格中。
localStorage 是 Web Storage API 的一部分,它提供了一种在客户端存储键值对数据的机制。与 cookie 相比,localStorage 存储容量更大,且不会随每次 HTTP 请求发送到服务器,因此更适合存储大量客户端数据。
localStorage 的基本用法非常简单,主要涉及以下几个方法:
为了将表格数据保存到 localStorage,我们需要在每次创建、更新或删除表格行时,将数据序列化并存储。以下是如何将示例代码中的数据保存到 localStorage 的步骤:
获取表格数据: 在保存数据之前,需要从表格中提取所有行的数据。可以将每一行的数据转换为一个 JavaScript 对象,然后将所有行的数据存储在一个数组中。
序列化数据: 由于 localStorage 只能存储字符串,我们需要将 JavaScript 对象或数组序列化为 JSON 字符串。可以使用 JSON.stringify() 方法来实现。
存储数据: 使用 localStorage.setItem() 方法将序列化后的 JSON 字符串存储到 localStorage 中。
读取数据: 在页面加载时,从 localStorage 中读取数据,并将其反序列化为 JavaScript 对象或数组。可以使用 JSON.parse() 方法来实现。
更新表格: 使用读取的数据更新表格。
以下代码展示了如何修改示例代码,将表格数据保存到 localStorage 中:
$(document).ready(function () {
// 定义 localStorage 的键名
const localStorageKey = 'tableData';
// 从 localStorage 加载数据
function loadDataFromLocalStorage() {
const storedData = localStorage.getItem(localStorageKey);
if (storedData) {
const data = JSON.parse(storedData);
// 清空表格
$("#tblData tbody").empty();
// 循环创建表格行
data.forEach(function (item) {
const newRow = "<tr>" +
"<td class='tdID'>" + item.id + "</td>" +
"<td class='tdName'>" + item.name + "</td>" +
"<td class='tdAddress'>" + item.address + "</td>" +
"<td class='tdAge'>" + item.age + "</td>" +
"<td class='tdAction'>" + rowButtons + "</td>" +
"</tr>";
$("#tblData tbody").append(newRow);
});
} else {
$("#tblData tbody").append(emptyRow); // adding empty row on page load
}
}
// 保存数据到 localStorage
function saveDataToLocalStorage() {
const tableData = [];
$("#tblData tbody tr").each(function () {
const id = $(this).find(".tdID").text() || $(this).find(".txtID").val();
const name = $(this).find(".tdName").text() || $(this).find(".txtName").val();
const address = $(this).find(".tdAddress").text() || $(this).find(".txtAddress").val();
const age = $(this).find(".tdAge").text() || $(this).find(".txtAge").val();
if (id || name || address || age) {
tableData.push({
id: id,
name: name,
address: address,
age: age
});
}
});
localStorage.setItem(localStorageKey, JSON.stringify(tableData));
}
// 页面加载时加载数据
loadDataFromLocalStorage();
$("#btnAdd").click(function () {
if ($("#tblData tbody").children().children().length == 1) {
$("#tblData tbody").html("");
}
$("#tblData tbody").append(emptyNewRow);
});
$('#tblData').on('click', '.btn-save', function () {
const id = $(this).parent().parent().find(".txtID").val();
$(this).parent().parent().find(".tdID").html("" + id + "");
const name = $(this).parent().parent().find(".txtName").val();
$(this).parent().parent().find(".tdName").html("" + name + "");
const address = $(this).parent().parent().find(".txtAddress").val();
$(this).parent().parent().find(".tdAddress").html("" + address + "");
const age = $(this).parent().parent().find(".txtAge").val();
$(this).parent().parent().find(".tdAge").html("" + age + "");
$(this).parent().parent().find(".tdAction").html(rowButtons);
saveDataToLocalStorage(); // 保存数据
});
$('#tblData').on('click', '.btn-danger', function () {
$(this).parent().parent().remove();
if ($("#tblData tbody").children().children().length == 0) {
$("#tblData tbody").append(emptyRow);
}
saveDataToLocalStorage(); // 保存数据
});
$('#tblData').on('click', '.btn-cancel', function () {
$(this).parent().parent().remove();
saveDataToLocalStorage(); // 保存数据
});
$('#tblData').on('click', '.btn-edit', function () {
const id = $(this).parent().parent().find(".tdID").html();
$(this).parent().parent().find(".tdID").html("<input type='text' value='" + id + "' class='form-control txtID' />");
const name = $(this).parent().parent().find(".tdName").html();
$(this).parent().parent().find(".tdName").html("<input type='text' value='" + name + "' class='form-control txtName' />");
const address = $(this).parent().parent().find(".tdAddress").html();
$(this).parent().parent().find(".tdAddress").html("<input type='text' value='" + address + "' class='form-control txtAddress' />");
const age = $(this).parent().parent().find(".tdAge").html();
$(this).parent().parent().find(".tdAge").html("<input type='text' value='" + age + "' class='form-control txtAge' />");
$(this).parent().parent().find(".tdAction").html(rowUpdateButtons);
});
});关键修改说明:
通过本文,你学习了如何使用 jQuery 将表格数据保存到 localStorage 中。这可以让你在用户的浏览器中持久化存储数据,即使关闭浏览器后数据仍然存在。请记住,localStorage 只能存储字符串,且存储容量有限,因此在使用时需要注意。通过适当的错误处理和浏览器兼容性检查,你可以安全地使用 localStorage 来增强你的 Web 应用程序的功能。
以上就是使用 jQuery 将数据保存到 localStorage的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号