gpt4 book ai didi

javascript - 循环遍历表并检查值

转载 作者:行者123 更新时间:2023-12-02 16:05:21 25 4
gpt4 key购买 nike

背景信息

我有两个表,一个是可用用户列表,称为“users”,另一个表称为“selected_users”。当单击用户表中的一行时,应用程序会从 selected_users 表中添加/删除记录

<table id=users>
<tr class="odd" role="row" id="row_89"><td>89</td><td>John Doe</td><td>23819</td><td>mm2@yahoo.com</td></tr>
<tr class="even" role="row" id="row_90"><td>90</td><td>John Doe</td><td>23819</td><td>36338</td></tr>
<tr class="odd" role="row" id="row_91"><td>91</td><td>Jane Doe</td><td>23820</td><td>jane@yahoo.com</td></tr>
<tr class="even" role="row" id="row_92"><td>92</td><td>Jane Doe</td><td>23820</td><td>28519</td></tr>
<tr class="odd" role="row" id="row_93"><td>93</td><td>Jim Bob</td><td>23801</td><td>jbob@yahoo.com</td></tr>
</table>

<table id=selected_users class="table table-condensed table-bordered" width="80%">
<tr class="info"><td colspan="4"><b>Selected Users:</b></td></tr>
</table>

问题

我需要更改现有逻辑,以便在选择可用用户列表中的一行时,可用用户表中具有匹配“pid”(即第三列)的所有其他行都应添加到selected_users 表。

这是当有人点击可用用户表时触发的代码:

    $('#users tbody').on('click', 'tr', function () {
var id = this.id;
var tr;
tr=$('<tr/>');

var index = $.inArray(id, selected);
if ( index === -1 ) {
selected.push( id ); //select/highlight in list of available users.

// Find td's inside this tr and add to selected_users table
var tds = $(this).find('td');
tr.append("<td>" + tds.eq(0).text() + "</td>");
tr.append("<td>" + tds.eq(1).text() + "</td>");
tr.append("<td>" + tds.eq(2).text() + "</td>");
tr.append("<td>" + tds.eq(3).text() + "</td>");
tr. attr('id', id.substring(4));
$('#selected_users').append(tr);

//loop through the avail users table and select all records with the same p number.
$("users td").each(function(i,o){
// new code goes here
}
} else {
selected.splice( index, 1 ); //deselect from list of avail users
//remove matching record from selected_users table.
var record_id = id.substring(4);
var rowtodelete = document.getElementById(record_id);
rowtodelete.parentNode.removeChild(rowtodelete);
}
$(this).toggleClass('selected');
} );
} );

到目前为止我所拥有的

我正在考虑在带有注释“新代码在这里”的部分中添加这样的代码(伪代码)

       //loop through the avail users table and select all records with the same pager number.
$("users tr").each(function(){
$(this).find('td ....
});

我不知道如何进行查找以查看第三列是否与 tds.eq(2).text() 中的内容匹配

如有任何建议,我们将不胜感激

编辑 1

这是我到目前为止所拥有的:

        //2015.06.11 - find and add all other rows with matching p number
var thisTR = $(this);
console.log(thisTR);
//select all siblings with same value in third column
thisTR.siblings().filter(function() {
console.log($('td',this).eq(2).text() );
//console.log($('td', thisTR).eq(2).text());
if ($('td',this).eq(2).text() == $('td', thisTR).eq(2).text() ) {
console.log("i found a match");
console.log("what is the row for: " + $('td',this).eq(2).text());
};
});

我只需要一种方法来识别找到匹配 td 的行,然后执行类似于我已经在向 selected_users 添加行的操作: var tr; tr=“”; ...找到该行然后... tr.append(""+ tds.eq(0).text() + ""); tr.append(""+ tds.eq(1).text() + ""); tr.append(""+ tds.eq(2).text() + ""); tr.append(""+ tds.eq(3).text() + ""); tr。 attr('id', id.substring(4)); $('#selected_users').append(tr);

最佳答案

这是您可以使用的 - .filter( function )

//save a reference of the current row
var thisTR = $(this);

//select all siblings with same value in third row
thisTR.siblings().filter(function() {
return $('td',this).eq(2).text() == $('td', thisTR).eq(2).text();
})

//Iterate through them and do what needs to be done.
.each(function() {
//do something
//here 'this' refers to tr (matched row)
//$('td', this) should give you all the tds in 'this' row
});

关于javascript - 循环遍历表并检查值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30807475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com