开发者

Javascript Table cell value reader doesn't get values from .net Repeater

I have JS function that read and summarize table column like that:

function calculateValue() {
        var sum = 0, index = 0;
        var table = document.getElementById("mytab1");
        for (var i = 1, row; row = table.rows[i]; i++) {
           开发者_StackOverflow社区 //alert(row.cells[4].firstChild.nodeValue);
            sum += row.cells[4].firstChild.nodeValue;
            index++;
        }
        //alert(sum / index + "_" + sum + "--" + index);
        return sum / index;
    }

This Table ("mytab1") is a dynamic table, a .net C# repeater build this table. when I try to alert the values, only the table header works, the other values are empty although there are values inside of them. Any Ideas why?

my page load function is:

 protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        load_profiles();
        ViewState["Time"] = "2";                   
        LoadGoals(2,0); /* this function put List<> in Repeater and creates a table   
                           with the values */          
    }
}

The js part that calls the function:

var def = {

percentage: calculateValue() || 0, scale: 100, limit: true, minimum: 0, maximum: 100, suffix: ' %', animate: true, digitalRoll: true, thisCss: { position: 'relative', width: '105px', height: '90px', padding: '0px', border: '0px', fontFamily: 'Arial', fontWeight: '250',

        },

When the function alert in the table search loop, i can see the values inside the table, and also the Column header is alerting with his value. Thanks.


I would suggest making sure that where you call your javascript:

var def = {
percentage: calculateValue() || 0, scale: 100, limit: true, minimum: 0, maximum: 100, suffix: ' %', animate: true, digitalRoll: true, thisCss: { position: 'relative', width: '105px', height: '90px', padding: '0px', border: '0px', fontFamily: 'Arial', fontWeight: '250',
    },

is either a) after the

<asp:Repeater></asp:Repeater>

control, or b) add a

$(document).ready(function() {
// Your code goes here
});

bit of JQuery to your page to make sure that the DOM has been fully loaded before you try to read the values from the table.

EDIT:

You may also want to use a JQuery selector to access and sum the values instead of using your loop.

var sum = 0;
$('#mytab1 .your-new-table-cell-class').each(function() {
    sum += Number($(this).val());
    });
});​​​​​​​​​
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜