Iはモデルインデックス値がわからない場合、どのようにインデックスから配列からアイテムを削除できますか?
function row() {
this.name = '';
this.rulingValue = '';
this.pathToImage = '';
this.rulingType = '';
this.dateStart = '';
this.dateEnd = '';
}
アレイvar jsonData = [];
Iが行TIこのテーブルを追加するテーブル
<table class="table" id="data-table">
<tbody>
</tbody>
</table>
ボタン有し有します。テーブルに行を追加すると、配列にも追加されます。テーブルに追加行のアレイに追加行の
methosが
が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);
}
方法
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);
}
すべてが正常に動作しかし、私は配列から現在の行を削除することはできません
$(this).closest('tr').remove(); // removed row from table
しかし、この行を配列から削除するにはどうすればよいですか?
EDIT
はい、私は、このソリューション
var array = [2, 5, 9];
var index = array.indexOf(5);
if (index > -1) {
array.splice(index, 1);
}
を参照してくださいしかし、どのように私はこれを行うことができますか?
var index = jsonData .indexOf(??????????????????????);
私は
var row = this.parentNode.parentNode;
var index = jsonData.indexOf(row)
alert(index);
を試してみましたが、私はあなたがfindIndex機能を使用することができ、あなたの条件に基づいたアイテムのインデックスを見つけるには、JSON
あなたのhtmlにjqueryライブラリが含まれていますか? jqueryタグがあなたの質問に欠けていたので、私はなぜ – prasanth
'array.splice(index、1)' – Rajesh
を見ていますか?[配列から要素を削除](http://stackoverflow.com/questions/5767325/)どのように特定の要素からjavascriptの配列を削除する –