The same table? Different tables (editable state table)_jquery
A new day has begun, and life continues. What I want to share with you today is a different table. An ordinary table is used to display data. The table I want to share today can not only display data, but also edit data. When the mouse clicks on the data, the corresponding data grid becomes editable. Without further ado, let’s get into today’s topic. First complete the HTML page:
< ;table>
< ;/tr>
Yes, it is still an ordinary table with no style at all. In order to make this table less abstract, let’s introduce CSS styles to it.
height: 150px;
}
table, table td, table th{
border:1px solid black;
border-collapse: collapse;
}
table td{
width:50%;
height: 25px;
}
thead th{
background-color:#87CEFA;
}
tbody th{
background-color:# FFFACD;
}
There are only a few editable page elements in the HTML page. Unfortunately, table is not one of them. In order to make the table editable, you need to add table Insert editable page elements into it, and then decorate it with CSS to make it look like an ordinary table, but with editable functions. This is what JS wants to accomplish. The JS code is as follows:
$( "#content tr:odd").css("background-color","#D2B48C");
$("#content tr:even").css("background-color","#C0C0C0") ;
$("#content td").click(function(){
var clickObj = $(this);
content = clickObj.html();
changeToEdit(clickObj);
});
function changeToEdit(node){
node.html("");
var inputObj = $("");
inputObj.css("border","0").css("background-color",node.css("background-color"))
.css("font-size",node.css(" font-size")).css("height","20px")
.css("width",node.css("width")).val(content).appendTo(node)
. get(0).select();
inputObj.click(function(){
return false;
}).keyup(function(event){
var keyvalue = event.which;
if(keyvalue==13){
node.html(node.children("input").val());
}
if(keyvalue==27){
node .html(content);
}
}).blur(function(){
if(node.children("input").val()!=content){
if(confirm ("Save the modified content? ","Yes","No")){
node.html(node.children("input").val());
}else{
node.html(content);
}
}else{
node.html(content);
}
});
}
});
Next, let’s do a simple analysis of this JS. The global variable var content is used to save the content in the table before editing. Sometimes the user edits the table but does not want to save the edited result, so he needs to save the table. The contents in are restored to before editing, so when the mouse is clicked, the contents of the table must be saved first.
The following two sentences $("#content tr:odd").css("background-color","#D2B48C"); $("#content tr:even").css("background -color","#C0C0C0"); is to make the table have alternate row colors, just to increase the visibility of the table. var inputObj = $(""); This sentence generates an editable JQuery object, which is to insert editable elements in the table. The following .css() methods It is to add CSS style to the inputObj object. The .css() method can not only set the CSS style for an object but also obtain the CSS style of an object. JQuery provides many such methods. Many times the JQuery object is returned after the JQuery method is executed, so inputObj.css().css().css().... appears. The
appendTo() method realizes the editability of the table (appendix() can also be used) and inserts editable elements into the table. The two methods .get(0).select() are used to select the content in inputObj so that the focus falls on the editable element. It should be noted that these two methods must be written after appendTo(), inputObj.click( The function(){}) method is also essential. Deleting this method will cause an interesting bug. You can give it a try.
The following keyup(function(event){}) can be used to obtain the key value corresponding to the key pressed on the keyboard through event.which. Commonly used key values include Enter key: 13, Esc key :27. When the user presses the Enter key, the edited content is saved and the table is restored to a normal table. When the user presses the Esc key, the contents of the table are restored and the table is restored to a normal table.
User experience, the emergence of Apple has made this word more popular, and we are here to join in the fun. In order to improve the user experience, the blur(function(){}) method is added here. When the focus leaves the editable element, first determine whether the content in the table has been changed. If there is no change, directly restore the table and the contents in the table. If there is a change, Prompts the user whether to save.
Today’s example is basically completed. If you reference the JS code in a separate JS file, a Chinese garbled bug may appear. You might as well give it a try. Thank you for your patience in reading this article, I hope it will be helpful to you.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

With the continuous development of front-end technology, data tables have become one of the important tools for enterprise management and data display. In daily development, sometimes it is necessary to modify or add data in the data table. At this time, it is necessary to implement an editable data table. This article will introduce how to use Vue to implement editable data tables. 1. Implementation ideas When implementing the editable data table function, we need to consider the following points: Data presentation: Render the data into the table for display and editing. Table editing: Edit data in the table.

How to use Layui to develop a personal schedule management system that supports editability. In recent years, with the rapid development of information technology and the accelerated pace of people's lives, personal schedule management has become more and more important. In order to allow people to better manage their time and tasks, we can use Layui, a front-end UI framework based on JavaScript. It provides rich components and a concise style, and is very suitable for developing personal schedule management systems. 1. Environment preparation First, we need to prepare the development environment. make sure you

Basic table Before developing the table component, first think about what style of API to use. Because the author uses element in production work, the styles of the previous components are similar to element, but this time I do not plan to use the element style. , I plan to change it and display it directly: We expect users to use it like this: constdataList=[{id:1,name:'"JavaEE Enterprise Application Practice"',author:'dev1ce',price:'10.22',desc:&# 3

Vue.js is one of the most popular front-end frameworks at present. It uses a data-driven approach to build user interfaces and has the characteristics of two-way data binding and componentization. In the Vue.js documentation, a method for implementing an editable table is provided. This article will introduce the specific implementation steps of this method. To prepare data, you must first prepare a set of data. Here we take student information as an example. The data format can be an array, and each element contains attributes such as name, gender, age, etc. students:[{name:'Xiao Ming

How to use Vue and Canvas to develop editable vector graphics applications Introduction: In recent years, vector graphics have become more and more widely used in the design field, and there are many design tools based on vector graphics such as Adobe Illustrator. In web development, we also hope to be able to develop editable vector graphics applications to meet users' customized needs for design. This article will introduce how to use Vue and Canvas to develop editable vector graphics applications, and provide detailed code examples. Preparations First, I

Overview of how to use Layui to develop an editable e-book reader: Layui is an easy-to-use and powerful front-end framework that provides a rich set of UI components and development tools, allowing developers to quickly build beautiful and fully functional web application. This article will introduce how to use the Layui framework to develop an e-book reader that supports editability, allowing users to perform operations such as highlighting, marking, and taking notes in the e-book. Step 1: Project preparation First, we need to prepare a basic project structure. Create a

How to use Layui to implement editable table functions Layui is a classic and concise front-end UI framework with rich components and powerful functions. During the development process using Layui, we may encounter the need to implement editable table functions. This article will introduce how to use Layui's table component and form component to implement editable table functions, and provide specific code examples. 1. Introduce the Layui library. First, introduce the relevant files of the Layui library into the project. can choose

How to use Layui to develop an editable flowchart designer Introduction: With the rapid development of informatization, flowcharts are increasingly used in various industries. However, the selection of flowchart editors currently on the market is limited, and most require payment. This article will introduce how to use the Layui framework to develop an editable flowchart designer and provide specific code examples. 1. Introduction to Layui: Layui is a simple and easy-to-use front-end framework that provides a wealth of components and interfaces to quickly build We
