Jquery 在 2 列中找到具有特定值的 table 行数

Jquery find number of table rows with specific values in 2 columns

我有一个 table,其中包含 2 列 "id"(唯一值)和 "name"。使用 jQuery 我想 return ID 与 id 数组(例如 SR1、B1、B2)匹配的行数(.length)和 "name" 字段中的值这些行是空的。

到目前为止我有:

var empty = $('[tabulator-field="name"]:empty').length;
var sr1 = $('.tabulator-row:nth-of-type(29) [tabulator-field="name"]:empty').length;
var sr2 = $('.tabulator-row:nth-of-type(30) [tabulator-field="name"]:empty').length;
var b1 = $('.tabulator-row:nth-of-type(7) [tabulator-field="name"]:empty').length;
var b2 = $('.tabulator-row:nth-of-type(8) [tabulator-field="name"]:empty').length;
var c10 = $('.tabulator-row:nth-of-type(27) [tabulator-field="name"]:empty').length;
var c11 = $('.tabulator-row:nth-of-type(28) [tabulator-field="name"]:empty').length;
var emptySR = sr1 + sr2 + b1 + b2 + c10 + c11;
var emptyBeds = empty - emptySR;

但是,这似乎是一种低效的计算方法,此外,使用 :nth-of- 根据它们在 table 中的位置(而不是它们的 ID 值)选择我感兴趣的行如果 table 按不同的字段排序,则键入 return 的结果不正确,因为行的顺序将发生变化。

谁能推荐一个强大的功能来实现我的需要?

使用选择器匹配空字段,然后使用过滤函数测试ID是否在数组中。

var bedIds = ['SR1', 'SR2', 'B1', 'B2', 'C10', 'C11'];
var emptyBeds = $('.tabulator-row:has([tabulator-field="name"]:empty) [tabulator-field="id"]').filter(function() {
    return bedIds.indexOf(this.textContent) != -1;
}).length;