0

I have a table where I can add text to it. every row is stored in an array.

1 row = 1 array

the table is a 2d array.

var table = [[1,2,3,4,5], [9,9,9,9,9]];

Look at this picture:

enter image description here

the first row is table[0]

[1,2,3,4,5]

and second is table1

[9,9,9,9,9]

Now I have a button that allow me to add 5 elements to the table (1 new row).

And this is added to the array table[] with a push. So if I add 5 things to my array. For example numbers 7,7,7,7,7. It looks like this:

table = [[1,2,3,4,5], [9,9,9,9,9], [7,7,7,7,7]];

Now, I have a delete button, that allows me to delete rows in the table.

This is my code. And it deletes the row my button is on:

var tableCell = document.createElement("td");           
            var newButton = tableCell.appendChild(document.createElement("button"));
            var buttonText = document.createTextNode("Remove");

        // THIS FUNCTION DELETES THE ROW
            newButton.onclick = function() { 



  //BEFORE I DELETE THE ROW, I WANT TO DELETE THE INDEX IN THE ARRAY TOO
//...........
                (this).closest('tr').remove();
            };

            newButton.appendChild(buttonText);
            ...

Now I wonder, how can I also delete the row from my array? I must some how find the row number I am on, and delete it from my array.

If i am on row number 2 in picture (9,9,9,9,9) which is index 1.

And I click "remove", i want it to delete index 1 in my array.

But how can I find what index to remove? I tried rowIndex but I don't know how to make it work with this...

table.splice(INDEX OF ROW,1)

how can I find what row my button I click on, is located on? And then use that to do something like:

table.splice(1,1)

this removes 1 array from index 1 in my 2d array

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
aliazik
  • 195
  • 1
  • 5
  • 13
  • what closest you use? it [this](https://developer.mozilla.org/en/docs/Web/API/Element/closest) or a [jQuery](https://api.jquery.com/closest/)? Can you provide how you create html table by your array? – Grundy May 08 '15 at 16:10
  • it's not jquery @Grundy I found it here http://stackoverflow.com/questions/11553768/remove-table-row-after-clicking-table-row-delete-button. My problem is that I just need to find what row my button is located on. If it's on rowIndex 2 in the table, then delete index 2 in my array as well – aliazik May 08 '15 at 16:12
  • 1
    but by link used jquery :-D – Grundy May 08 '15 at 16:16
  • how can I delete rows then? weird man..... because, I dont use jquery, and it delete my row lol – aliazik May 08 '15 at 16:17
  • but any ways how can I find what row my button that is being clicked is located on? is there no way to do this? :S – aliazik May 08 '15 at 16:18
  • sure, you have a few ways from saving index to button attribut, to looping by rows in table and checking what row you get – Grundy May 08 '15 at 16:20
  • can you provide how you create table by your array? :-) – Grundy May 08 '15 at 16:21

4 Answers4

0

When you create the button add an id of "myid[ROWNUMBER]". In the event listener you can then do something like

 var value = parseInt((thid.id).replace('myId',''));

Then you can splice the row from you array.

  • so when I add a button like: `var button = cell.appendChild(document.createElement("button"));` and then I type `button.id = row[i];` or what? and then the row is inside the row[i], like row[3]. but i dont understand where i add ur code. right before i delete the row? – aliazik May 08 '15 at 16:24
0

You actually seem to be using Element.closest and node.remove which aren't supported in IE.

To find a <tr>'s index, you can get it's sibling <tr>s and then look for it amongst them

Here is how you might do it using Array.prototype.filter and Array.prototype.indexOf

var tr = this.closest('tr'),
    idx = Array.prototype.filter.call(this.parentNode.children, function (e) {return e.tagName === 'TR'}).indexOf(this);

tr.remove();
table.splice(idx, 1);

Please be aware that this may not give the expected results if you need to count more than immediate siblings, e.g. you're using many <tbody>s

Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • 1
    I tried your code and it still is not remvoing from the array. When I press the remove button it removes the full row like before. But when I press my "Add Row" button and add a new row, the old one appears at the top again. It's like it is only temporarily removed. It's still in the array somewhere, because thats where I load the rows from – aliazik May 08 '15 at 16:28
0

If the button is direct child of the TD at the row, then in the buttons event handler something like this should work, if the performance is not issue...

  newButton.onclick = function() { 
    // ....
    var row = this.parentNode.parentNode, 
    rows = row.parentNode.querySelectorAll("tr"),
    index = 0;
    for( var r=0; r<rows.length; r++) {
        if(row==rows[r]) {
            index = r;
            break;
        }
    }
    console.log(index); // remove this index from array

Testing http://jsfiddle.net/11p3tt0n/

Tero Tolonen
  • 4,144
  • 4
  • 27
  • 32
  • still does not work for me. im about to go mental because of this. what am i doing wrong?`?? – aliazik May 08 '15 at 16:41
  • I updated the fiddle, check the console logs, it should be working... perhaps you should post more about the event binding code. http://jsfiddle.net/11p3tt0n/1/ – Tero Tolonen May 08 '15 at 16:45
0

Methink simplest solution is use rowIndex property like

function createButton() {
  var btn = document.createElement('button');
  btn.type = 'button';
  btn.appendChild(document.createTextNode('Remove'));
  btn.onclick = function() {
    var row = this.closest('tr');
    table.splice(row.rowIndex,1);
    row.remove();
    printTable(table);
  }
  return btn;
}

var table = [
  [1, 2, 3, 4, 5],
  [9, 9, 9, 9, 9]
];

var t = document.getElementById('table');
var b = document.getElementById('add');
b.onclick = function() {
  var num = parseInt(Math.random() * 10),
    row = [num, num, num, num, num];

  table.push(row);
  createRow(t, row);
  printTable(table);
};

function printTable(t) {
  var tjson = document.getElementById('tablejson');
  tjson.innerHTML = JSON.stringify(t);
}

printTable(table);

function createButton() {
  var btn = document.createElement('button');
  btn.type = 'button';
  btn.appendChild(document.createTextNode('Remove'));
  btn.onclick = function() {
    var row = this.closest('tr');
    table.splice(row.rowIndex,1);
    row.remove();
    printTable(table);
  }
  return btn;
}

function createRow(table, cells) {
  var row = table.insertRow();
  cells.forEach(function(el) {
    var cell = row.insertCell();
    cell.innerHTML = el;
  });
  var btnCell = row.insertCell();
  btnCell.appendChild(createButton());
}

table.forEach(function(row) {
  console.log(t, row);
  createRow(t, row);
});
<table id="table">
</table>
<button id='add'>Add</button>
<pre id="tablejson"></pre>
Grundy
  • 13,356
  • 3
  • 35
  • 55