jQueryでテーブル行を削除する最良の方法は何ですか?
-
05-07-2019 - |
質問
jQueryでテーブル行を削除する最良の方法は何ですか?
解決
その通りです:
$('#myTableRow').remove();
次のような行にid
がある場合、これは正常に機能します。
<tr id="myTableRow"><td>blah</td></tr>
<=>がない場合は、jQueryの多数のセレクターを使用できます。 。
他のヒント
$('#myTable tr').click(function(){
$(this).remove();
return false;
});
テーブルのデータセル内にボタン/リンクがあると仮定すると、次のようなものがうまくいきます...
$(".delete").live('click', function(event) {
$(this).parent().parent().remove();
});
これにより、クリックされたボタン/リンクの親の親が削除されます。これは通常の DOM オブジェクトではなく jQuery オブジェクトであるため、parent() を使用する必要があります。また、ボタンはデータ セル内に存在し、データ セルは行内に存在するため、parent() を 2 回使用する必要があります。削除したいもの。$(this) はクリックされたボタンなので、次のようにするだけでボタンのみが削除されます。
$(this).remove();
これによりデータ セルが削除されます。
$(this).parent().remove();
行の任意の場所をクリックして削除したい場合は、次のような方法が機能します。これを簡単に変更して、ユーザーにプロンプトを表示したり、ダブルクリックのみで動作したりすることができます。
$(".delete").live('click', function(event) {
$(this).parent().remove();
});
お役に立てば幸いです...私自身、これについては少し苦労しました。
次を使用できます:
$($(this).closest("tr"))
要素の親テーブル行を見つけるため。
parent()。parent()よりもエレガントです。これは私が始めたもので、すぐに私のやり方の誤りを学びました。
-編集- 誰かが問題は行を削除することだと指摘しました...
$($(this).closest("tr")).remove()
下で指摘したように、あなたは簡単にできます:
$(this).closest('tr').remove();
同様のコードスニペットは、複数の要素でイベントを発生させるなど、多くの操作に使用できます。
簡単..これを試してください
$("table td img.delete").click(function () {
$(this).parent().parent().parent().fadeTo(400, 0, function () {
$(this).remove();
});
return false;
});
以下は受け入れられます:
$('#myTableRow').remove();
function removeRow(row) {
$(row).remove();
}
<tr onmousedown="removeRow(this)"><td>Foo</td></tr>
たぶん、このようなものでも動作するでしょうか? <!> quot; this <!> quot;で何かを試していないので、動作するかどうかわかりません。
する必要があるのは、テーブルからテーブル行(<tr>
)タグを削除することだけです。たとえば、テーブルから最後の行を削除するコードは次のとおりです。
$('#myTable tr:last').remove();
*上記のコードは、から取得したものです。 このjQuery Howtoの投稿 。
サイズについてはこれを試してください
$(this).parents('tr').first().remove();
完全なリスト:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.deleteRowButton').click(DeleteRow);
});
function DeleteRow()
{
$(this).parents('tr').first().remove();
}
</script>
</head>
<body>
<table>
<tr><td>foo</td>
<td><a class="deleteRowButton">delete row</a></td></tr>
<tr><td>bar bar</td>
<td><a class="deleteRowButton">delete row</a></td></tr>
<tr><td>bazmati</td>
<td><a class="deleteRowButton">delete row</a></td></tr>
</table>
</body>
</html>
別のempty()
によるもの:
$(this).closest('tr').empty();
削除する行が変更される可能性がある場合は、これを使用できます。削除したい行を#この関数に渡すだけです。
function removeMyRow(docRowCount){
$('table tr').eq(docRowCount).remove();
}
$('tr').click(function()
{
$(this).remove();
});
私は上記のコードを試してみますが、うまくいくと思いますが、なぜそれがいつか機能し、テーブル全体が削除される理由がわかりません。行をクリックして行を削除しようとしています。正確な答えが見つかりませんでした。
このようなHTMLがある場合
<tr>
<td><span class="spanUser" userid="123"></span></td>
<td><span class="spanUser" userid="123"></span></td>
</tr>
userid="123"
は、テーブルを作成するときに動的に入力できるカスタム属性です。
次のようなものを使用できます
$(".spanUser").live("click", function () {
var span = $(this);
var userid = $(this).attr('userid');
var currentURL = window.location.protocol + '//' + window.location.host;
var url = currentURL + "/Account/DeleteUser/" + userid;
$.post(url, function (data) {
if (data) {
var tdTAG = span.parent(); // GET PARENT OF SPAN TAG
var trTAG = tdTAG.parent(); // GET PARENT OF TD TAG
trTAG.remove(); // DELETE TR TAG == DELETE AN ENTIRE TABLE ROW
} else {
alert('Sorry, there is some error.');
}
});
});
その場合、TR
タグのクラスまたはIDはわかりませんが、とにかく削除できます。
これは古い投稿であることに感謝していますが、同じことをしようとしていて、受け入れられた答えがうまくいかなかったことがわかりました。これが書かれてからJQueryが進んだと仮定します。
とにかく、次のことがうまくいったことがわかりました:
$('#resultstbl tr[id=nameoftr]').remove();
これが誰にも役立つかどうかはわかりません。上記の私の例は、より大きな関数の一部であったため、イベントリスナーにラップしませんでした。
ブートストラップテーブルを使用している場合
このコードスニペットをbootstrap_table.jsに追加
BootstrapTable.prototype.removeRow = function (params) {
if (!params.hasOwnProperty('index')) {
return;
}
var len = this.options.data.length;
if ((params.index > len) || (params.index < 0)){
return;
}
this.options.data.splice(params.index, 1);
if (len === this.options.data.length) {
return;
}
this.initSearch();
this.initPagination();
this.initBody(true);
};
次に、var allowedMethods = [
追加'removeRow'
最後に、$("#your-table").bootstrapTable('removeRow',{index:1});
idは現在、適切なセレクタではありません。行にいくつかのプロパティを定義できます。セレクターとして使用できます。
<tr category="petshop" type="fish"><td>little fish</td></tr>
<tr category="petshop" type="dog"><td>little dog</td></tr>
<tr category="toys" type="lego"><td>lego starwars</td></tr>
そしてfuncを使用して、次のような行を選択できます(ES6):
const rowRemover = (category,type)=>{
$(`tr[category=${category}][type=${type}]`).remove();
}
rowRemover('petshop','fish');