js localStorage editable table 示例

 

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8"/>
	<title></title>
 
	<style type="text/css">
		body{
			background-color:#eeeeee;
		}
		table{
			border:2px solid red;
			border-collapse: collapse;
		}
		th{
			background-color: #008080;
			color:white;
		}
		
		td{
			width:auto;
			min-width: 20em;
			background-color: white;
			border:1px solid red;
 
		}
		input{
			border: none;
			width: 100%;
		}
	</style>
 
	<script type="text/javascript">
		//创建一个命名的本地存储 原有本地数据会被清除
		function createLocalStorage(storageName){
			if(!localStorage){
				console.log("localStorage is not supported!");
				return;
			}
			localStorage[storageName]="[]";
			console.log("localStorage "+storageName+" is created!");
 
		}
 
 
		//保存数据到本地
		//注意data的格式是二维数组
		function saveDataToLocal(data,storageName){
			if(!localStorage){
				console.log("localStorage is not supported!");
				return;
			}
			console.log("data to be saved: "+data);
			//先从本地取出原有数据
			var oldData=JSON.parse(localStorage[storageName]);
			console.log("oldData length: "+oldData.length);
			//新数据合并一起
			for(i=0;i<data.length;i++){
				oldData.push(data[i]);
			}
			//合并后的数据再存入本地
			localStorage[storageName]=JSON.stringify(oldData);
			console.log("saved data: "+localStorage[storageName]);
 
		}
 
		//将数据显示为可编辑的table
		function showEditTable(tableData,tableContainerId){
			tableData=tableData;
			mytable=document.createElement("table");
			for(i=0;i<tableData.length;i++){
				tableRow=document.createElement("tr");
				mytable.appendChild(tableRow);
				for(j=0;j<tableData[i].length;j++){
					tableCell=document.createElement("td");
					textInput=document.createElement("input");
					textInput.type="text";
					textInput.value=tableData[i][j];
					textInput.id="cell_"+i+"_"+j;
					textInput.i=i
					textInput.j=j;
					tableCell.appendChild(textInput);
					tableRow.appendChild(tableCell);
					textInput.onchange=function(event){
						var eventElement=event.target;
						tableData[eventElement.i][eventElement.j]=eventElement.value;						
					}
					textInput.onkeypress=function(event){
						if(event.keyCode==13){
							var eventElement=event.target;
							if(eventElement.i<tableData.length-1){
								var targetCellId="cell_"+(parseInt(eventElement.i)+1)+"_"+eventElement.j;
								document.getElementById(targetCellId).focus();
							}else if(eventElement.i==(tableData.length-1)){
								tableData[eventElement.i][eventElement.j]=eventElement.value;
								var tableColumnCount=tableData[0].length;
								var item=[];
								for(ci=0;ci<tableColumnCount;ci++){
									item.push("");
								}
								tableData.push(item);
								showEditTable(tableData,tableContainerId);
								var targetCellId="cell_"+(parseInt(eventElement.i)+1)+"_"+eventElement.j;
								document.getElementById(targetCellId).focus();
							}
 
						}
					}	
					
				}
				
			}
			document.getElementById(tableContainerId).innerHTML="";
			document.getElementById(tableContainerId).appendChild(mytable);
		}
 
 
		//页面按钮事件
		function getLocalStorageData(){
			data=JSON.parse(localStorage["bbb"]);
			showEditTable(data,"tableContainer");
			tableData=data;
		}
 
		function setLocalStorageData(){
			data=tableData;
			saveDataToLocal(data,"bbb");
		}	
 
	</script>
</head>
<body>
 
<button title="注意!旧的数据会被清空!" onclick="createLocalStorage('bbb')">新建本地存储</button>
<button onclick="setLocalStorageData()">保存到本地</button>
<button onclick="getLocalStorageData()">从本地导入</button>
 
 
<div id="tableContainer"></div>
 
<script type="text/javascript">
	//数据格式为多维数组
	tableData=[["","",""]];
 
	showEditTable(tableData,"tableContainer");	
 
	console.log("bbb:"+JSON.parse(localStorage["bbb"]));
 
</script>
 
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值