0

This function pretend to read from a csv and populate a table of input field . Having rows and columns I need to nest loops or call function in a loop, or even call function by itself . I tried already all the combinations . In the best case it populates only the first row ( A1,B1,C1, etc.. ) , it knows exactly the starting point in buffer and everything apparently is correct in the numbers it makes , BUT the outer loop is broken after the first iteration (whatever I do with my code about inner function, or inner loop, or nested function ) . I tried "continue", "return", the closure stuff.

function getPage(p) {
    var init = buffer.indexOf("Page:" + p + ",");
    var init_index = init + 1;
    var linea = "";
    var r = 1;
    for (var x in buffer) {
        if (x > init_index) {
            linea = buffer[x].split(",");
            read_line(linea, r);
            r++;
        }
    }
}

function read_line(l, r) {
    var debug = new Array();
    var n = 0;
    while (n < l.length + 1) {
        var colons = ["A" + r, "B" + r, "C" + r, "D" + r, "E" + r, "F" + r, "G" + r, "H" + r, "se_ckbox" + r, "me_ckbox" + r, "sim_ckbox" + r, "I" + r, "J" + r, "K" + r, "L" + r, "M" + r, "N" + r, "O" + r, "P" + r, "Q" + r, "R" + r, "S" + r, "T" + r, "U" + r, "V" + r];
        var field = l[n + 1];
        debug.push("col:" + colons[n].toString() + "\nVAL:" + field + "\n");
        document.getElementById(colons[n].toString()).value = field;
        document.getElementById("debug").value = debug.toString();
        n++;
    }
    return;
}
j08691
  • 204,283
  • 31
  • 260
  • 272
qmarty
  • 1
  • Can you please provide some test data? Or even create a Fiddle? – bernland May 20 '15 at 13:41
  • A fiddle would be great. I currently have no idea what you're asking – Tom May 20 '15 at 13:42
  • Have you tried giving the length a direct value of let's say 5, or make a dummy string array value and test out a fake buffer2 e.g "buffer2[x]" That way you will know where the problem lies or where it doesn't. – frontsideup May 20 '15 at 13:55
  • Does the array linea always has more than 1 element? You take the second element: var field = l[n + 1]; Also, I would recommend explicitly converting the array into a string, to use as the value for field. – Sylvia Stuurman May 20 '15 at 14:06
  • Hi, thanks for the quick replies : about the fiddle I have to learn what is that.. I tried changing the length and also, I even tried "stupidly" to avoid the outer loop like linea = buffer[x].split(","); read_line(linea, r); r++; – qmarty May 20 '15 at 14:06
  • and so on, repeating manually the function. noway, it always does only ONCE the function . Even if I put an alert() after the first call, it never executes it, but executes the whole function . This read_line(l, r) successfully reads one line and put every value in the right place in the input fields' line. I am asking WHY it executes only once the function, whatever manner you try to repeat it . [n+1] is because the indexOf("Page:2,") leave me on the "0" of the string I need. – qmarty May 20 '15 at 14:18
  • it is like, the function would have a break inside or something which closes the calling function: function A(){ while() { call function B(); } } function B(){ for(){ } } simplified – qmarty May 20 '15 at 14:23
  • executing B() once, terminates A(). Inside function A(){ while(){B(); alert();}} never does the alert() – qmarty May 20 '15 at 14:24
  • SOLVED : finally with js debug of Firefox I found the clue. var col_name = ''+colons[n]+''; was the key colon[n] is an element of the array, typeof returns OBJECT !!? this method to translate in string is by John Magnolia (http://stackoverflow.com/questions/5612787/converting-an-object-to-a-string) , a GENIUS. – qmarty May 20 '15 at 17:17

0 Answers0