Home Web Front-end JS Tutorial Self-written dynamic table example compatible with FF and IE_javascript skills

Self-written dynamic table example compatible with FF and IE_javascript skills

May 16, 2016 pm 05:19 PM

The HTML table structure is as follows:

Copy code The code is as follows:

class="tableStyle1" cellspacing="0">

< ;th style="width: 40px">Serial number












< td>readonly='true' />title='Product Code' />name='Dy_hd_rowState' type='hidden' value='1' title='The status of the row' />type='hidden' value='0' title='Part Specifications' />








Part model Part name Quantity Price without tax Price with tax Tax amount< ;/th>
Payment Full amount Actions
1 style='width: 35px' type='text' /> < ;/td>


The js code is as follows :
Copy code The code is as follows:

function addEvent (o,c,h){
if(o.attachEvent){
o.attachEvent('on' c,h);
}else{
o.addEventListener(c,h,false);
}
return true;
}
var selectRow;//Page-level js variable, used to store the selected row, so the pop-up window Assign a value to this row in
function addnode(){
var table=document.getElementById("Dy_table");
var tr=table.rows[1].cloneNode(true);
for (var i=1;ifor(var p=0;pif(tr.childNodes[i].getElementsByTagName("input")[p].name=="Dy_hd_rowState")//Special treatment for row status
tr.childNodes[i].getElementsByTagName("input" )[p].value="1";
else
tr.childNodes[i].getElementsByTagName("input")[p].value="";
}
}
var rowCount = table.rows[0].cells[0].getElementsByTagName("input")[1].value;//The number of rows visible to the user
tr.firstChild.innerHTML=parseInt(rowCount) 1;
table.rows[0].cells[0].getElementsByTagName("input")[1].value = parseInt(rowCount) 1;//Number of visible rows 1
table.rows[0].cells [0].getElementsByTagName("input")[0].value = table.rows.length;//Total number of rows, including hidden
var tbody=table.getElementsByTagName("tbody");
if( tbody!=null){
tbody[0].appendChild(tr);
}else
table.appendChild(tr);
}
//Event when deleted
function delnode(){
var table=document.getElementById("Dy_table");
var rowCount = table.rows[0].cells[0].getElementsByTagName("input")[1].value; //The number of rows visible to the user
var row; //Get the last visible row
for( var i=table.rows.length-1; i>=0 ;i--){
if(table.rows[i].style.display!="none")
{
row=table.rows[i];
break;
}
}
var rowId=row.cells[1].getElementsByTagName("input")[2].value;
if( rowCount > 1 ){
if(rowId=="")//New row When not written to the database, delete directly
{
var tbody=table.getElementsByTagName("tbody");
if(tbody!=null){
tbody[0].removeChild(row) ;
}else
table.removeChild(row);
table.rows[0].cells[0].getElementsByTagName("input")[1].value = parseInt(rowCount) - 1;
}
else//If you need to delete it from the database, set the deletion mark
{
row.style.display="none";
row.cells[1].getElementsByTagName(" input")[3].value = "2";
table.rows[0].cells[0].getElementsByTagName("input")[1].value = parseInt(rowCount)-1;
}
}else{
if(rowId == ""){//When the new row is not written to the database, clear
row.cells[1].getElementsByTagName("input")[0 ].value="";
row.cells[1].getElementsByTagName("input")[1].value="";
row.cells[1].getElementsByTagName("input")[2 ].value="";
row.cells[1].getElementsByTagName("input")[3].value="";
row.cells[1].getElementsByTagName("input")[4 ].value="";
row.cells[2].getElementsByTagName("input")[0].value="";
row.cells[3].getElementsByTagName("input")[0 ].value="1";
row.cells[4].getElementsByTagName("input")[0].value="";
row.cells[5].getElementsByTagName("input")[ 0].value="";
row.cells[6].getElementsByTagName("input")[0].value="";
row.cells[7].getElementsByTagName("input")[ 0].value="";
row.cells[8].getElementsByTagName("input")[0].value="";
}else{//If you need to delete it from the database, delete it Tag
row.style.display="none";
row.cells[1].getElementsByTagName("input")[3].value = "2";
table.rows[0]. cells[0].getElementsByTagName("input")[1].value = parseInt(rowCount) - 1;
addnode();
}
}
setClf();
}
//Events when deleting
function delnode1(o){
var tr=o.parentNode.parentNode;
var table=document.getElementById("Dy_table");
var rowCount = table.rows[0].cells[0].getElementsByTagName("input")[1].value;//The number of rows visible to the user
var rowId=tr.cells[1].getElementsByTagName("input" )[2].value;
if( rowCount > 1 ){
if(rowId=="")//When the newly added row is not written to the database, delete it directly
{
var tbody=table.getElementsByTagName("tbody");
if(tbody!=null){
tbody[0].removeChild(tr);
}else
table.removeChild(tr) ;
table.rows[0].cells[0].getElementsByTagName("input")[1].value = parseInt(rowCount) - 1;
}
else
{
tr.style.display="none";
tr.cells[1].getElementsByTagName("input")[3].value = "2";
table.rows[0].cells[0] .getElementsByTagName("input")[1].value = parseInt(rowCount) - 1;
}
}else{
if(rowId==""){//When the new row is not written to the database, clear it directly
tr.cells[1].getElementsByTagName("input")[0].value= "";
tr.cells[1].getElementsByTagName("input")[1].value="";
tr.cells[1].getElementsByTagName("input")[2].value= "";
tr.cells[1].getElementsByTagName("input")[3].value="";
tr.cells[1].getElementsByTagName("input")[4].value= "";
tr.cells[2].getElementsByTagName("input")[0].value="";
tr.cells[3].getElementsByTagName("input")[0].value= "1";
tr.cells[4].getElementsByTagName("input")[0].value="";
tr.cells[5].getElementsByTagName("input")[0].value ="";
tr.cells[6].getElementsByTagName("input")[0].value="";
tr.cells[7].getElementsByTagName("input")[0].value ="";
tr.cells[8].getElementsByTagName("input")[0].value="";
}else{//If you need to delete it from the database, set a delete mark
tr.style.display="none";
tr.cells[1].getElementsByTagName("input")[3].value = "2";
table.rows[0].cells[0] .getElementsByTagName("input")[1].value = parseInt(rowCount) - 1;
addnode();
}
}
//The following loop is used to update the table when deleting from the middle Row number
for( var i= 1,p = 1; i < table.rows.length ;i ){
if(table.rows[i].style.display!="none")
{
table.rows[i].cells[0].innerHTML = p;
p;
}
}
setClf();
}
/ / Events that occur when modifying, change row status
function textChange(o){
var tr=o.parentElement.parentElement;
if(o.parentElement.parentElement.parentElement==null)return;/ /If it is a newly added row, return
var rowState = tr.cells[1].getElementsByTagName("input")[3].value;
if( rowState == "1")
return ;
else
tr.cells[1].getElementsByTagName("input")[3].value = "3";
setClf();
}
//Verification before submission data, ensuring there are no duplicate rows
function checkSameData(){
var table=document.getElementById("Dy_table");
for( var i= 1; i < table.rows.length ;i ){
if(table.rows[i].style.display == "none"||table.rows[i].cells[1].getElementsByTagName("input")[1].value==" ") continue;
for( var p= i 1; p < table.rows.length ;p ){
if(table.rows[p].style.display == "none") continue;
if(table.rows[i].cells[1].getElementsByTagName("input")[1].value.replace(/s $/g,"") ==
table.rows[p ].cells[1].getElementsByTagName("input")[1].value.replace(/s $/g,""))
{alert("There are duplicate items in the parts section and cannot be saved! ");return false;}
}
}
return true;
}
var dialogWin;//Whether the parts window is open
//Select parts
function selectLj( o){
if(dialogWin == null){
selectRow = o.parentNode.parentNode;//Assign the row to the global variable
var cpxh = selectRow.cells[1].getElementsByTagName("input ")[0].value;
dialogWin = winOpen('selectLj.aspx?ljh=' cpxh);
// window.open("../jddgl/Select_lj.aspx?ljh=" cpxh, window,
// "center:yes;dialogWidth:600px;dialogHeight:400px;help:no;status:no;");
}
}
function winOpen(url){
return window.open(url,'selectLj','resizable=1,status=0,menubar=0,scrollbars=1,height=400px,width=600px');
}

/ /Calculate the cost in the table
function setClf(){}

This is an improvement on the previously written dynamically adding tables. The previous one was really just after I learned js. This is a failed work. Now it is finally compatible with FF and IE. During the process of compatibility, I did not miss the standard DOM specification and improved a lot of knowledge. When using JS DOM, you should pay attention to it when reading the MS DHTML manual. Are the methods and attributes mentioned in it standard? It is best to use standard ones.

This dynamic table can be dynamically increased or decreased as long as the table is set in HTML. You don’t need to care about how many tds it has. Pay attention to this. The related input hidden is added to the second td. Dynamic addition and deletion is only a superficial function. This table has a row status like the dataset, and the row status can be used to update, delete and add data on the server side. 1 New addition. , 2 deleted, 3 modified. Just using the pop-up window does not work in FF and IE7. I don’t know how it works with iframe.

Under IE, the cloned tr cannot be used to obtain the td collection, FF. The following is possible. Since there is input in tr and the onpropertychange event is used, it will also be triggered when the newly added input value in tr is removed, so an if is used in this event to eliminate this situation. Browser compatibility is really a bit troublesome. There is still a problem under FF. When the page is never submitted, when FF reloads the page, the value of the server-side control will be saved, but under IE, it is really reloaded, and any value on the page will be saved. Will not be retained. FF's behavior of saving server control values ​​should be a problem with its asp.net support. This should not happen without submitting the page.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1230
24
Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript Engines: Comparing Implementations JavaScript Engines: Comparing Implementations Apr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript: Exploring the Versatility of a Web Language JavaScript: Exploring the Versatility of a Web Language Apr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration) How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration) Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

From C/C   to JavaScript: How It All Works From C/C to JavaScript: How It All Works Apr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration) Building a Multi-Tenant SaaS Application with Next.js (Backend Integration) Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

See all articles