JavaScript Implementation table additions and deletions to the operation of the example detailed

Source: Internet
Author: User
Tags commit reset trim

In this paper, we illustrate the method of JavaScript implementation of table additions and deletions. Share to everyone for your reference. The implementation methods are as follows:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140-1 41 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170-171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201-2 02 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231-232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262-2 63 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298-299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329-3 30 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359-360 361 362 363 364 365 <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <html xmlns=" http://www.w3.org/1999/xhtml "> <head> <meta http-equiv=" Content-type "content=" text/html; Charset=utf-8 "/> <title>javascript form additions </title> <script type=" Text/javascript "> var _otable_ = Null var _otbody_ = null; var _arrselect_ = new Array; var _otempvalue_=new Object; _otempvalue_["$updateIndex"]=-1; var _theadname_=new Array ("name", "Sex", "age", "place of origin", "Delete No"); var cells_count=_theadname_.length-1; String.prototype.trim=function () {return this.replace (/(^s*) (s*$)/g, ');} window.onload = function () {var $oAdd = docu Ment.getelementbyid ("Btnadd"); $oAdd. onclick = function () {var _ocol1_ = document.getElementById ("Col1"); var _ocol2_ = document.getElementById ("Col2") ; var _ocol3_ = document.getElementById ("Col3"); var _ocol4_ = document.getElementById ("Col4"); if (!_otable_)//If no table exists, create new {_otable_ = Document.createElement ("table"); _otable_.setattribute ("Border", "1"); _otable_.setattribute ("width", "800px"); var _thead_=_otable_.createthead (); var _trow_=_thead_.insertrow (0); for (Var _headindex_=0;_headindex_<cells_count+1;_headindex_++) {var _tth=_trow_.insertcell (_headindex_); _ Tth.appendchild (document.createTextNode (_theadname_[_headindex_)); } _otbody_ = Document.createelement ("tbody"); _otable_.appendchild (_otbody_); document.getElementById ("Tabledata"). AppendChild (_otable_); } if (!_ocol1_.value.trim ()) {alert ("name must be filled out!") "); return;} Add a row of var _orow_ = _otbody_.insertrow (-1); Add 5 columns, four column values, one column select var _ocell1_ = _orow_.insertcell (-1); _ocell1_.appendchild (document.createTextNode (_ocol1_.value)); var _ocell2_ = _orow_.insertcell (-1); _ocell2_.appendchild (document.createTextNode (_ocol2_.value)); var _ocell3_ = _orow_.insertcell (-1); _ocell3_.appendchild (document.createTextNode (_ocol3_.value)); var _ocell4_ = _orow_.insertcell (-1); _ocell4_.appendchild (document.createTextNode (_ocol4_.value)); _oCol1_.value = ""; _ocol2_.value = ""; _ocol3_.value = ""; _ocol4_.value = ""; Select var _ocell5_ = _orow_.insertcell (4); _ocell5_.setattribute ("Style", "width:50px;"); var _ocheckbox_ = document.createelement ("input"); _ocheckbox_.setattribute ("Type", "checkbox"); _ocell5_.appendchild (_OCHECKBOX_); _ocheckbox_.onclick = function () {if (_ocheckbox_.checked) {var _rowindex_ = _ocheckbox_. Parentnode.parentnode.rowindex-1; _arrselect_.push (_ROWINDEX_); } _orow_.ondblclick = function () {var _opreupdateindex_=_otempvalue_["$updateIndex"]; var _opretemprow_=null; if (pars Eint (_opreupdateindex_)!=-1)//original selected row reset {if (!_otable_ | |!_otbody_) return; _opretemprow_= _otbody_.rows[parseint (_oPr EUPDATEINDEX_)]; var _cancelornot_=false; for (Var _cellindex_=0;_cellindex_<cells_count;_cellindex_++) {var $attributeNode _=_opretemprow_.cells[_ Cellindex_].firstchild; var $nodedata _=document.all $attributeNode _.getattribute ("value"): $attributeNode _.value; if ($nodedata _!=_otempvalue_["$" +_cellindex_]//Compared with the original value {_cancelornot_=confirm ("Your previous content has been modified to save changes?") "); Break } if (_cancelornot_) {//Avoid previous commit as null Var _firstnode_=_opretemprow_.cells[0].firstchild; var $firstnodedata _=_firstnode_. GetAttribute ("value"); if (! $firstnodedata _| |! $firstnodedata _.trim ()) {alert ("name cannot be empty, please edit it again!") "); _firstnode_.focus (); return;}; for (Var _cellindex_=0;_cellindex_<cells_count;_cellindex_++) {var _oldnode_=_opretemprow_.cells[_cellindex_]. FirstChild; var $nodedata _=document.all?_oldnode_.getattribute ("value"): _oldnode_.value; var _textnode_= document.createtextnode ($nodedata _); _opretemprow_.cells[_cellindex_].replacechild (_textnode_,_oldnode_); Delete _otempvalue_["$" +_cellindex_]; } else {for (var _cellindex_=0;_cellindex_<cells_count;_cellindex_++) {var _oldnode_=_opretemprow_.cells[_ Cellindex_].firstchild; var _textnode_= document.createtextnode (_otempvalue_["$" +_cellindex_)); _opretemprow_.cells[_cellindex_].replacechild (_textnode_,_oldnode_); Delete _otempvalue_["$" +_cellindex_]; }}} _otempvalue_["$updateIndex"] = this.rowindex-1; There is only one text node in the cell for (Var _cellindex_=0;_cellindex_<cells_count;_cellindex_++) {var _textbox_= Document.createelement ("input"); _textbox_.setattribute ("type", "text"); var _prenode_=this.cells[_cellindex_].firstchild; _otempvalue_["$" +_cellindex_]=_prenode_.nodevalue; Record the original value _textbox_.setattribute ("value", _prenode_.nodevalue); This.cells[_cellindex_].replacechild (_textbox_, _prenode_); } }; }; Delete var $oDelete = document.getElementById ("Btndelete"); $oDelete. onclick = function () {if (_arrselect_.length = 0) {alert ("You have not selected the row you want to delete."); return;}   if (_otable_ && Amp _otbody_) {var _confirmmsg_ = "Are you sure you want to remove the name is as follows: N"; for (var index = 0, Ilen = _arrselect_.length; index < Ilen; index++) { var _deletname_ = _otbody_.rows[parseint (_arrselect_[index])].cells[0].firstchild.nodevalue; _confirmmsg_ = _confirmmsg_.concat (_deletname_ + "n"); } _confirmmsg_ = _confirmmsg_.concat ("" Information?) "); if (!confirm (_confirmmsg_)) return;   for (var index =_arrselect_.length-1; Index >= 0; index--) {_otbody_.deleterow (parseint (_arrselect_[index]));} _arrselect_.splice (0,_arrselect_.length); Empty the select list}; Update: (Red section for updated code)//update var $oUpdate = document.getElementById ("Btnupdate"); $oUpdate. onclick = function () {var _opreupdateindex_=_otempvalue_["$updateIndex"] if (parseint (_opreupdateindex_) = =- 1 {alert ("You did not edit any update lines!"); return; if (_otable_ && _otbody_) {if (confirm) (Are you sure you want to modify it?) ) {var _temprow_= _otbody_.rows[parseint (_opreupdateindex_)]; var $namenode =_temprow_.cells[0].firstchild; var $namenodevalue =document.all $namenode. getattribute ("value"): $namenode. Value; if (! $namenodevalue | |! $namenodevalue. Trim ()) {alert ("name cannot be empty, please edit it again!") "); $namenode. focus (); return;} for (Var _cellindex_=0;_cellindex_<cells_count;_cellindex_++) {var $tmpnode _=_temprow_.cells[_cellindex_]. FirstChild; var $nodedata _=document.all $tmpnode _.getattribute ("value"): $tmpnode _.value; var _textnode_= document.createtextnode ($nodedata _); var _oldnode_=_temprOw_.cells[_cellindex_].firstchild; _temprow_.cells[_cellindex_].replacechild (_textnode_,_oldnode_); Delete _otempvalue_["$" +_cellindex_]; }} _otempvalue_["$updateIndex"] =-1}; Find var $oFind = document.getElementById ("Btnfind"); $oFind. Onclick=function () {if (!_otable_ &&!_otbody_) {alert ("No data available at this time!") "); return;} Before the judge has edited the uncommitted var _opreupdateindex_=_otempvalue_["$updateIndex"]; var _opretemprow_=null; if (parseint (_opreupdateindex_)!=-1)//original selected row reset {if (!_otable_ | |!_otbody_) return; _opretemprow_= _otbody_.rows[parsei NT (_OPREUPDATEINDEX_)]; var _cancelornot_=false; for (Var _cellindex_=0;_cellindex_<cells_count;_cellindex_++) {var $childNode _=_opretemprow_.cells[_cellindex_] . firstchild; var $nodedata _=document.all $childNode _.getattribute ("value"): $childNode _.value; if ($nodedata _!=_otempvalue_["$" +_cellindex_])//compared to the original value {_cancelornot_=confirm ("Your previous content has been modified to save changes?"). "); Break } if (_cancelornot_) {//Avoid previous commit as empty Var _firstnode_=_opretemprow_.cells[0].firstchild; var $firstnodedata _=document.all?_firstnode_.getattribute ("value"): _firstnode_.value; if (! $firstnodedata _| |! $firstnodedata _.trim ()) {alert ("name cannot be empty, please edit it again!") "); _firstnode_.focus (); return;}; for (Var _cellindex_=0;_cellindex_<cells_count;_cellindex_++) {var _oldnode_=_opretemprow_.cells[_cellindex_]. FirstChild; var $nodedata _=document.all?_oldnode_.getattribute ("value"): _oldnode_.value; var _textnode_= document.createtextnode ($nodedata _); _opretemprow_.cells[_cellindex_].replacechild (_textnode_,_oldnode_); Delete _otempvalue_["$" +_cellindex_]; } else {for (var _cellindex_=0;_cellindex_<cells_count;_cellindex_++) {var _oldnode_=_opretemprow_.cells[_ Cellindex_].firstchild; var _textnode_= document.createtextnode (_otempvalue_["$" +_cellindex_)); _opretemprow_.cells[_cellindex_].replacechild (_textnode_,_oldnode_); Delete _otempvalue_["$" +_cellindex_]; }}//Clear update list for (Var $obj _ in _otempvalue_) {Delete _otempvalue_[$obj _];}//_otempvalue_=new Object; _otempvalue_["$updateIndex"]=-1; Start querying var _$oselect_= document.getelementbyid ("Selectcol"); var _index_=_$oselect_.selectedindex; var _$oselectvalue_= _$oselect_.value; var _$oselecttext_= _$oselect_.options[_index_].text; var _$olike_=document.getelementbyid ("Col9"); var _$rowcollection_=_otbody_.rows; var _$rlen=_$rowcollection_.length; Switch (parseint (_$oselectvalue_)) {case 0:for (var _rindex=0;_rindex<_$rlen;_rindex++) {var _selectrow_=_$ Rowcollection_[_rindex]; var $pat = new RegExp (_$olike_.value.trim (), "I"); if (!_$olike_.value.trim ()) {_selectrow_.style.display=document.all? Block ":" Table-row ";} If the query box is empty, extract all ... Fuzzy search Else {if (! $pat. Test (_selectrow_.cells[0].firstchild.nodevalue.trim ())) {_selectrow_.style.display= ' None ';}}} Break Case 1:for (var _rindex=0;_rindex<_$rlen;_rindex++) {var _selectrow_=_$rowcollection_[_rindex]; var $pat = new RegExp (_$olike_.value.trim (), "I"); if (!_$olike_.value.trim ()) {_selectrow_.style.display=document.all? Block ":" Table-row ";} else {if (! $pAt.test (_selectrow_.cells[1].firstchild.nodevalue.trim ())) {_selectrow_.style.display= ' None ';}} } break; Case 2:for (var _rindex=0;_rindex<_$rlen;_rindex++) {var _selectrow_=_$rowcollection_[_rindex]; var $pat = new RegExp (_$olike_.value.trim (), "I"); if (!_$olike_.value.trim ()) {_selectrow_.style.display=document.all? Block ":" Table-row ";} else {if (! $pat. Test (_selectrow_.cells[2].firstchild.nodevalue.trim ())) {_selectrow_.style.display= ' None ';}}} Break Update (red section for newer) Case 3:for (var _rindex=0;_rindex<_$rlen;_rindex++) {var _selectrow_=_$rowcollection_[_rindex]; var $pat = new RegExp (_$olike_.value.trim (), "I"); if (!_$olike_.value.trim ()) {_selectrow_.style.display=document.all? Block ":" Table-row ";} else {if (! $pat. Test (_selectrow_.cells[3].firstchild.nodevalue.trim ())) {_selectrow_.style.display= ' None ';}}} Break } _arrselect_.splice (0,_arrselect_.length);//Clear Delete list var _checkboxlist_=document.getelementsbytagname ("input"); Resets the checkbox selection. for (Var _index=0,ilen=_checkboxlist_.length;_index<ilen;_index++) {if (_checkboxlist_[_index].type== "checkbox") {_checkboxlist_[_index].checked=false;}}}; var $oSelectAll = document.getElementById ("Btnselectall"); $oSelectAll. Onclick=function () {if (_otable_ && _otbody_) {var _$rowall_=_otbody_.rows; for (Var _rindex=0,_ rlen=_$rowall_.length;_rindex<_rlen;_rindex++) {var _selectrow_=_$rowall_[_rindex]; _selectrow_.style.display= Document.all? " Block ":" Table-row "; Delete from </head> <body> <fieldset> <legend> operation table, </script> </legend> < Fieldset> <legend> Add </legend> <label for= "Col1" > Name: </label> <input type= "text" id= " Col1 "/> <label for=" Col2 "> Gender: </label> <input type=" text "id=" Col2 "/> <label for=" Col3 "> Age: </label> <input type= "text" id= "Col3"/> <label for= "Col4" > Birthplace: </label> <input type= "text" ID = "Col4"/> <input type= "button" value= "Add" id= "Btnadd"/> </FIELDSET&GT <fieldset> <legend> Find </legend> <label for= "Col9" > Find content: </label> <script type= "text/ JavaScript "> var options = [" <option value= "0" selected> name </option> "," <option value= "1" > Sex </ Option> "," <option value= "2" > Age </option> "," <option value= "3" > Birthplace </option> "]; document.write ("<select name=" Selectcol "id=" Selectcol ">" + Options.join ("") + "</select>"); </script> <input type= "text" id= "Col9"/> <input type= "button" value= "Find" id= "Btnfind"/> </ fieldset> </fieldset> <br/> <fieldset id= "Tabledata" > <legend> tabular Data </legend> </ fieldset> <input type= "button" value= "delete" id= "Btndelete"/> "<input type=" button "value=" Update "id=" Btnupdate "/ > <input type= "button" value= "Show All" id= "Btnselectall"/> </body> </html>

The

wants this article to help you with your JavaScript programming.

Related Article

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.