Javascript “的总数”;是”;数据表中的行

Javascript “的总数”;是”;数据表中的行,javascript,jquery,datatables,css-selectors,totals,Javascript,Jquery,Datatables,Css Selectors,Totals,我有一些表格有“可计费”行,数据填写为y或n(是或否)。我知道如何添加列的总价,但不知道如何计算包含“y”的行。例如,在下面,我将查找第一行,获取长度并通知总数 HTML <table id='pricetable'> <tr> <th>Price</th> <th>Billable</th> </tr> <tr> <

我有一些表格有“可计费”行,数据填写为y或n(是或否)。我知道如何添加列的总价,但不知道如何计算包含“y”的行。例如,在下面,我将查找第一行,获取长度并通知总数

HTML

<table id='pricetable'>
    <tr>
        <th>Price</th>
        <th>Billable</th>
    </tr>    
    <tr>
        <td>201</td>
        <td>y</td>
    </tr>       
    <tr>
        <td>51</td>
        <td>y</td>
    </tr> 
    <tr>
        <td>7</td>
        <td>n</td>
    </tr>      
</table>

…但问题是,这需要与Datatables保持一致。如果您更改行数,您会注意到我有6行,4行可计费,但它只提醒3行。无论如何,价格总计对我来说很好,但我需要知道如何合计可计费行。提前谢谢!更新:

我会将您的循环更改为迭代所有TDs,如下所示:

$('#pricetable td').each(function (index) {
    var item = $(this).text();
    if (index % 2 == 0)
        priceTotal += parseFloat(item);
    else if (item == "y")
        billableCount++;
});

这是一个有效的

我会将您的循环更改为迭代所有TDs,如下所示:

$('#pricetable td').each(function (index) {
    var item = $(this).text();
    if (index % 2 == 0)
        priceTotal += parseFloat(item);
    else if (item == "y")
        billableCount++;
});

这是一个有效的

我会将您的循环更改为迭代所有TDs,如下所示:

$('#pricetable td').each(function (index) {
    var item = $(this).text();
    if (index % 2 == 0)
        priceTotal += parseFloat(item);
    else if (item == "y")
        billableCount++;
});

这是一个有效的

我会将您的循环更改为迭代所有TDs,如下所示:

$('#pricetable td').each(function (index) {
    var item = $(this).text();
    if (index % 2 == 0)
        priceTotal += parseFloat(item);
    else if (item == "y")
        billableCount++;
});
这是一个可行的

这应该适合你

$(function () {
 var count = 0;
 $("#pricetable").find("td").each(function () {
    var val = $(this).text();
    if (val == 'y') count++;
 });
 alert(count);
});
这应该对你有用

$(function () {
 var count = 0;
 $("#pricetable").find("td").each(function () {
    var val = $(this).text();
    if (val == 'y') count++;
 });
 alert(count);
});
这应该对你有用

$(function () {
 var count = 0;
 $("#pricetable").find("td").each(function () {
    var val = $(this).text();
    if (val == 'y') count++;
 });
 alert(count);
});
这应该对你有用

$(function () {
 var count = 0;
 $("#pricetable").find("td").each(function () {
    var val = $(this).text();
    if (val == 'y') count++;
 });
 alert(count);
});

要添加所有可计费行,可以使用.DataTable().rows().eq(0)并应用如下筛选器

$(document).ready(function() {
    var table=$('#pricetable').DataTable({//use DataTable instead to use .rows()
        "sPaginationType": "full_numbers", 
        "iDisplayLength": 5
    });   
    var priceTotal = 0;
    table.rows().eq(0).filter( function (row) {//each row inside the .DataTable
        if(table.cell(row,1).data() === 'y'){//if it is billable
           priceTotal+=parseInt(table.cell(row,0).data());//parse the data to int and sum
        }
    });  
    alert("price "+priceTotal);
});    
您需要使用.DataTable()返回DataTables API实例,以便可以使用.rows()访问每一行,并循环仅添加可计费行

要添加所有可计费行,请使用.DataTable().rows().eq(0)并应用如下筛选器

$(document).ready(function() {
    var table=$('#pricetable').DataTable({//use DataTable instead to use .rows()
        "sPaginationType": "full_numbers", 
        "iDisplayLength": 5
    });   
    var priceTotal = 0;
    table.rows().eq(0).filter( function (row) {//each row inside the .DataTable
        if(table.cell(row,1).data() === 'y'){//if it is billable
           priceTotal+=parseInt(table.cell(row,0).data());//parse the data to int and sum
        }
    });  
    alert("price "+priceTotal);
});    
您需要使用.DataTable()返回DataTables API实例,以便可以使用.rows()访问每一行,并循环仅添加可计费行

要添加所有可计费行,请使用.DataTable().rows().eq(0)并应用如下筛选器

$(document).ready(function() {
    var table=$('#pricetable').DataTable({//use DataTable instead to use .rows()
        "sPaginationType": "full_numbers", 
        "iDisplayLength": 5
    });   
    var priceTotal = 0;
    table.rows().eq(0).filter( function (row) {//each row inside the .DataTable
        if(table.cell(row,1).data() === 'y'){//if it is billable
           priceTotal+=parseInt(table.cell(row,0).data());//parse the data to int and sum
        }
    });  
    alert("price "+priceTotal);
});    
您需要使用.DataTable()返回DataTables API实例,以便可以使用.rows()访问每一行,并循环仅添加可计费行

要添加所有可计费行,请使用.DataTable().rows().eq(0)并应用如下筛选器

$(document).ready(function() {
    var table=$('#pricetable').DataTable({//use DataTable instead to use .rows()
        "sPaginationType": "full_numbers", 
        "iDisplayLength": 5
    });   
    var priceTotal = 0;
    table.rows().eq(0).filter( function (row) {//each row inside the .DataTable
        if(table.cell(row,1).data() === 'y'){//if it is billable
           priceTotal+=parseInt(table.cell(row,0).data());//parse the data to int and sum
        }
    });  
    alert("price "+priceTotal);
});    
您需要使用.DataTable()返回DataTables API实例,以便可以使用.rows()访问每一行,并循环仅添加可计费行



Dave,问题是数据表不会因为任何原因在小提琴中启动。此代码在我端显示为“0”。另外,我有12个列,许多列都有y/n,所以我坚持使用第n个子选择器来指定列。$(“#pricetable td:nth child(2)”)。每个(函数(){var item=$(this).text();if(item==“y”)billableCount++});上面的代码可能更接近我所需要的,但它确实显示为零。Dave,问题是数据表不是出于任何原因在小提琴中启动的。此代码在我端显示为“0”。另外,我有12个列,许多列都有y/n,所以我坚持使用第n个子选择器来指定列。$(“#pricetable td:nth child(2)”)。每个(函数(){var item=$(this).text();if(item==“y”)billableCount++});上面的代码可能更接近我所需要的,但它确实显示为零。Dave,问题是数据表不是出于任何原因在小提琴中启动的。此代码在我端显示为“0”。另外,我有12个列,许多列都有y/n,所以我坚持使用第n个子选择器来指定列。$(“#pricetable td:nth child(2)”)。每个(函数(){var item=$(this).text();if(item==“y”)billableCount++});上面的代码可能更接近我所需要的,但它确实显示为零。Dave,问题是数据表不是出于任何原因在小提琴中启动的。此代码在我端显示为“0”。另外,我有12个列,许多列都有y/n,所以我坚持使用第n个子选择器来指定列。$(“#pricetable td:nth child(2)”)。每个(函数(){var item=$(this).text();if(item==“y”)billableCount++});上面的代码可能更接近我所需要的,但它确实显示为零。问题是Datatables并不是出于任何原因在fiddle中启动的。这段代码在我这边显示为“0”。@triplethreat77如果您有另一个问题,代码对我来说很好。因为您没有启动Datatables。问题是Datatables在fiddle中由于任何原因都没有启动。这段代码在我这边显示为“0”。@triplethreat77如果您有另一个问题,代码对我来说很好。因为您没有启动Datatables。问题是Datatables在fiddle中由于任何原因都没有启动。这段代码在我这边显示为“0”。@triplethreat77如果您有另一个问题,代码对我来说很好。因为您没有启动Datatables。问题是Datatables在fiddle中由于任何原因都没有启动。这段代码在我这边显示为“0”。@triplethreat77如果您有另一个问题,代码对我来说就可以了。因为您没有启动Datatables。非常接近。我只需要提醒可计费行的数量。这里是4,但我可能有500。更新:要获得可计费行数,只需在filter函数中添加myBillable+,这在fiddle中起作用,但无论我在项目中做了什么,我都会在表中得到“Undefined is not a function”。rows().eq(0)。filter(function)(row)行。有什么想法吗?可能是我使用的datatables版本,你放在fiddle 1.10.2上的第一个版本使用datatables 1.9.4,jQuery 1.10.2非常接近。我只需要提醒可计费行的数量。这里是4,但我可能有500。更新:要获得可计费行的数量,只需在筛选函数中添加myBillable++,这在t中工作他摆弄着,但不管我在我的项目上做什么,我都会在表上看到“未定义的不是函数”。rows().eq(0)。filter(函数(行)行。有什么想法吗?可能是我使用的datatables版本,你放在小提琴上的第一个版本1.10.2使用datatables 1.9.4,jQuery 1.10.2非常接近。我只需要提醒可计费行的数量。这里是4