I have model
function row() {
this.name = '';
this.rulingValue = '';
this.pathToImage = '';
this.rulingType = '';
this.dateStart = '';
this.dateEnd = '';
}
and array var jsonData = [];
I have table
<table class="table" id="data-table">
<tbody>
</tbody>
</table>
and button to add row ti this table. When I add row to table I add it to array too.
methos for add row to array
function add() {
var item = new row();
item.name = document.getElementById("ruling-name").value;
item.pathToImage = document.getElementById("path-to-image").value;
item.rulingValue = document.getElementById("ruling-value").value;
item.rulingType = document.getElementById("ruling-type").value;
item.dateStart = document.getElementById("dtp_input1").value;
item.dateEnd = document.getElementById("dtp_input2").value;
jsonData.push(item);
ddRow(item);
}
method for add row to table
function ddRow(item) {
const newRow = tbody.insertRow(tbody.rows.length);
newRow.insertCell(0).appendChild(document.createTextNode(item.name));
newRow.insertCell(1).appendChild(document.createTextNode(item.pathToImage));
newRow.insertCell(2).appendChild(document.createTextNode(item.rulingValue));
newRow.insertCell(3).appendChild(document.createTextNode(item.rulingType));
newRow.insertCell(4).appendChild(document.createTextNode(item.dateStart));
newRow.insertCell(5).appendChild(document.createTextNode(item.dateEnd));
var btn = document.createElement("BUTTON");
btn.setAttribute('type', 'button');
btn.onclick = function () {
$(this).closest('tr').remove();
};
var t = document.createTextNode("delete");
btn.appendChild(t);
newRow.insertCell(6).appendChild(btn);
}
All work fine But I can not remove current row from array
$(this).closest('tr').remove(); // removed row from table
But how can I removed this row from array?
EDIT
Yes I see this solution
var array = [2, 5, 9];
var index = array.indexOf(5);
if (index > -1) {
array.splice(index, 1);
}
But How can I make this?
var index = jsonData .indexOf(??????????????????????);
I tried
var row = this.parentNode.parentNode;
var index = jsonData.indexOf(row)
alert(index);
and I have "udefined
"