使用jQuery修改表中特定行的总计行

时间:2014-06-17 14:27:27

标签: javascript jquery html

我能够为一张桌子创建一个没有任何问题的总计行 但是,有一列(第7列)我不想计算总数,而是显示从上个月到当前单位的单元格值。
这是一个图像(我必须使用一个图像,因为这个表是动态生成的,从SharePoint列表中提取数据):

enter image description here

如您所见,我有列和待定列。
我们是六月份,所以我希望Pending专栏显示“ 4 ” 这就是我到目前为止所做的:

JS:

var theDate = new Date();
var prevMonth = theDate.getMonth() - 1;
prevMonth = prevMonth === -1 ? 12 : prevMonth;
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
rows = [];
$('#myTable tr td:nth-child(7)').each(function ()
{
    var rowText = $(this).text();
    rows.push(rowText);
    var n = month[prevMonth];

    if (rowText !== n)
    {
        $('#myTable tr:last td:nth-child(7)').text($(this).text());
    }
});

我无法弄清楚如何同时读取两列中每行的值。

1 个答案:

答案 0 :(得分:1)

您需要找到包含上个月名称的行。如果您发现可以获得该月的待定列的值,并将其放在总计行中。

<强> JS

var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var today = new Date();
var thisMonth = today.getMonth(); 
var prevMonth = thisMonth == 0 ? 11 : thisMonth - 1;
var month = months[prevMonth];

var val = $("#myTable tr:contains('" + month + "') td:nth-of-type(7)").text();
$("#myTable tr:last-of-type td:nth-of-type(7)").text(val);

有关工作示例,请参阅此 FIDDLE