error in display sum in footer [Resolved]

Posted by Klbaiju under jQuery on 3/21/2014 | Points: 10 | Views : 1009 | Status : [Member] | Replies : 5
Hi,

i want to display sum in footer

here is gridview

<asp:GridView ID="GridView1" runat="server" ShowFooter="true" >
<RowStyle CssClass="record" />
<FooterStyle CssClass="Footer" />
</asp:GridView>

here is code

var totalHr = 0;
var totalHr1 = 0;
$("#GridView1 tr td").filter(":not(td:first-child)").each(function () {
firstcelltext = $(this).parent().find("td:first-child").text();
var fcelltext = firstcelltext.replace(/ /g, '-');
var colindex = $(this).closest("tr td").prevAll("tr td").length;
var headertext = $("table[id*=GridView1] th").eq(colindex).text();

firstcelltext = $(this).parent().find("td:first-child").text();
var fcelltext = firstcelltext.replace(/ /g, '-');
var colindex = $(this).closest("tr td").prevAll("tr td").length;
var headertext = $("table[id*=GridView1] th").eq(colindex).text();

var cellText = $(this).text();
if ($.trim(cellText) == '') {
$(this).css('background-color', 'LightGreen');

}
else {
var idn = $(this).attr("id");
if (headertext == 2500) {
var name = $(this).text();


totalHr += parseFloat(name.replace(":", "."));
}
if (headertext == 2501) {
var name1 = $(this).text();
totalHr1 += parseFloat(name1.replace(":", "."));

}


}
if ($(this).hasClass("Footer")) {

$(this).empty();
$(this).append("<td colspan='2' align='right'> " + totalHr + "</td>");

}

});

iam getting value of totalHr

but it doesn't display in footer

how to solve this

Regards

Baiju




Responses

Posted by: A2H on: 3/22/2014 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Hi,
You need to slight modify the logic to show the respective details in each column like given below
if ($(this).closest('tr').hasClass("Footer")) {

$(this).empty();
//Check if the header text value is '2500' the show the total value of that column
if (headertext == 2500) {
$(this).append("<td colspan='2' align='right'>" + totalHr + "</td>");
}
//Check if the header text value is '2501' the show the total value of that column
if (headertext == 2501) {
$(this).append("<td colspan='2' align='right'>" + totalHr1 + "</td>");
}
}


Complete Code is given below

 var totalHr = 0;

var totalHr1 = 0;
$("#GridView1 tr td").filter(":not(td:first-child)").each(function () {
firstcelltext = $(this).parent().find("td:first-child").text();
var fcelltext = firstcelltext.replace(/ /g, '-');
var colindex = $(this).closest("tr td").prevAll("tr td").length;
var headertext = $("table[id*=GridView1] th").eq(colindex).text();
firstcelltext = $(this).parent().find("td:first-child").text();
var fcelltext = firstcelltext.replace(/ /g, '-');
var colindex = $(this).closest("tr td").prevAll("tr td").length;
var headertext = $("table[id*=GridView1] th").eq(colindex).text();
var cellText = $(this).text();
if ($.trim(cellText) == '') {
$(this).css('background-color', 'LightGreen');
}
else {
var idn = $(this).attr("id");
if (headertext == 2500) {
var name = $(this).text();
totalHr += parseFloat(name.replace(":", "."));
}
if (headertext == 2501) {
var name1 = $(this).text();
totalHr1 += parseFloat(name1.replace(":", "."));
}
}

//Here your check condition was always false and thats the reason its not showing data

//if ($(this).hasClass("Footer")) {
if ($(this).closest('tr').hasClass("Footer")) {
$(this).empty();
//Check if the header text value is '2500' the show the total value of that column
if (headertext == 2500) {
$(this).append("<td colspan='2' align='right'>" + totalHr + "</td>");
}
//Check if the header text value is '2501' the show the total value of that column
if (headertext == 2501) {
$(this).append("<td colspan='2' align='right'>" + totalHr1 + "</td>");
}
}
});


Please mark my reply as answer if it answer your question

Thanks,
A2H
My Blog

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 3/22/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,
Your check condition were wrongs and due to that it was always false and thats the reason in footer value was not getting displayed.
To Resolve the issue you need to change the below code from
if ($(this).hasClass("Footer")) { 

$(this).empty();
$(this).append("<td colspan='2' align='right'> " + totalHr + "</td>");
}

to
if ($(this).closest('tr').hasClass("Footer")) {

$(this).empty();
$(this).append("<td colspan='2' align='right'>" + totalHr + "</td>");
}


Your Modified Complete Code

var totalHr = 0;

var totalHr1 = 0;
$("#GridView1 tr td").filter(":not(td:first-child)").each(function () {
firstcelltext = $(this).parent().find("td:first-child").text();
var fcelltext = firstcelltext.replace(/ /g, '-');
var colindex = $(this).closest("tr td").prevAll("tr td").length;
var headertext = $("table[id*=GridView1] th").eq(colindex).text();

firstcelltext = $(this).parent().find("td:first-child").text();
var fcelltext = firstcelltext.replace(/ /g, '-');
var colindex = $(this).closest("tr td").prevAll("tr td").length;
var headertext = $("table[id*=GridView1] th").eq(colindex).text();

var cellText = $(this).text();
if ($.trim(cellText) == '') {
$(this).css('background-color', 'LightGreen');

}
else {
var idn = $(this).attr("id");
if (headertext == 2500) {
var name = $(this).text();


totalHr += parseFloat(name.replace(":", "."));
}
if (headertext == 2501) {
var name1 = $(this).text();
totalHr1 += parseFloat(name1.replace(":", "."));

}


}
//Here your check condition was always false and thats the reason its not showing data
//if ($(this).hasClass("Footer")) {
if ($(this).closest('tr').hasClass("Footer")) {
$(this).empty();
$(this).append("<td colspan='2' align='right'>" + totalHr + "</td>");
}

});


Please mark my reply as answer if it answer your question

Thanks,
A2H
My Blog

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 3/22/2014 [Member] Starter | Points: 25

Up
0
Down
Hi A2H,
your code displays result in every cell of footer.
I want to display like this
$(this).append("<td colspan='2' align='right'>" + totalHr + "</td>");
$(this).append("<td colspan='3' align='right'>" + totalHr1 + "</td>");

every cell displays both results.
i want to display one result in second cell and other in third cell.
how to correct this

Regards
Baiju

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 3/23/2014 [Member] Starter | Points: 25

Up
0
Down
Hi Mr A2H,
Thank you very much .it is working fine.

Once again thanks
Regards
K L BAIJU

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 3/23/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Glad to be of help.
Happy Coding.

Thanks,
A2H
My Blog

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response