简体   繁体   中英

Modify a totals row in a table for specific row using jQuery

I was able to create a totals row for a table without any issues.
But, there is a column (column number 7) of which I don't want to calculate the total, but instead show the value of a cell from the previous month to the current one.
Here it is an image (I had to use an image because this table this table is dynamically generated pulling data from a SharePoint list) of the table:

在此处输入图片说明

As you can see I have a Month column and a Pending column.
We are in June, so I would like the Pending column to show " 4 ".
This is what I've done so far:

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());
    }
});

I can't figure out how to read the values of each row in both columns at the same time.

You need to find the row containing the previous month's name. If you've found that you can get the value of the Pending column for that month and place it in the Totals row.

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);

See this FIDDLE for a working example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM