Add, delete, modify, and set the color of the odd and even rows in the jquery table.

Source: Internet
Author: User

In the last week, I learned a little bit about HTML, CSS, javascript, and jquery using javascript to complete a simple table operation, which has the functions of adding, deleting, and modifying.

The table consists of three columns: Student ID, Student name, and age. Enter data in the name and age boxes, and click "Add" to Add data (the ID number is automatically generated progressively ), enter the ID number and enter the new name and age point "Edit" to modify it. If you Delete the ID number, click "Delete" to Delete the specified row.

At the top of the table, the number of rows in the current table is updated in a timely manner. When the ID is entered, the table dynamically responds and updates the content of the name age input box. The program achieves basic operability. The table header uses CSS for color control, and the content in the table uses CSS to set different colors based on parity. In this way, the interface becomes more beautiful.

Below is:


 

The complete code is as follows (Win7 + IE9 passed the test ):

// By MoreWindows (http://www.bkjia.com)
<Html>
<Head>
<Script src = "jquery-1.7.min.js"> </script>
<Script>
$ (Document). ready (function ()
{
SetTableRowColor ();
UpdataTableRowCount ();

If ($. browser. msie) // determines if it is MS's IE browser
{
$ ("# Id"). bind ("propertychange", function () {IDInputChange ();});
}
Else
{
Document. getElementById ("# id"). addEventListener ("input", IDInputChange, false );
}
});
</Script>
<Script>
// Obtain the corresponding content in the table based on the value in the ID input box and fill it in the name age input box
Function IDInputChange ()
{
// Find the specified row by id
Var I = SearchIdInTable ($ ("# Table tr"), $ ("# id"). val ());
If (I! =-1)
{
// Obtain the data of the row
Var name = $ ("# Table tr: eq (" + I + ") td: eq (1)" ).html ();
Var age = $ ("# Table tr: eq (" + I + ") td: eq (2)" ).html ();
 
// Update the data to the corresponding text box
$ ("# Name"). val (name );
$ ("# Age"). val (age );
}
Else
{
$ ("# Name"). val ("");
$ ("# Age"). val ("");
}
}
// Search for rows with the specified ID in the first column of the table
Function SearchIdInTable (tablerow, findid)
{
Var I;
Var tablerowtablerownum = tablerow. length;
For (I = 1; I <tablerownum; I ++)
If ($ ("# Table tr: eq (" + I + ") td: eq (0)" ).html () = findid)
Return I;
Return-1;
}
// Use CSS to control the color of the even row
Function SetTableRowColor ()
{
 
$ ("# Table tr: odd" ).css ("background-color", "# e6e6fa ");
$ ("# Table tr: even" ).css ("background-color", "# fff0fa ");
}
// Update the number of rows currently displayed in the table
Function UpdataTableRowCount ()
{
$ ("# TableRowCount" 2.16.html ($ ("# Table tr"). length-1 );
}
Function IncTableRowCount ()
{
Var tc = $ ("# tableRowCount ");
Tc.html(parseInt(tc.html () + 1 );
}
Function DecTableRowCount ()
{
Var tc = $ ("# tableRowCount ");
Tc.html(parseInt(tc.html ()-1 );
}
</Script>
<Script>
$ (Document). ready (function ()
{
// Add
$ ("# AddBtn"). click (function ()
{
Var id = parseInt ($ ("# Table tr: last td: first" ).html () + 1;
 
Var name = $ ("# Name"). val ()! = ""? $ ("# Name"). val ():"";
Var age = $ ("# Age"). val ()! = ""? $ ("# Age"). val ():"";

// Add a new row
Var appendstr = "<tr> ";
Appendstr + = "<td>" + id + "</td> ";
Appendstr + = "<td>" + name + "</td> ";
Appendstr + = "<td>" + age + "</td> ";
Appendstr + = "</tr> ";
$ ("# Table"). append (appendstr );

IncTableRowCount ();
SetTableRowColor ();
});
// Edit
$ ("# EditBtn"). click (function ()
{
// Find the specified row by id
Var I = SearchIdInTable ($ ("# Table tr"), $ ("# id"). val ());
If (I! =-1)
{
// Get new content
Var name = $ ("# Name"). val ()! = ""? $ ("# Name"). val ():"";
Var age = $ ("# Age"). val ()! = ""? $ ("# Age"). val ():"";

// Modify the two columns of the row
$ ("# Table tr: eq (" + I + ") td: eq (1)" pai.html (name );
$ ("# Table tr: eq (" + I + ") td: eq (2)" pai.html (age); // parseInt (age) can also
}
});
// Delete
$ ("# DeleteBtn"). click (function ()
{
// Find the specified row by id
Var I = SearchIdInTable ($ ("# Table tr"), $ ("# id"). val ());
If (I! =-1)
{
// Delete the row in the table
$ ("# Table tr: eq (" + I + ")"). slideUp ("slow ");
$ ("# Table tr: eq (" + I + ")"). remove ();

DecTableRowCount ();
SetTableRowColor ();
}
});
});
</Script>
</Head>
<Body>
<P> simple table operations include adding, deleting, and modifying tables. The id input box dynamically responds to the input. </p>
Id: <input type = "text" id = "id"/>
Name: <input type = "text" id = "Name"/>
Age: <input type = "text" id = "Age"/>
<Input type = "button" id = "AddBtn" value = "Add"/>
<Input type = "button" id = "EditBtn" value = "Edit"/>
<Input type = "button" id = "DeleteBtn" value = "Delete"/>
<Table id = "Table" align = "center" border = "2" cellpadding = "10" cellspacing = "1" bordercolor = "# FFAA00">
<Caption style = "font-size: 15px"> Student table <label id = "tableRowCount"> </label> </caption>
<Th> id </th> <th> Name </th> <th> Age </th>
<Tr> <td> 1 </td> <td> MoreWindows </td> <td> 24 </td> </tr>
<Tr> <td> 2 </td> <td> MW </td> <td> 19 </td> </tr>
</Table>
</Body>
</Html>
<! -- Css controls the background color of the header. css dual-tag-derived selector -->
<Style>
# Table th
{
Background-color: #7cfc00;
}
// By MoreWindows (http://blog.csdn.net/MoreWindows)
<Html>
<Head>
<Script src = "jquery-1.7.min.js"> </script>
<Script>
$ (Document). ready (function ()
{
SetTableRowColor ();
UpdataTableRowCount ();
 
If ($. browser. msie) // determines if it is MS's IE browser
{
$ ("# Id"). bind ("propertychange", function () {IDInputChange ();});
}
Else
{
Document. getElementById ("# id"). addEventListener ("input", IDInputChange, false );
}
});
</Script>
<Script>
// Obtain the corresponding content in the table based on the value in the ID input box and fill it in the name age input box
Function IDInputChange ()
{
// Find the specified row by id
Var I = SearchIdInTable ($ ("# Table tr"), $ ("# id"). val ());
If (I! =-1)
{
// Obtain the data of the row
Var name = $ ("# Table tr: eq (" + I + ") td: eq (1)" ).html ();
Var age = $ ("# Table tr: eq (" + I + ") td: eq (2)" ).html ();

// Update the data to the corresponding text box
$ ("# Name"). val (name );
$ ("# Age"). val (age );
}
Else
{
$ ("# Name"). val ("");
$ ("# Age"). val ("");
}
}
// Search for rows with the specified ID in the first column of the table
Function SearchIdInTable (tablerow, findid)
{
Var I;
Var tablerownum = tablerow. length;
For (I = 1; I <tablerownum; I ++)
If ($ ("# Table tr: eq (" + I + ") td: eq (0)" ).html () = findid)
Return I;
Return-1;
}
// Use CSS to control the color of the even row
Function SetTableRowColor ()
{

$ ("# Table tr: odd" ).css ("background-color", "# e6e6fa ");
$ ("# Table tr: even" ).css ("background-color", "# fff0fa ");
}
// Update the number of rows currently displayed in the table
Function UpdataTableRowCount ()
{
$ ("# TableRowCount" 2.16.html ($ ("# Table tr"). length-1 );
}
Function IncTableRowCount ()
{
Var tc = $ ("# tableRowCount ");
Tc.html(parseInt(tc.html () + 1 );
}
Function DecTableRowCount ()
{
Var tc = $ ("# tableRowCount ");
Tc.html(parseInt(tc.html ()-1 );
}
</Script>
<Script>
$ (Document). ready (function ()
{
// Add
$ ("# AddBtn"). click (function ()
{
Var id = parseInt ($ ("# Table tr: last td: first" ).html () + 1;

Var name = $ ("# Name"). val ()! = ""? $ ("# Name"). val ():"";
Var age = $ ("# Age"). val ()! = ""? $ ("# Age"). val ():"";
 
// Add a new row
Var appendstr = "<tr> ";
Appendstr + = "<td>" + id + "</td> ";
Appendstr + = "<td>" + name + "</td> ";
Appendstr + = "<td>" + age + "</td> ";
Appendstr + = "</tr> ";
$ ("# Table"). append (appendstr );
 
IncTableRowCount ();
SetTableRowColor ();
});
// Edit
$ ("# EditBtn"). click (function ()
{
// Find the specified row by id
Var I = SearchIdInTable ($ ("# Table tr"), $ ("# id"). val ());
If (I! =-1)
{
// Get new content
Var name = $ ("# Name"). val ()! = ""? $ ("# Name"). val ():"";
Var age = $ ("# Age"). val ()! = ""? $ ("# Age"). val ():"";

// Modify the two columns of the row
$ ("# Table tr: eq (" + I + ") td: eq (1)" pai.html (name );
$ ("# Table tr: eq (" + I + ") td: eq (2)" pai.html (age); // parseInt (age) can also
}
});
// Delete
$ ("# DeleteBtn"). click (function ()
{
// Find the specified row by id
Var I = SearchIdInTable ($ ("# Table tr"), $ ("# id"). val ());
If (I! =-1)
{
// Delete the row in the table
$ ("# Table tr: eq (" + I + ")"). slideUp ("slow ");
$ ("# Table tr: eq (" + I + ")"). remove ();

DecTableRowCount ();
SetTableRowColor ();
}
});
});
</Script>
</Head>
<Body>
<P> simple table operations include adding, deleting, and modifying tables. The id input box dynamically responds to the input. </p>
Id: <input type = "text" id = "id"/>
Name: <input type = "text" id = "Name"/>
Age: <input type = "text" id = "Age"/>
<Input type = "button" id = "AddBtn" value = "Add"/>
<Input type = "button" id = "EditBtn" value = "Edit"/>
<Input type = "button" id = "DeleteBtn" value = "Delete"/>
<Table id = "Table" align = "center" border = "2" cellpadding = "10" cellspacing = "1" bordercolor = "# FFAA00">
<Caption style = "font-size: 15px"> Student table <label id = "tableRowCount"> </label> </caption>
<Th> id </th> <th> Name </th> <th> Age </th>
<Tr> <td> 1 </td> <td> MoreWindows </td> <td> 24 </td> </tr>
<Tr> <td> 2 </td> <td> MW </td> <td> 19 </td> </tr>
</Table>
</Body>
</Html>
<! -- Css controls the background color of the header. css dual-tag-derived selector -->
<Style>
# Table th
{
Background-color: #7cfc00;
}
The jquery-1.7.min.js in the code can be downloaded on jquery's official website.

It's a good dish.

 

From MoreWindows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.