javascript - Dynamically delete multiple columns in html table -


I am trying to delete several columns from the html table using javascript. Argument is using it to search for the tag in the top row and then deletes that column.

The problem is that only one cell is going to be in the top row, then this column fixes, but if there are multiple columns it throws the error.

This is my code

  & lt ;! DOCTYPE html & gt; & Lt; Html & gt; & Lt; Body & gt; & Lt; Table style = "width: 100%" range = '1' id = 'just_for_california' & gt; & Lt; TR & gt; & Lt; TD & gt; & Lt; Period & gt; & Lt; / Span & gt; & Lt; / TD & gt; & Lt; TD & gt; S & lt; / TD & gt; & Lt; TD & gt; & Lt; Period & gt; & Lt; / Span & gt; & Lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; Eve & lt; / TD & gt; & Lt; TD & gt; Jackson & lt; / TD & gt; & Lt; TD & gt; 94 & lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; John & lt; / TD & gt; & Lt; TD & gt; Do & lt; / TD & gt; & Lt; TD & gt; 80 & lt; / TD & gt; & Lt; / TR & gt; & Lt; / Table & gt; & Lt; / Body & gt; & Lt; Script & gt; Var datatable_legueval = document.getAllimateBiID ('bus_firlacalphini'). Rows [0] .cells.length; Var count_rows = document.getElementById ('just_for_california'). Rows.line; Var column_aray = []; (Var i = 0; i & lt; datatable_language; I ++) {var str = document.getElementById ("just_for_california") rows [0]. Seals [i]. WinnerHTML; If (str.search (" ") = -1) {column_array.push (i); }} Var len = column_array.length; (Var i = count_rows-1; i> = 0; i--) {rows_number = document.getElementById ('just_for_california'). Rows [i]; Console.log ("ROW_NUMBER:" + i); {Rows_number.deleteCell (column_array [j]) for (var j = 0; j & lt; len; j ++); }} & Lt; / Script & gt; & Lt; / Html & gt;  

This is because you calculate the index incorrectly when you type cells To remove. I revised the code (it is clarifying) and now it starts working:

  var table = document.getElementById ('just_for_california'), line = table.rows ; (Var i = 0; i & lt; rows [0] .cells.length; i ++) {var str = rows [0]. Sales [i]. WinnerHTML; If (str.search (" ") = -1) {for (var j = 0; j & lt; rows.length; j ++) {lines [J] .deleteCell (i); }}}  

The problem is that if you are trying to extract cells "horizontally" in the row, then say that you indexed 1 and 3 and the table has 4 columns. When you delete the first cell 1 then it works fine but then you move to the right and try to remove the cell at index 3 . This fails because you have already removed the cell 1 , no index is 3 no longer the maximum index is now 2 is. Therefore error

In my correction code, I am removing the "vertical" column, so there can be no such issue.

Demo:


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -