jQuery(非HTML5)可編輯表格實(shí)現(xiàn)代碼
更新時(shí)間:2012年12月11日 09:34:41 作者:
單擊單元格選中,選中過程中使用方向鍵更換選中的單元格,選中過程中按回車鍵或者直接雙擊單元格進(jìn)入可編輯狀態(tài),單元格失去焦點(diǎn)時(shí)保存修改的內(nèi)容
功能:
單擊單元格選中,選中過程中使用方向鍵更換選中的單元格,選中過程中按回車鍵或者直接雙擊單元格進(jìn)入可編輯狀態(tài),單元格失去焦點(diǎn)時(shí)保存修改的內(nèi)容。
主要實(shí)現(xiàn)思路:
選中,移動(dòng)選中區(qū)域等都是依靠jQuery強(qiáng)大的API進(jìn)行實(shí)現(xiàn)的。而可編輯的單元格實(shí)際上是在選中單元格時(shí),在單元格上面添加個(gè)input輸入域,動(dòng)態(tài)的更新數(shù)據(jù)
源代碼:
HTML代碼:
<table class="editableTable">
<thead>
<tr>
<th>Item1</th>
<th>Item2</th>
<th>Item3</th>
</tr>
</thead>
<tbody>
<tr>
<td class="editable simpleInput">arthinking</td>
<td class="editable simpleInput">Jason</td>
<td class="editable simpleInput">itzhai</td>
</tr>
<tr>
<td class="editable simpleInput">arthinking</td>
<td class="editable simpleInput">Jason</td>
<td class="editable simpleInput">itzhai</td>
</tr>
<tr>
<td class="editable simpleInput">arthinking</td>
<td class="editable simpleInput">Jason</td>
<td class="editable simpleInput">itzhai</td>
</tr>
</tbody>
</table>
CSS代碼:
body{
text-shadow:#FFFFFF 1px 1px 1px;
}
.editableTable{
width: 220px;
padding: 10px;
background-color: #DDEEF6;
border:1px solid #DDEEF6;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
}
.editableTable thead{
background:#FFFFCC;
}
td{
background:#66CCFF;
cursor:pointer;
}
.selectCell{
background:#6699FF;
}
JS代碼:
var EdTable = function(){
// 給單元格綁定事件
function initBindGridEvent(){
$("td.editable").unbind();
// 添加單元格點(diǎn)擊事件
addGridClickEvent();
// 添加單元格雙擊事件
addGridDbClickEvent();
// 添加鍵盤事件
addGridKeyPressEvent();
}
// 給單元格添加單擊事件
function addGridClickEvent(){
$("td.simpleInput").bind("click",function(){
$('.simpleInput').each(function(){
$(this).removeClass("selectCell");
});
// 給選中的元素添加選中樣式
$(this).addClass("selectCell");
});
}
//給單元格添加雙擊事件
function addGridDbClickEvent(){
$("td.simpleInput").bind("dblclick",function(){
$('.simpleInput').each(function(){
$(this).removeClass("selectCell");
});
var val=$(this).html();
var width = $(this).css("width");
var height = $(this).css("height");
$(this).html("<input type='text' onblur='EdTable.saveEdit(this)' style='width:"+ width +";height:"+ height +"; padding:0px; margin:0px;' value='"+val+"' >");
$(this).children("input").select();
});
}
// 給單元格添加鍵盤事件
function addGridKeyPressEvent(){
$(document).keyup(function(event){
if(event.keyCode == 37){
// 左箭頭
var selectCell = $(".selectCell").prev()[0];
if(selectCell != undefined){
$(".selectCell").removeClass("selectCell").prev().addClass("selectCell");
}
} else if(event.keyCode == 38){
// 上箭頭
var col = $(".selectCell").prevAll().length;
var topCell = $(".selectCell").parent("tr").prev().children()[col];
if(topCell != undefined){
$(".selectCell").removeClass("selectCell");
$(topCell).addClass("selectCell");
}
} else if(event.keyCode == 39){
// 右箭頭
var selectCell = $(".selectCell").next()[0];
if(selectCell != undefined){
$(".selectCell").removeClass("selectCell").next().addClass("selectCell");
}
} else if(event.keyCode == 40){
// 下箭頭
var col = $(".selectCell").prevAll().length;
var topCell = $(".selectCell").parent("tr").next().children()[col];
if(topCell != undefined){
$(".selectCell").removeClass("selectCell");
$(topCell).addClass("selectCell");
}
} else if(event.keyCode == 13){
// 回車鍵
var selectCell = $(".selectCell")[0];
if(selectCell != undefined){
$(selectCell).dblclick();
}
}
});
}
// 單元格失去焦點(diǎn)后保存表格信息
function saveEdit(gridCell){
var pnt=$(gridCell).parent();
$(pnt).html($(gridCell).attr("value"));
$(gridCell).remove();
}
return{
initBindGridEvent : initBindGridEvent,
saveEdit : saveEdit
}
}();
源代碼下載:
EditableTable.rar
單擊單元格選中,選中過程中使用方向鍵更換選中的單元格,選中過程中按回車鍵或者直接雙擊單元格進(jìn)入可編輯狀態(tài),單元格失去焦點(diǎn)時(shí)保存修改的內(nèi)容。
主要實(shí)現(xiàn)思路:
選中,移動(dòng)選中區(qū)域等都是依靠jQuery強(qiáng)大的API進(jìn)行實(shí)現(xiàn)的。而可編輯的單元格實(shí)際上是在選中單元格時(shí),在單元格上面添加個(gè)input輸入域,動(dòng)態(tài)的更新數(shù)據(jù)
源代碼:
HTML代碼:
復(fù)制代碼 代碼如下:
<table class="editableTable">
<thead>
<tr>
<th>Item1</th>
<th>Item2</th>
<th>Item3</th>
</tr>
</thead>
<tbody>
<tr>
<td class="editable simpleInput">arthinking</td>
<td class="editable simpleInput">Jason</td>
<td class="editable simpleInput">itzhai</td>
</tr>
<tr>
<td class="editable simpleInput">arthinking</td>
<td class="editable simpleInput">Jason</td>
<td class="editable simpleInput">itzhai</td>
</tr>
<tr>
<td class="editable simpleInput">arthinking</td>
<td class="editable simpleInput">Jason</td>
<td class="editable simpleInput">itzhai</td>
</tr>
</tbody>
</table>
CSS代碼:
復(fù)制代碼 代碼如下:
body{
text-shadow:#FFFFFF 1px 1px 1px;
}
.editableTable{
width: 220px;
padding: 10px;
background-color: #DDEEF6;
border:1px solid #DDEEF6;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
}
.editableTable thead{
background:#FFFFCC;
}
td{
background:#66CCFF;
cursor:pointer;
}
.selectCell{
background:#6699FF;
}
JS代碼:
復(fù)制代碼 代碼如下:
var EdTable = function(){
// 給單元格綁定事件
function initBindGridEvent(){
$("td.editable").unbind();
// 添加單元格點(diǎn)擊事件
addGridClickEvent();
// 添加單元格雙擊事件
addGridDbClickEvent();
// 添加鍵盤事件
addGridKeyPressEvent();
}
// 給單元格添加單擊事件
function addGridClickEvent(){
$("td.simpleInput").bind("click",function(){
$('.simpleInput').each(function(){
$(this).removeClass("selectCell");
});
// 給選中的元素添加選中樣式
$(this).addClass("selectCell");
});
}
//給單元格添加雙擊事件
function addGridDbClickEvent(){
$("td.simpleInput").bind("dblclick",function(){
$('.simpleInput').each(function(){
$(this).removeClass("selectCell");
});
var val=$(this).html();
var width = $(this).css("width");
var height = $(this).css("height");
$(this).html("<input type='text' onblur='EdTable.saveEdit(this)' style='width:"+ width +";height:"+ height +"; padding:0px; margin:0px;' value='"+val+"' >");
$(this).children("input").select();
});
}
// 給單元格添加鍵盤事件
function addGridKeyPressEvent(){
$(document).keyup(function(event){
if(event.keyCode == 37){
// 左箭頭
var selectCell = $(".selectCell").prev()[0];
if(selectCell != undefined){
$(".selectCell").removeClass("selectCell").prev().addClass("selectCell");
}
} else if(event.keyCode == 38){
// 上箭頭
var col = $(".selectCell").prevAll().length;
var topCell = $(".selectCell").parent("tr").prev().children()[col];
if(topCell != undefined){
$(".selectCell").removeClass("selectCell");
$(topCell).addClass("selectCell");
}
} else if(event.keyCode == 39){
// 右箭頭
var selectCell = $(".selectCell").next()[0];
if(selectCell != undefined){
$(".selectCell").removeClass("selectCell").next().addClass("selectCell");
}
} else if(event.keyCode == 40){
// 下箭頭
var col = $(".selectCell").prevAll().length;
var topCell = $(".selectCell").parent("tr").next().children()[col];
if(topCell != undefined){
$(".selectCell").removeClass("selectCell");
$(topCell).addClass("selectCell");
}
} else if(event.keyCode == 13){
// 回車鍵
var selectCell = $(".selectCell")[0];
if(selectCell != undefined){
$(selectCell).dblclick();
}
}
});
}
// 單元格失去焦點(diǎn)后保存表格信息
function saveEdit(gridCell){
var pnt=$(gridCell).parent();
$(pnt).html($(gridCell).attr("value"));
$(gridCell).remove();
}
return{
initBindGridEvent : initBindGridEvent,
saveEdit : saveEdit
}
}();
源代碼下載:
EditableTable.rar
您可能感興趣的文章:
- jQuery實(shí)現(xiàn)可編輯表格并生成json結(jié)果(實(shí)例代碼)
- jQuery 實(shí)現(xiàn)雙擊編輯表格功能
- jQuery實(shí)現(xiàn)的可編輯表格完整實(shí)例
- 基于Bootstrap使用jQuery實(shí)現(xiàn)簡單可編輯表格
- BootStrap和jQuery相結(jié)合實(shí)現(xiàn)可編輯表格
- jQuery+PHP實(shí)現(xiàn)可編輯表格字段內(nèi)容并實(shí)時(shí)保存
- 用Jquery實(shí)現(xiàn)可編輯表格并用AJAX提交到服務(wù)器修改數(shù)據(jù)
- jQuery一步一步實(shí)現(xiàn)跨瀏覽器的可編輯表格,支持IE、Firefox、Safari、Chrome、Opera
- 基于jQuery實(shí)現(xiàn)可編輯的表格
相關(guān)文章
JQuery實(shí)現(xiàn)動(dòng)態(tài)表格點(diǎn)擊按鈕表格增加一行
動(dòng)態(tài)表格,功能為點(diǎn)擊添加按鈕,表格增加一行并給其name屬性賦予的值,點(diǎn)擊刪除,自動(dòng)刪除這一行,具體實(shí)現(xiàn)如下2014-08-08
jQuery實(shí)現(xiàn)的網(wǎng)格線繪制方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)的網(wǎng)格線繪制方法,涉及jQuery針對(duì)頁面元素的獲取及樣式動(dòng)態(tài)操作相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
通過jquery的ajax請(qǐng)求本地的json文件方法
今天小編就為大家分享一篇通過jquery的ajax請(qǐng)求本地的json文件方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
基于jQuery實(shí)現(xiàn)滾動(dòng)刷新效果
這篇文章主要為大家詳細(xì)介紹了基于jQuery實(shí)現(xiàn)滾動(dòng)刷新效果,使用Ajax獲取后臺(tái)數(shù)據(jù)更新前端頁面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
淺析jQuery的鏈?zhǔn)秸{(diào)用之each函數(shù)
如果對(duì)于jquery的$()包裝器函數(shù)還不是很清楚,請(qǐng)先參閱我的上一篇日志:淺析jQuery的鏈?zhǔn)秸{(diào)用2010-12-12

