1

I have a problem I can't figure out. I have an ajax success function that loops through a multidimensional array like so:

function onSuccessClientCustomerData(data) {
    // Count length of object
    var count = 0;
    for (i in data) {
        if (data.hasOwnProperty(i)) {
            count++;
        }
    }

    // Fetch data
    var arr = new Array(data.variable0, data.variable1, data.variable2, data.variable3, data.variable4);
    for(var i = 0; i< count; i++) {
        var variable = $.parseJSON(arr[i]);

    }
}

I each arrays have three keys and I want to populate the td:eq(1) to td:eq(2) on each row:

<table id="test">
<tr>
    <td><input type="text"></td>
<td class="outputName">-</td>
    <td class="outputRating">-</td>
<td class="outputTurnover">-</td>
</tr>
<tr>
    <td><input type="text"></td>
<td class="outputName">-</td>
    <td class="outputRating">-</td>
<td class="outputTurnover">-</td>
</tr>
</table>

How do I do that. I've tried som each functions but couldn't get it to work!

Would be grateful for any help or references to read!

Thanks!

EDIT / HERE IS MY SOLUTION

function onSuccessClientCustomerData(data) {
    // Count length of object
    var count = 0;
    for (i in data) {
        if (data.hasOwnProperty(i)) {
            count++;
        }
    }

    // Fetch data
    var arr = new Array(data.variable0, data.variable1, data.variable2, data.variable3, data.variable4);
    for (var i = 0; i < count; i++) {
        var arr_j = $.parseJSON(arr[i]);
        $("#row" + i + " .key0").html(arr_j.name);
        $("#row" + i + " .key1").html(arr_j.rating);
        $("#row" + i + " .key2").html(arr_j.percentOfTotalIncome);
    }
}

2 Answers 2

2

Do you need help in the part where you have to insert the values? If so:

jQuery Code

function onSuccessClientCustomerData(data) {
    // Count length of object
    var count = 0;
    for (i in data) {
        if (data.hasOwnProperty(i)) {
            count++;
        }
    }

    // Fetch data
    var arr_i = new Array(data.variable0, data.variable1, data.variable2, data.variable3, data.variable4);
    for (var i = 0; i < count; i++) {
        var arr_j = arr_i[i];
        for (var j = 0; j < arr_j. length; j++) {
            $("#row" + i + " .key" + j).html(arr_j[j]);
        }
    }
}

HTML Code

<table id="test">
  <tr id="row0">
    <td><input type="text"></td>
    <td class="key0" class="outputName">...</td>
    <td class="key1" class="outputRating">...</td>
    <td class="key2" class="outputTurnover">...</td>
  </tr>
  <tr id="row1">
    <td><input type="text"></td>
    <td class="key0" class="outputName">...</td>
    <td class="key1" class="outputRating">...</td>
    <td class="key2" class="outputTurnover">...</td>
  </tr>
</table>

Test Code

I tested the code with Chrome and it worked fine. Here are the steps to reproduce it:-

  • Open the Elements pane using F12 and do the following:

    1. Add the HTML part to a webpage;
    2. Optionally add a border and some margins to make it clearer;


  • Open the Web Console using Ctrl + Shift + J and do the following:

    1. Give arr_i (we'll be using that instead of data) a placeholder value:

      var arr_i = new Array([00, 01, 02], [10, 12, 13]);
      
    2. Run the code to set count:

      // Count length of object
              var count = 0;
              for (i in data) {
                  if (data.hasOwnProperty(i)) {
                      count++;
                  }
             }
      
    3. Use my function to add the values (be sure to run the code on a page that uses jQuery! Or else, use this bookmark):

      for (var i = 0; i < count; i++) {
          var arr_j = arr_i[i];
          for (var j = 0; j < arr_j. length; j++) {
              $("#row" + i + " .key" + j).html(arr_j[j]);
          }
      }
      

And voilà! This should probably fetch me about 20 to 30 votes by today tomorrow this week next year (hopefully) ...

Sign up to request clarification or add additional context in comments.

8 Comments

Will try when I get to me my computer! Thanks!
Hey tested it now, I get this out {, ", 0 so this means it doesn't fetch the key values. Only the three first characters. Don't know why?!
Your suggestions works but I have a multidimensional array so the $.parseJSON() is needed. I tried adding it to your suggestion but that doesn't work :) At least not for me :)
I tweeeked your code some but it works perfectly! Thank you! :)
@Ismailp Happy to help. Would you mind editing my answer to reflect the tweaks you made? It'll help future readers and I'd like to see it... :)
|
0
function onSuccessClientCustomerData(data) {
    var data = $.parseJSON(data);
    ....
}

You need to parse the data first.

1 Comment

Look at the success function. It is already parsed. I get the data but I want to know how to populate the table as described

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.