1

I'm creating a dynamic table row. my data source looks like this

 data = [
  {
    "id": 11,
    "tests": [{
        "id": 2,
        "name": "Glucose Level"
      },
      {
        "id": 4,
        "name": "Blood Oxygen"
      }
    ],
    "carepathway": {
      "id": 16,
      "name": "General Check-Up",
      "slug": "general_check_up",
      "dynamic_field": null
    }
  }
]

This is how I'm creating my table.

$.each(data, function(key, val) {
  var tr = $("<tr />");


  tr.append($('<td>').append("<p>" + val.carepathway.name + "</p>"))
    .append(
      $('<td>').append(

        $.each(val.tests, function(k, v) {
          var $test_controls = $("<div/>", {
            "class": "controls"
          })
          $test_controls.append(
            $("<label>", {
              "class": "checkbox",
              "name": v.name,
              "text": v.name,
              'id': v.id
            }).append(
              $("<input>", {
                "type": "checkbox",
                "value": v.name,
                'id': v.id

              })
            )
          )
        })
      ))

  $("#patient_care_pathway_table tbody").append(tr);

});

First <td> value coming fine but nothing is coming for the second <td> What I'm doing wrong here?

Second <td> should contain the checkbox with the label, Like this

enter image description here

JSFIDDLE

2
  • 1
    How should the table of the given data look like? Commented Jun 8, 2018 at 19:15
  • Added image on the post Commented Jun 8, 2018 at 19:23

2 Answers 2

1

The problem is that you're appending the return value of $.each which is undefined to the second td. Use $.map instead of $.each:

tr.append($('<td>').append("<p>" + val.carepathway.name + "</p>"))
.append(
  $('<td>').append(
    $.map(val.tests, function(k, v) {              // use map here instead of each
      var $test_controls = ...;
      ...
      return $test_controls;                       // don't forget to return the element (read about map)
    })
  ))
Sign up to request clarification or add additional context in comments.

Comments

1

I would put the loop outside instead of daisy-chaining it inside the append function like how you did, it would return undefined for the loop. This should work:

$.each(data, function(key, val) {
    var tr = $("<tr />");

    tr.append($('<td>').append("<p>" + val.carepathway.name + "</p>"));
    var second_td = $('<td>'); // Have a variable for the second td so that you can access it later
    $.each(val.tests, function(k, v) { // Process content of your second td
        var $test_controls = $("<div/>", {
            "class": "controls"
        })
        $test_controls.append(
            $("<label>", {
                "class": "checkbox",
                "name": v.name,
                "text": v.name,
                'id': v.id
            }).append(
                $("<input>", {
                    "type": "checkbox",
                    "value": v.name,
                    'id': v.id

                })
            )
        )
        second_td.append($test_controls);
    });
    tr.append(second_td); // Once done, add it back to tr

    $("#patient_care_pathway_table tbody").append(tr);

});

Comments

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.