To implement the editable table function, we need to solve the following problems:
1. Clarify which columns in the table the data to be modified are (how to find these cells);
2. How to make the cells editable Edited;
3. How to handle some key events of cells;
4. Solve cross-browser issues.
We can solve the above problems step by step through jQuery.
1. Draw a table
First we draw a table. " -//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Click on the table to edit | |
---|---|
Student number | Name | 000001 | Zhang San |
| 李思 |
000003 | 王五 |
000004 | Zhao Liu td> |
editTable05.jpg
What is the cause of the above bug? Because after a cell is inserted into the text box, the text box belongs to the cell. When we click on the text box, the click event of the cell will also be triggered.
We need to prevent the click behavior of the text box (prevent event bubbling).
Code6:
return false;
});
Code7:
//Find all cells in the student number column
//Because the position of the cells in the student number column in all td is an even number (0, 2, 4, 6), so Through even, you can filter to even-numbered cells in td
var numTd = $("tbody td:even");
//When you click these td, create a text box
numTd.click( function(){
//Create a text box object
var inputobj = $("");
//Get the currently clicked cell object
var tdobj = $(this);
//Get the text in the cell
var text = tdobj.html();
//If there is a text box in the current cell, jump out of the method directly
//Note: Be sure to make a judgment before inserting the text box
if(tdobj.children("input").length>0){
return false;
}
//Clear Cell text
tdobj.html("");
inputobj.css("border","0")
.css("font-size",tdobj.css("font-size "))
.css("font-family",tdobj.css("font-family"))
.css("background-color",tdobj.css("background-color"))
.css("color","#C75F3E")
.width(tdobj.width())
.val(text)
.appendTo(tdobj);
inputobj.get( 0).select();
//Prevent the click event of the text box
inputobj.click(function(){
return false;
});
});
});
Code 8:
Code9:
Now that the above problems have been solved, let's add some key events to the text box. We know that the keyCode for obtaining keystrokes is different in different browsers, but jQuery helps us solve this problem.
You only need to add the event parameter to the function of the event, and then in the method body, you can obtain the keyCode through the which attribute of the event object. The event.which attribute assimilates the methods of obtaining keyCode in different browsers.
After obtaining the keyCode, I mainly do two key events: ESC key (key value: 27) and Enter key (key value: 13).
Code10:
//Handle the operations of the enter and esc keys on the text box
//The function of an event method in jQuery can define an event parameter, and jQuery will block the differences between browsers and pass it to us Available event objects
inputobj.keyup(function(event){
//Get the key value of the current key
//There is a which attribute on the jQuery event object to get the key value of the keyboard key
var keycode = event.which;
//Handle carriage return
if(keycode==13){
//Get the content of the current text box
var inputtext = $(this ).val();
//Modify the content of td to the content in the text box
tdobj.html(inputtext);
}
//Handle the situation of esc
if( keycode == 27){
//Restore the content in td to text
tdobj.html(text);
}
});
The following is Complete js code:
Code11:
$( document).ready(function(){
//Find all the cells in the column of student number
//Because the position of the cells in the column of student number in all td is an even number (0,2,4 ,6), so you can filter to the even-numbered cells in td through even
var numTd = $("tbody td:even");
//Create a text box when you click these tds
numTd.click(function(){
//Create a text box object
var inputobj = $("");
//Get the currently clicked unit Cell object
var tdobj = $(this);
//Get the text in the cell
var text = tdobj.html();
//If there is a text box in the current cell, Just jump out of the method
//Note: Be sure to make a judgment before inserting the text box
if(tdobj.children("input").length>0){
return false;
}
//Clear the text of the cell
tdobj.html("");
inputobj.css("border","0")
.css("font-size",tdobj.css ("font-size"))
.css("font-family",tdobj.css("font-family"))
.css("background-color",tdobj.css("background- color"))
.css("color","#C75F3E")
.width(tdobj.width())
.val(text)
.appendTo(tdobj);
inputobj.get(0).select();
//Prevent the click event of the text box
inputobj.click(function(){
return false;
});
/ /Handle the operation of the enter and esc keys on the text box
//The function of an event method in jQuery can define an event parameter. jQuery will mask the differences between browsers and pass us a usable event object
inputobj.keyup(function(event){
//Get the key value of the current key
//There is a which attribute on the jQuery event object to get the key value of the keyboard key
var keycode = event. which;
//Handle carriage return
if(keycode==13){
//Get the content of the current text box
var inputtext = $(this).val();
//Modify the content of td to the content in the text box
tdobj.html(inputtext);
}
//Handle the situation of esc
if(keycode == 27){
//Restore the content in td to text
tdobj.html(text);
}
});
});
});
Related document package download