Implement editable tables using jQuery

  • 2020-03-30 03:02:49
  • OfStack

Today you learned how to implement an editable table using jQuery as an example. The requirements for this example are as follows: click the cell in the foreground table to modify the contents, press enter to save the modified contents, and esc to undo the saved contents. Principle: when you click the cell of the client table, add a text box in the cell, and assign the original content in the cell to the text box, and then further modify the text box content, modify the text box content re-assign value to the cell.


Foreground code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %> 
<!DOCTYPE html 
<html xmlns=""> 
<head runat="server"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
<title>jq2 -- tables that can be edited </title> 
<link href="css/editTable.css" rel="stylesheet" /> 
<script type="text/javascript" src="js/jquery.js"></script> 
<script type="text/javascript" src="js/editTable.js"></script> 
<%--<script type="text/javascript" src="js/jquery-1.9.1.js"></script>--%> 
<form id="form1" runat="server"> 
<th colspan="2"> The mouse clicks the table item to be able to edit </th> 
<th> Student id </th> 
<th> The name </th> 
<td> Zhang SAN </td> 
<td> Li si </td> 
<td> Cathy </td> 
<td> Zhao six </td> 

The CSS code:
body { 
table { 
border:1px solid #000000; 
table td { 
border:1px solid #000000; 
table th { 
border:1px solid #000000; 
tbody th { 

Jquery code
$(function () { 
//Find all even rows in the table except the first tr
//Use even to return all tr elements through tbody tr
$("tbody tr:even").css("background-color", "#ece9d8"); 
//Find all the student number cells
var numId = $("tbody td:even"); 

//Register mouse click events for cells () { 
//Find the td corresponding to the current mouse click. This corresponds to the td responding to the click
var tdObj = $(this); 
//Determines whether there is a text box in td
if (tdObj.children("input").length>0) { 
return false; 
//Gets the contents of the table
var text = tdObj.html(); 
//Clear out the content in td
//Create text box
//Remove the border of the text box
//Set the font size in the text box to be the same as the text in the table.
//Sets the background color of the text box to be the same as the background color of the table
//The width of the text field is the same as the width of td
//And put the td median into the text box
//Inserts a text box into td
var inputObj = $("<input type='text'>").css("border-width", "0").css("font-size", tdObj.css("font-size")).css("background-color", tdObj.css("background-color")).width(tdObj.width()).val(text).appendTo(tdObj); 
//Text box inserted after the first focus, then selected
//The click event cannot be triggered after the text box is inserted () { 
return false; 
//Handles carriage return and esc buttons on the text box
inputObj.keyup(function (event) { 
//Gets the key value of the currently pressed keyboard
var keycode = event.which; 
//Handle the return case
if (keycode==13) { 
//Gets the contents of the current text box
var inputtext = $(this).val(); 
//Change the content in td to the content of the text box
//Handle the contents of esc
if (keycode==27) { 
//Restore the content in td to its original content

Summary: the following knowledge points can be obtained through the study of this example:


1. The table can contain thead and tbody

2. The contents of the header can be put in th

3. Table {} this type of writing is called a label selector, which can affect the entire table.

4. Table td{} this way represents all td contained in the table.

Second, jquery

You can put four different arguments in the bracket of $()

1. Parameter directly put function to indicate that the page has been loaded: for example, $(function(){} in the first line of jquery code in the above example.

2. The parameter can be a CSS class selector and wrapped as a jquery object. For example: line 4 of jquery code $(" tbody tr:even ") in the example above

3. If the parameters are HTML text, you can create a dom node and wrap it as a jquery object. For example: line 27 $("< Input type = 'text' >" )

4. The parameter can be a dom object, which is equivalent to replacing a dom object with a jquery object. Line 11 of jquery code in the above example var tdObj = $(this)

The jquery object in this example

1. Add CSS properties after the jquery object, you can set the CSS properties of the node. For example, line 4 $("tbody tr:even"). CSS (" backback-color ", "#ece9d8");

2. The content of the jquery object contains the dom nodes corresponding to the selectors, which are saved in an array.

3. Jquery object can be followed by HTML method to set or get the HTML content of the node. For example, line 17 in the jquery code above, var text = tdobj.html ()

4. The val method is added after the jquery object to get or set the value value of the node. For example, var inputtext = $(this).val()

5. Add the width method after the jquery object to set or get the width of a node. For example, tdobj. width() in line 27 of the jquery code in the above example

6. ApppendTo method can append a node to all children of another node. For example, appendTo(tdObj) at line 27 in the jquery code

7. The trigger method can be added after the jquery object to trigger a certain js event. For example, line 29 in the jquery code inputobj. trigger("focus"). Trigger ("select")

8. Children can be obtained by adding children method after jquery object, and parameters can be set to limit the content of children nodes. For example, line 13 in the jquery code above, tdobj.children ("input").length

9. If the jquery object returned by the selector contains more than one dom node, register a click event on that object, and all dom nodes will be used for this event. For example, in line 9 of the jquery code in the example above;

Related articles: