如何使用jQuery根据某些条件获取HTML表的下一行
How to get next row of HTML table based on some condition using jQuery?
好吧,我是JQuery的新手,我需要根据行对表进行一些操作。
该表由属于3个不同样式类的行组成,Brand
具有category
,category
具有products
。
var table = $("table tbody");
table.find(".brand").each(function(i) {
var $tdsBrand = $(this).find("td"),
brand = $tdsBrand.eq(0).text(),
atyBrand = $tdsBrand.eq(1).text(),
alyBrand = $tdsBrand.eq(2).text();
console.log('Brand Row ' + (i + 1) + ':'nBrand Name: ' + brand + ''nActual TY: ' + atyBrand + ''nActual LY: ' + alyBrand);
var brandClass = $(this).attr("class");
console.log('brand class : ' + brandClass);
if (this row has next row as category) {
//if(brand.next($( "tr[class='category']" ))) {
//if ("(.band):has(.category)") {
//if ($(this).parents(".category").length == 1) {
table.find(".category").each(function(i) {
var catClass = $(this).attr("class");
console.log('category class : ' + catClass);
var $tdsCategory = $(this).find("td"),
category = $tdsCategory.eq(0).text(),
atyCategory = $tdsCategory.eq(1).text(),
alyCategory = $tdsCategory.eq(2).text();
console.log('Category Row ' + (i + 1) + ':'nCategory Name: ' + category + ''nActual TY: ' + atyCategory + ''nActual LY: ' + alyCategory);
if (This row has next row as product) {
//if(next($( "tr[class='product']" ))) {
//if ("(.category):has(.product)") {
//if ($(this).parents("product").length == 1) {
table.find(".product").each(function(i) {
var proClass = $(this).attr("class");
console.log('product class : ' + proClass);
var $tds = $(this).find("td"),
product = $tds.eq(0).text(),
aty = $tds.eq(1).text(),
aly = $tds.eq(2).text();
console.log('Product Row ' + (i + 1) + ':'nProduct Name: ' + product + ''nActual TY: ' + aty + ''nActual LY: ' + aly);
});
}
});
}
});
我想做的是,我必须总结产品的实际TY值,并将其显示在其类别中。然后将类别的实际TY(根据不同类别的产品计算得出)汇总到其品牌。
请参阅http://jsfiddle.net/cfhhz0zr/46/为了清楚地理解我的需求和代码,我已经尝试过了。
谢谢。
刚刚修改了一点代码,它似乎正在做你想要做的事情。另请参阅http://jsfiddle.net/88prg1dt/
我重构了一些变量,并重命名了一些变量以使其更有意义,所以现在应该相当清楚了。如果你想计算产品/类别的总数,现在应该非常简单。
这是JS代码:
var $table = $("table tbody");
$table.find(".brand").each(function (brandIndex) {
var $brandRow = $(this);
var $tdsBrand = $(this).find("td");
var brandName = $tdsBrand.eq(0).text();
var atyBrand = $tdsBrand.eq(1).text();
var alyBrand = $tdsBrand.eq(2).text();
console.log('Brand Row ' + (brandIndex + 1) + ':'nBrand Name: ' + brandName + ''nActual TY: ' + atyBrand + ''nActual LY: ' + alyBrand);
var $categoryRows = $brandRow.nextUntil('.brand').filter('.category');
$categoryRows.each(function (categoryIndex) {
var $categoryRow = $(this);
var $tdsCategory = $categoryRow.find("td");
var categoryName = $tdsCategory.eq(0).text();
var atyCategory = $tdsCategory.eq(1).text();
var alyCategory = $tdsCategory.eq(2).text();
console.log('Category Row: ' + (categoryIndex + 1) + ':'nCategory Name: ' + categoryName + ''nActual TY: ' + atyCategory + ''nActual LY: ' + alyCategory);
var $productRows = $categoryRow.nextUntil('.brand, .category').filter('.product');
$productRows.each(function (productIndex) {
var $productRow = $(this);
var $tdProducts = $productRow.find("td");
var productName = $tdProducts.eq(0).text();
var atyProduct = $tdProducts.eq(1).text();
var aly = $tdProducts.eq(2).text();
console.log('Product Row ' + (productIndex + 1) + ':'nProduct Name: ' + productName + ''nActual TY: ' + atyProduct + ''nActual LY: ' + aly);
});
});
});
我用jQuery nextUntil()
方法作为文档:
描述:获取每个元素的所有以下同级,最多可达但不超过包括选择器、DOM节点或jQuery匹配的元素对象已传递。
这是在回答你的问题吗?
相关文章:
- 从 html 表中选择一行(突出显示)并在单击按钮时发送值
- html画布鼠标后的一行
- 当上一行/当前行被填充时,将新行添加到HTML表中
- Html-从数据库中删除一行
- 用 HTML 中的一行连接 2 张图像.姜戈模板
- 如何将动态颜色应用于与 HTML 表不同的每一行
- 从同一行中获取最接近复选框列的列的 html 值
- 使用 JavaScript 编辑 HTML 中的每一行
- 阻止浮动 HTML 元素下降到下一行
- 如何使用jQuery和Ajax将文本文件中的每一行加载到HTML列表中
- JQuery HTML 避免使用每个表中的第一行
- html表的xsl生成为每一行提供一个按钮
- 如何在嵌入php的html表中为每一行添加复选框,并在数据库(Postgresql)中更新其值
- 在不影响第一行的情况下,每隔一行缩进一个表.html/css/js/jQuery
- 如何使用jQuery根据某些条件获取HTML表的下一行
- 阻止一行html出现在谷歌搜索中
- 如何将值获取到一行html数据表中
- 在一个页面被呈现后,任何方式都可以去掉一行HTML代码
- 禁用最后一行html表复选框
- 为什么我不能删除每一行html的空白?