JSON object to display the row data in existing dynamic table in JavaScript along with Add Remove












2















I need to use JSON object to display the row data in the dynamic table. I am getting data from ajax call below :



function getJSON() {
return JSON.parse($.ajax({
type: 'GET',
url: thisURL,
dataType: 'json',
global: false,
async: false,
success: function(data) {
return data;
}
}).responseText);
}

var data_all = getJSON();




[{
"Row_1": "Row_1",
"Row_1_Name": "Name1",
"Row_1_age": "Age1",
"Row_2": "Row_2",
"Row_2_Name": "Name2",
"Row_2_age": "Age2",
"Row_3": "Row_3",
"Row_3_Name": "Name3",
"Row_3_age": "Age3",
"Row_4": "Row_4",
"Row_4_Name": "Name4",
"Row_4_age": "Age4"
}]


Ajax call, dynamic table add row are okay. All I need is to display it in the table when onload occurs. Could anyone suggest me. Thanks.






    function createTable() {
var myHead = new Array();
myHead = ['Row','Name', 'Age',''];
var myTable = document.createElement('table');
myTable.setAttribute('id', 'myTable');
var tr = myTable.insertRow(-1);
for (var h = 0; h < myHead.length; h++) {
var th = document.createElement('th');
th.innerHTML = myHead[h];
tr.appendChild(th);
}
var div = document.getElementById('main_Div');
div.appendChild(myTable);
}

function addRow() {
var myHead = new Array();
myHead = ['Row','Name', 'Age',''];
var myTab = document.getElementById('myTable');
var rowCnt = myTab.rows.length;
var tr = myTab.insertRow(rowCnt);
// tr = myTab.insertRow(rowCnt);
var No = "Row_"+rowCnt;
for (var c = 0; c < myHead.length; c++) {
var td = document.createElement('td');
td = tr.insertCell(c);
if (c == 0) {
var ele = document.createElement('input');
ele.setAttribute('type', 'text');
ele.setAttribute('value', No);
ele.setAttribute('readonly', true);
ele.setAttribute('name', 'No');
td.appendChild(ele);
}
else if (c == 1) {
var ele = document.createElement('input');
ele.setAttribute('type', 'text');
ele.setAttribute('value', '');
ele.setAttribute('name', 'Name');
ele.setAttribute('id', 'Name');

td.appendChild(ele);
}
else if (c == 2) {
var ele = document.createElement('input');
ele.setAttribute('type', 'text');
ele.setAttribute('value', '');
ele.setAttribute('name', 'Age');
ele.setAttribute('id', 'Age');

td.appendChild(ele);
}
else if (c == 3) {
var button = document.createElement('input');
button.setAttribute('type', 'button');
button.setAttribute('value', 'Remove');

button.setAttribute('onclick', 'removeRow(this)');

td.appendChild(button);
}

}
}

function removeRow(button) {
var myTab = document.getElementById('myTable');
myTab.deleteRow(button.parentNode.parentNode.rowIndex);

// Use map to rebuild input values
.slice.call(myTab.rows).map(
function (item,index) {
if (item.firstChild.firstChild.nodeType == 1) {
item.firstChild.firstChild.value = "Row_" + index
};
return true;
});
}

		 <head>
<title>Dynamic table</title>
</head>
<body onload="createTable()">
<input type="button" id="addRow" value="Add New Row" onclick="addRow()" />

<div id="main_Div"></div>
</body>












share|improve this question





























    2















    I need to use JSON object to display the row data in the dynamic table. I am getting data from ajax call below :



    function getJSON() {
    return JSON.parse($.ajax({
    type: 'GET',
    url: thisURL,
    dataType: 'json',
    global: false,
    async: false,
    success: function(data) {
    return data;
    }
    }).responseText);
    }

    var data_all = getJSON();




    [{
    "Row_1": "Row_1",
    "Row_1_Name": "Name1",
    "Row_1_age": "Age1",
    "Row_2": "Row_2",
    "Row_2_Name": "Name2",
    "Row_2_age": "Age2",
    "Row_3": "Row_3",
    "Row_3_Name": "Name3",
    "Row_3_age": "Age3",
    "Row_4": "Row_4",
    "Row_4_Name": "Name4",
    "Row_4_age": "Age4"
    }]


    Ajax call, dynamic table add row are okay. All I need is to display it in the table when onload occurs. Could anyone suggest me. Thanks.






        function createTable() {
    var myHead = new Array();
    myHead = ['Row','Name', 'Age',''];
    var myTable = document.createElement('table');
    myTable.setAttribute('id', 'myTable');
    var tr = myTable.insertRow(-1);
    for (var h = 0; h < myHead.length; h++) {
    var th = document.createElement('th');
    th.innerHTML = myHead[h];
    tr.appendChild(th);
    }
    var div = document.getElementById('main_Div');
    div.appendChild(myTable);
    }

    function addRow() {
    var myHead = new Array();
    myHead = ['Row','Name', 'Age',''];
    var myTab = document.getElementById('myTable');
    var rowCnt = myTab.rows.length;
    var tr = myTab.insertRow(rowCnt);
    // tr = myTab.insertRow(rowCnt);
    var No = "Row_"+rowCnt;
    for (var c = 0; c < myHead.length; c++) {
    var td = document.createElement('td');
    td = tr.insertCell(c);
    if (c == 0) {
    var ele = document.createElement('input');
    ele.setAttribute('type', 'text');
    ele.setAttribute('value', No);
    ele.setAttribute('readonly', true);
    ele.setAttribute('name', 'No');
    td.appendChild(ele);
    }
    else if (c == 1) {
    var ele = document.createElement('input');
    ele.setAttribute('type', 'text');
    ele.setAttribute('value', '');
    ele.setAttribute('name', 'Name');
    ele.setAttribute('id', 'Name');

    td.appendChild(ele);
    }
    else if (c == 2) {
    var ele = document.createElement('input');
    ele.setAttribute('type', 'text');
    ele.setAttribute('value', '');
    ele.setAttribute('name', 'Age');
    ele.setAttribute('id', 'Age');

    td.appendChild(ele);
    }
    else if (c == 3) {
    var button = document.createElement('input');
    button.setAttribute('type', 'button');
    button.setAttribute('value', 'Remove');

    button.setAttribute('onclick', 'removeRow(this)');

    td.appendChild(button);
    }

    }
    }

    function removeRow(button) {
    var myTab = document.getElementById('myTable');
    myTab.deleteRow(button.parentNode.parentNode.rowIndex);

    // Use map to rebuild input values
    .slice.call(myTab.rows).map(
    function (item,index) {
    if (item.firstChild.firstChild.nodeType == 1) {
    item.firstChild.firstChild.value = "Row_" + index
    };
    return true;
    });
    }

    		 <head>
    <title>Dynamic table</title>
    </head>
    <body onload="createTable()">
    <input type="button" id="addRow" value="Add New Row" onclick="addRow()" />

    <div id="main_Div"></div>
    </body>












    share|improve this question



























      2












      2








      2








      I need to use JSON object to display the row data in the dynamic table. I am getting data from ajax call below :



      function getJSON() {
      return JSON.parse($.ajax({
      type: 'GET',
      url: thisURL,
      dataType: 'json',
      global: false,
      async: false,
      success: function(data) {
      return data;
      }
      }).responseText);
      }

      var data_all = getJSON();




      [{
      "Row_1": "Row_1",
      "Row_1_Name": "Name1",
      "Row_1_age": "Age1",
      "Row_2": "Row_2",
      "Row_2_Name": "Name2",
      "Row_2_age": "Age2",
      "Row_3": "Row_3",
      "Row_3_Name": "Name3",
      "Row_3_age": "Age3",
      "Row_4": "Row_4",
      "Row_4_Name": "Name4",
      "Row_4_age": "Age4"
      }]


      Ajax call, dynamic table add row are okay. All I need is to display it in the table when onload occurs. Could anyone suggest me. Thanks.






          function createTable() {
      var myHead = new Array();
      myHead = ['Row','Name', 'Age',''];
      var myTable = document.createElement('table');
      myTable.setAttribute('id', 'myTable');
      var tr = myTable.insertRow(-1);
      for (var h = 0; h < myHead.length; h++) {
      var th = document.createElement('th');
      th.innerHTML = myHead[h];
      tr.appendChild(th);
      }
      var div = document.getElementById('main_Div');
      div.appendChild(myTable);
      }

      function addRow() {
      var myHead = new Array();
      myHead = ['Row','Name', 'Age',''];
      var myTab = document.getElementById('myTable');
      var rowCnt = myTab.rows.length;
      var tr = myTab.insertRow(rowCnt);
      // tr = myTab.insertRow(rowCnt);
      var No = "Row_"+rowCnt;
      for (var c = 0; c < myHead.length; c++) {
      var td = document.createElement('td');
      td = tr.insertCell(c);
      if (c == 0) {
      var ele = document.createElement('input');
      ele.setAttribute('type', 'text');
      ele.setAttribute('value', No);
      ele.setAttribute('readonly', true);
      ele.setAttribute('name', 'No');
      td.appendChild(ele);
      }
      else if (c == 1) {
      var ele = document.createElement('input');
      ele.setAttribute('type', 'text');
      ele.setAttribute('value', '');
      ele.setAttribute('name', 'Name');
      ele.setAttribute('id', 'Name');

      td.appendChild(ele);
      }
      else if (c == 2) {
      var ele = document.createElement('input');
      ele.setAttribute('type', 'text');
      ele.setAttribute('value', '');
      ele.setAttribute('name', 'Age');
      ele.setAttribute('id', 'Age');

      td.appendChild(ele);
      }
      else if (c == 3) {
      var button = document.createElement('input');
      button.setAttribute('type', 'button');
      button.setAttribute('value', 'Remove');

      button.setAttribute('onclick', 'removeRow(this)');

      td.appendChild(button);
      }

      }
      }

      function removeRow(button) {
      var myTab = document.getElementById('myTable');
      myTab.deleteRow(button.parentNode.parentNode.rowIndex);

      // Use map to rebuild input values
      .slice.call(myTab.rows).map(
      function (item,index) {
      if (item.firstChild.firstChild.nodeType == 1) {
      item.firstChild.firstChild.value = "Row_" + index
      };
      return true;
      });
      }

      		 <head>
      <title>Dynamic table</title>
      </head>
      <body onload="createTable()">
      <input type="button" id="addRow" value="Add New Row" onclick="addRow()" />

      <div id="main_Div"></div>
      </body>












      share|improve this question
















      I need to use JSON object to display the row data in the dynamic table. I am getting data from ajax call below :



      function getJSON() {
      return JSON.parse($.ajax({
      type: 'GET',
      url: thisURL,
      dataType: 'json',
      global: false,
      async: false,
      success: function(data) {
      return data;
      }
      }).responseText);
      }

      var data_all = getJSON();




      [{
      "Row_1": "Row_1",
      "Row_1_Name": "Name1",
      "Row_1_age": "Age1",
      "Row_2": "Row_2",
      "Row_2_Name": "Name2",
      "Row_2_age": "Age2",
      "Row_3": "Row_3",
      "Row_3_Name": "Name3",
      "Row_3_age": "Age3",
      "Row_4": "Row_4",
      "Row_4_Name": "Name4",
      "Row_4_age": "Age4"
      }]


      Ajax call, dynamic table add row are okay. All I need is to display it in the table when onload occurs. Could anyone suggest me. Thanks.






          function createTable() {
      var myHead = new Array();
      myHead = ['Row','Name', 'Age',''];
      var myTable = document.createElement('table');
      myTable.setAttribute('id', 'myTable');
      var tr = myTable.insertRow(-1);
      for (var h = 0; h < myHead.length; h++) {
      var th = document.createElement('th');
      th.innerHTML = myHead[h];
      tr.appendChild(th);
      }
      var div = document.getElementById('main_Div');
      div.appendChild(myTable);
      }

      function addRow() {
      var myHead = new Array();
      myHead = ['Row','Name', 'Age',''];
      var myTab = document.getElementById('myTable');
      var rowCnt = myTab.rows.length;
      var tr = myTab.insertRow(rowCnt);
      // tr = myTab.insertRow(rowCnt);
      var No = "Row_"+rowCnt;
      for (var c = 0; c < myHead.length; c++) {
      var td = document.createElement('td');
      td = tr.insertCell(c);
      if (c == 0) {
      var ele = document.createElement('input');
      ele.setAttribute('type', 'text');
      ele.setAttribute('value', No);
      ele.setAttribute('readonly', true);
      ele.setAttribute('name', 'No');
      td.appendChild(ele);
      }
      else if (c == 1) {
      var ele = document.createElement('input');
      ele.setAttribute('type', 'text');
      ele.setAttribute('value', '');
      ele.setAttribute('name', 'Name');
      ele.setAttribute('id', 'Name');

      td.appendChild(ele);
      }
      else if (c == 2) {
      var ele = document.createElement('input');
      ele.setAttribute('type', 'text');
      ele.setAttribute('value', '');
      ele.setAttribute('name', 'Age');
      ele.setAttribute('id', 'Age');

      td.appendChild(ele);
      }
      else if (c == 3) {
      var button = document.createElement('input');
      button.setAttribute('type', 'button');
      button.setAttribute('value', 'Remove');

      button.setAttribute('onclick', 'removeRow(this)');

      td.appendChild(button);
      }

      }
      }

      function removeRow(button) {
      var myTab = document.getElementById('myTable');
      myTab.deleteRow(button.parentNode.parentNode.rowIndex);

      // Use map to rebuild input values
      .slice.call(myTab.rows).map(
      function (item,index) {
      if (item.firstChild.firstChild.nodeType == 1) {
      item.firstChild.firstChild.value = "Row_" + index
      };
      return true;
      });
      }

      		 <head>
      <title>Dynamic table</title>
      </head>
      <body onload="createTable()">
      <input type="button" id="addRow" value="Add New Row" onclick="addRow()" />

      <div id="main_Div"></div>
      </body>








          function createTable() {
      var myHead = new Array();
      myHead = ['Row','Name', 'Age',''];
      var myTable = document.createElement('table');
      myTable.setAttribute('id', 'myTable');
      var tr = myTable.insertRow(-1);
      for (var h = 0; h < myHead.length; h++) {
      var th = document.createElement('th');
      th.innerHTML = myHead[h];
      tr.appendChild(th);
      }
      var div = document.getElementById('main_Div');
      div.appendChild(myTable);
      }

      function addRow() {
      var myHead = new Array();
      myHead = ['Row','Name', 'Age',''];
      var myTab = document.getElementById('myTable');
      var rowCnt = myTab.rows.length;
      var tr = myTab.insertRow(rowCnt);
      // tr = myTab.insertRow(rowCnt);
      var No = "Row_"+rowCnt;
      for (var c = 0; c < myHead.length; c++) {
      var td = document.createElement('td');
      td = tr.insertCell(c);
      if (c == 0) {
      var ele = document.createElement('input');
      ele.setAttribute('type', 'text');
      ele.setAttribute('value', No);
      ele.setAttribute('readonly', true);
      ele.setAttribute('name', 'No');
      td.appendChild(ele);
      }
      else if (c == 1) {
      var ele = document.createElement('input');
      ele.setAttribute('type', 'text');
      ele.setAttribute('value', '');
      ele.setAttribute('name', 'Name');
      ele.setAttribute('id', 'Name');

      td.appendChild(ele);
      }
      else if (c == 2) {
      var ele = document.createElement('input');
      ele.setAttribute('type', 'text');
      ele.setAttribute('value', '');
      ele.setAttribute('name', 'Age');
      ele.setAttribute('id', 'Age');

      td.appendChild(ele);
      }
      else if (c == 3) {
      var button = document.createElement('input');
      button.setAttribute('type', 'button');
      button.setAttribute('value', 'Remove');

      button.setAttribute('onclick', 'removeRow(this)');

      td.appendChild(button);
      }

      }
      }

      function removeRow(button) {
      var myTab = document.getElementById('myTable');
      myTab.deleteRow(button.parentNode.parentNode.rowIndex);

      // Use map to rebuild input values
      .slice.call(myTab.rows).map(
      function (item,index) {
      if (item.firstChild.firstChild.nodeType == 1) {
      item.firstChild.firstChild.value = "Row_" + index
      };
      return true;
      });
      }

      		 <head>
      <title>Dynamic table</title>
      </head>
      <body onload="createTable()">
      <input type="button" id="addRow" value="Add New Row" onclick="addRow()" />

      <div id="main_Div"></div>
      </body>





          function createTable() {
      var myHead = new Array();
      myHead = ['Row','Name', 'Age',''];
      var myTable = document.createElement('table');
      myTable.setAttribute('id', 'myTable');
      var tr = myTable.insertRow(-1);
      for (var h = 0; h < myHead.length; h++) {
      var th = document.createElement('th');
      th.innerHTML = myHead[h];
      tr.appendChild(th);
      }
      var div = document.getElementById('main_Div');
      div.appendChild(myTable);
      }

      function addRow() {
      var myHead = new Array();
      myHead = ['Row','Name', 'Age',''];
      var myTab = document.getElementById('myTable');
      var rowCnt = myTab.rows.length;
      var tr = myTab.insertRow(rowCnt);
      // tr = myTab.insertRow(rowCnt);
      var No = "Row_"+rowCnt;
      for (var c = 0; c < myHead.length; c++) {
      var td = document.createElement('td');
      td = tr.insertCell(c);
      if (c == 0) {
      var ele = document.createElement('input');
      ele.setAttribute('type', 'text');
      ele.setAttribute('value', No);
      ele.setAttribute('readonly', true);
      ele.setAttribute('name', 'No');
      td.appendChild(ele);
      }
      else if (c == 1) {
      var ele = document.createElement('input');
      ele.setAttribute('type', 'text');
      ele.setAttribute('value', '');
      ele.setAttribute('name', 'Name');
      ele.setAttribute('id', 'Name');

      td.appendChild(ele);
      }
      else if (c == 2) {
      var ele = document.createElement('input');
      ele.setAttribute('type', 'text');
      ele.setAttribute('value', '');
      ele.setAttribute('name', 'Age');
      ele.setAttribute('id', 'Age');

      td.appendChild(ele);
      }
      else if (c == 3) {
      var button = document.createElement('input');
      button.setAttribute('type', 'button');
      button.setAttribute('value', 'Remove');

      button.setAttribute('onclick', 'removeRow(this)');

      td.appendChild(button);
      }

      }
      }

      function removeRow(button) {
      var myTab = document.getElementById('myTable');
      myTab.deleteRow(button.parentNode.parentNode.rowIndex);

      // Use map to rebuild input values
      .slice.call(myTab.rows).map(
      function (item,index) {
      if (item.firstChild.firstChild.nodeType == 1) {
      item.firstChild.firstChild.value = "Row_" + index
      };
      return true;
      });
      }

      		 <head>
      <title>Dynamic table</title>
      </head>
      <body onload="createTable()">
      <input type="button" id="addRow" value="Add New Row" onclick="addRow()" />

      <div id="main_Div"></div>
      </body>






      javascript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 9:23









      Nick Parsons

      7,6052724




      7,6052724










      asked Nov 20 '18 at 9:18









      user3280899user3280899

      847




      847
























          1 Answer
          1






          active

          oldest

          votes


















          0














          try this one. it create the whole table at the time of page load.



          function createTable() {
          var myHead = new Array();
          myHead = ['Row', 'Name', 'Age', ''];
          var myTable = document.createElement('table');
          myTable.setAttribute('id', 'myTable');
          var tr = myTable.insertRow(-1);
          for (var h = 0; h < myHead.length; h++) {
          var th = document.createElement('th');
          th.innerHTML = myHead[h];
          tr.appendChild(th);
          }
          var div = document.getElementById('main_Div');
          div.appendChild(myTable);
          addRow() //newly added
          }
          function addRow() {
          var data_all = [{
          "Row_": "Row_1",
          "Row_Name": "Name1",
          "Row_age": "Age1"
          },
          {
          "Row_": "Row_2",
          "Row_Name": "Name2",
          "Row_age": "Age2"
          },
          {
          "Row_": "Row_3",
          "Row_Name": "Name3",
          "Row_age": "Age3"
          },
          {
          "Row_": "Row_4",
          "Row_Name": "Name4",
          "Row_age": "Age4"
          }] //newly added for making it easy

          var myHead = new Array();
          myHead = ['Row', 'Name', 'Age', ''];
          var myTab = document.getElementById('myTable');
          var rowCnt = myTab.rows.length;
          var row_c = 4; //newly added
          //this loop creates rows
          for (var j = data_all.length - 1; j > -1; j--) { //newly added
          var tr = myTab.insertRow(rowCnt);

          var No = "Row_" + (row_c); //newly added
          row_c--; //newly added
          //creates cells in a row
          for (var c = 0; c < myHead.length; c++) {
          var td = document.createElement('td');
          td = tr.insertCell(c);
          if (c == 0) {
          var ele = document.createElement('input');
          ele.setAttribute('type', 'text');
          ele.setAttribute('value', No);
          ele.setAttribute('readonly', true);
          ele.setAttribute('name', 'No');
          td.appendChild(ele);
          }
          else if (c == 1) {
          var ele = document.createElement('input');
          ele.setAttribute('type', 'text');
          ele.setAttribute('value', '');
          ele.setAttribute('name', 'Name');
          ele.setAttribute('id', 'Name');
          ele.setAttribute('value', data_all[j].Row_Name); //newly added
          td.appendChild(ele);
          }
          else if (c == 2) {
          var ele = document.createElement('input');
          ele.setAttribute('type', 'text');
          ele.setAttribute('value', '');
          ele.setAttribute('name', 'Age');
          ele.setAttribute('id', 'Age');
          ele.setAttribute('value', data_all[j].Row_age); //newly added
          td.appendChild(ele);
          }
          else if (c == 3) {
          var button = document.createElement('input');
          button.setAttribute('type', 'button');
          button.setAttribute('value', 'Remove');

          button.setAttribute('onclick', 'removeRow(this)');

          td.appendChild(button);

          }

          }
          }
          }

          function removeRow(button) {
          var myTab = document.getElementById('myTable');
          myTab.deleteRow(button.parentNode.parentNode.rowIndex);

          // Use map to rebuild input values
          .slice.call(myTab.rows).map(
          function (item, index) {
          if (item.firstChild.firstChild.nodeType == 1) {
          item.firstChild.firstChild.value = "Row_" + index
          };
          return true;
          });
          }





          share|improve this answer


























          • Hi @Fathima, thanks for your reply. Now as per your suggestion I am getting the JSON object. But how the rows should be added in the dynamic table without clicking addrow() button. As per existing code only CreateTable has onLoad and i have no problem to create an JSON object. When I have existing JSON object records I need to display the saved data to the table. Hope you get my point! Thanks.

            – user3280899
            Nov 21 '18 at 9:12













          • Check the newly edited version of my comment.. the code creates the whole table at the time of page load... perdon me if i made any mistakes or if i failed to understand you... hope it will serve your purpose. good luck.

            – Fathma siddique
            Nov 21 '18 at 11:50











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389729%2fjson-object-to-display-the-row-data-in-existing-dynamic-table-in-javascript-alon%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          try this one. it create the whole table at the time of page load.



          function createTable() {
          var myHead = new Array();
          myHead = ['Row', 'Name', 'Age', ''];
          var myTable = document.createElement('table');
          myTable.setAttribute('id', 'myTable');
          var tr = myTable.insertRow(-1);
          for (var h = 0; h < myHead.length; h++) {
          var th = document.createElement('th');
          th.innerHTML = myHead[h];
          tr.appendChild(th);
          }
          var div = document.getElementById('main_Div');
          div.appendChild(myTable);
          addRow() //newly added
          }
          function addRow() {
          var data_all = [{
          "Row_": "Row_1",
          "Row_Name": "Name1",
          "Row_age": "Age1"
          },
          {
          "Row_": "Row_2",
          "Row_Name": "Name2",
          "Row_age": "Age2"
          },
          {
          "Row_": "Row_3",
          "Row_Name": "Name3",
          "Row_age": "Age3"
          },
          {
          "Row_": "Row_4",
          "Row_Name": "Name4",
          "Row_age": "Age4"
          }] //newly added for making it easy

          var myHead = new Array();
          myHead = ['Row', 'Name', 'Age', ''];
          var myTab = document.getElementById('myTable');
          var rowCnt = myTab.rows.length;
          var row_c = 4; //newly added
          //this loop creates rows
          for (var j = data_all.length - 1; j > -1; j--) { //newly added
          var tr = myTab.insertRow(rowCnt);

          var No = "Row_" + (row_c); //newly added
          row_c--; //newly added
          //creates cells in a row
          for (var c = 0; c < myHead.length; c++) {
          var td = document.createElement('td');
          td = tr.insertCell(c);
          if (c == 0) {
          var ele = document.createElement('input');
          ele.setAttribute('type', 'text');
          ele.setAttribute('value', No);
          ele.setAttribute('readonly', true);
          ele.setAttribute('name', 'No');
          td.appendChild(ele);
          }
          else if (c == 1) {
          var ele = document.createElement('input');
          ele.setAttribute('type', 'text');
          ele.setAttribute('value', '');
          ele.setAttribute('name', 'Name');
          ele.setAttribute('id', 'Name');
          ele.setAttribute('value', data_all[j].Row_Name); //newly added
          td.appendChild(ele);
          }
          else if (c == 2) {
          var ele = document.createElement('input');
          ele.setAttribute('type', 'text');
          ele.setAttribute('value', '');
          ele.setAttribute('name', 'Age');
          ele.setAttribute('id', 'Age');
          ele.setAttribute('value', data_all[j].Row_age); //newly added
          td.appendChild(ele);
          }
          else if (c == 3) {
          var button = document.createElement('input');
          button.setAttribute('type', 'button');
          button.setAttribute('value', 'Remove');

          button.setAttribute('onclick', 'removeRow(this)');

          td.appendChild(button);

          }

          }
          }
          }

          function removeRow(button) {
          var myTab = document.getElementById('myTable');
          myTab.deleteRow(button.parentNode.parentNode.rowIndex);

          // Use map to rebuild input values
          .slice.call(myTab.rows).map(
          function (item, index) {
          if (item.firstChild.firstChild.nodeType == 1) {
          item.firstChild.firstChild.value = "Row_" + index
          };
          return true;
          });
          }





          share|improve this answer


























          • Hi @Fathima, thanks for your reply. Now as per your suggestion I am getting the JSON object. But how the rows should be added in the dynamic table without clicking addrow() button. As per existing code only CreateTable has onLoad and i have no problem to create an JSON object. When I have existing JSON object records I need to display the saved data to the table. Hope you get my point! Thanks.

            – user3280899
            Nov 21 '18 at 9:12













          • Check the newly edited version of my comment.. the code creates the whole table at the time of page load... perdon me if i made any mistakes or if i failed to understand you... hope it will serve your purpose. good luck.

            – Fathma siddique
            Nov 21 '18 at 11:50
















          0














          try this one. it create the whole table at the time of page load.



          function createTable() {
          var myHead = new Array();
          myHead = ['Row', 'Name', 'Age', ''];
          var myTable = document.createElement('table');
          myTable.setAttribute('id', 'myTable');
          var tr = myTable.insertRow(-1);
          for (var h = 0; h < myHead.length; h++) {
          var th = document.createElement('th');
          th.innerHTML = myHead[h];
          tr.appendChild(th);
          }
          var div = document.getElementById('main_Div');
          div.appendChild(myTable);
          addRow() //newly added
          }
          function addRow() {
          var data_all = [{
          "Row_": "Row_1",
          "Row_Name": "Name1",
          "Row_age": "Age1"
          },
          {
          "Row_": "Row_2",
          "Row_Name": "Name2",
          "Row_age": "Age2"
          },
          {
          "Row_": "Row_3",
          "Row_Name": "Name3",
          "Row_age": "Age3"
          },
          {
          "Row_": "Row_4",
          "Row_Name": "Name4",
          "Row_age": "Age4"
          }] //newly added for making it easy

          var myHead = new Array();
          myHead = ['Row', 'Name', 'Age', ''];
          var myTab = document.getElementById('myTable');
          var rowCnt = myTab.rows.length;
          var row_c = 4; //newly added
          //this loop creates rows
          for (var j = data_all.length - 1; j > -1; j--) { //newly added
          var tr = myTab.insertRow(rowCnt);

          var No = "Row_" + (row_c); //newly added
          row_c--; //newly added
          //creates cells in a row
          for (var c = 0; c < myHead.length; c++) {
          var td = document.createElement('td');
          td = tr.insertCell(c);
          if (c == 0) {
          var ele = document.createElement('input');
          ele.setAttribute('type', 'text');
          ele.setAttribute('value', No);
          ele.setAttribute('readonly', true);
          ele.setAttribute('name', 'No');
          td.appendChild(ele);
          }
          else if (c == 1) {
          var ele = document.createElement('input');
          ele.setAttribute('type', 'text');
          ele.setAttribute('value', '');
          ele.setAttribute('name', 'Name');
          ele.setAttribute('id', 'Name');
          ele.setAttribute('value', data_all[j].Row_Name); //newly added
          td.appendChild(ele);
          }
          else if (c == 2) {
          var ele = document.createElement('input');
          ele.setAttribute('type', 'text');
          ele.setAttribute('value', '');
          ele.setAttribute('name', 'Age');
          ele.setAttribute('id', 'Age');
          ele.setAttribute('value', data_all[j].Row_age); //newly added
          td.appendChild(ele);
          }
          else if (c == 3) {
          var button = document.createElement('input');
          button.setAttribute('type', 'button');
          button.setAttribute('value', 'Remove');

          button.setAttribute('onclick', 'removeRow(this)');

          td.appendChild(button);

          }

          }
          }
          }

          function removeRow(button) {
          var myTab = document.getElementById('myTable');
          myTab.deleteRow(button.parentNode.parentNode.rowIndex);

          // Use map to rebuild input values
          .slice.call(myTab.rows).map(
          function (item, index) {
          if (item.firstChild.firstChild.nodeType == 1) {
          item.firstChild.firstChild.value = "Row_" + index
          };
          return true;
          });
          }





          share|improve this answer


























          • Hi @Fathima, thanks for your reply. Now as per your suggestion I am getting the JSON object. But how the rows should be added in the dynamic table without clicking addrow() button. As per existing code only CreateTable has onLoad and i have no problem to create an JSON object. When I have existing JSON object records I need to display the saved data to the table. Hope you get my point! Thanks.

            – user3280899
            Nov 21 '18 at 9:12













          • Check the newly edited version of my comment.. the code creates the whole table at the time of page load... perdon me if i made any mistakes or if i failed to understand you... hope it will serve your purpose. good luck.

            – Fathma siddique
            Nov 21 '18 at 11:50














          0












          0








          0







          try this one. it create the whole table at the time of page load.



          function createTable() {
          var myHead = new Array();
          myHead = ['Row', 'Name', 'Age', ''];
          var myTable = document.createElement('table');
          myTable.setAttribute('id', 'myTable');
          var tr = myTable.insertRow(-1);
          for (var h = 0; h < myHead.length; h++) {
          var th = document.createElement('th');
          th.innerHTML = myHead[h];
          tr.appendChild(th);
          }
          var div = document.getElementById('main_Div');
          div.appendChild(myTable);
          addRow() //newly added
          }
          function addRow() {
          var data_all = [{
          "Row_": "Row_1",
          "Row_Name": "Name1",
          "Row_age": "Age1"
          },
          {
          "Row_": "Row_2",
          "Row_Name": "Name2",
          "Row_age": "Age2"
          },
          {
          "Row_": "Row_3",
          "Row_Name": "Name3",
          "Row_age": "Age3"
          },
          {
          "Row_": "Row_4",
          "Row_Name": "Name4",
          "Row_age": "Age4"
          }] //newly added for making it easy

          var myHead = new Array();
          myHead = ['Row', 'Name', 'Age', ''];
          var myTab = document.getElementById('myTable');
          var rowCnt = myTab.rows.length;
          var row_c = 4; //newly added
          //this loop creates rows
          for (var j = data_all.length - 1; j > -1; j--) { //newly added
          var tr = myTab.insertRow(rowCnt);

          var No = "Row_" + (row_c); //newly added
          row_c--; //newly added
          //creates cells in a row
          for (var c = 0; c < myHead.length; c++) {
          var td = document.createElement('td');
          td = tr.insertCell(c);
          if (c == 0) {
          var ele = document.createElement('input');
          ele.setAttribute('type', 'text');
          ele.setAttribute('value', No);
          ele.setAttribute('readonly', true);
          ele.setAttribute('name', 'No');
          td.appendChild(ele);
          }
          else if (c == 1) {
          var ele = document.createElement('input');
          ele.setAttribute('type', 'text');
          ele.setAttribute('value', '');
          ele.setAttribute('name', 'Name');
          ele.setAttribute('id', 'Name');
          ele.setAttribute('value', data_all[j].Row_Name); //newly added
          td.appendChild(ele);
          }
          else if (c == 2) {
          var ele = document.createElement('input');
          ele.setAttribute('type', 'text');
          ele.setAttribute('value', '');
          ele.setAttribute('name', 'Age');
          ele.setAttribute('id', 'Age');
          ele.setAttribute('value', data_all[j].Row_age); //newly added
          td.appendChild(ele);
          }
          else if (c == 3) {
          var button = document.createElement('input');
          button.setAttribute('type', 'button');
          button.setAttribute('value', 'Remove');

          button.setAttribute('onclick', 'removeRow(this)');

          td.appendChild(button);

          }

          }
          }
          }

          function removeRow(button) {
          var myTab = document.getElementById('myTable');
          myTab.deleteRow(button.parentNode.parentNode.rowIndex);

          // Use map to rebuild input values
          .slice.call(myTab.rows).map(
          function (item, index) {
          if (item.firstChild.firstChild.nodeType == 1) {
          item.firstChild.firstChild.value = "Row_" + index
          };
          return true;
          });
          }





          share|improve this answer















          try this one. it create the whole table at the time of page load.



          function createTable() {
          var myHead = new Array();
          myHead = ['Row', 'Name', 'Age', ''];
          var myTable = document.createElement('table');
          myTable.setAttribute('id', 'myTable');
          var tr = myTable.insertRow(-1);
          for (var h = 0; h < myHead.length; h++) {
          var th = document.createElement('th');
          th.innerHTML = myHead[h];
          tr.appendChild(th);
          }
          var div = document.getElementById('main_Div');
          div.appendChild(myTable);
          addRow() //newly added
          }
          function addRow() {
          var data_all = [{
          "Row_": "Row_1",
          "Row_Name": "Name1",
          "Row_age": "Age1"
          },
          {
          "Row_": "Row_2",
          "Row_Name": "Name2",
          "Row_age": "Age2"
          },
          {
          "Row_": "Row_3",
          "Row_Name": "Name3",
          "Row_age": "Age3"
          },
          {
          "Row_": "Row_4",
          "Row_Name": "Name4",
          "Row_age": "Age4"
          }] //newly added for making it easy

          var myHead = new Array();
          myHead = ['Row', 'Name', 'Age', ''];
          var myTab = document.getElementById('myTable');
          var rowCnt = myTab.rows.length;
          var row_c = 4; //newly added
          //this loop creates rows
          for (var j = data_all.length - 1; j > -1; j--) { //newly added
          var tr = myTab.insertRow(rowCnt);

          var No = "Row_" + (row_c); //newly added
          row_c--; //newly added
          //creates cells in a row
          for (var c = 0; c < myHead.length; c++) {
          var td = document.createElement('td');
          td = tr.insertCell(c);
          if (c == 0) {
          var ele = document.createElement('input');
          ele.setAttribute('type', 'text');
          ele.setAttribute('value', No);
          ele.setAttribute('readonly', true);
          ele.setAttribute('name', 'No');
          td.appendChild(ele);
          }
          else if (c == 1) {
          var ele = document.createElement('input');
          ele.setAttribute('type', 'text');
          ele.setAttribute('value', '');
          ele.setAttribute('name', 'Name');
          ele.setAttribute('id', 'Name');
          ele.setAttribute('value', data_all[j].Row_Name); //newly added
          td.appendChild(ele);
          }
          else if (c == 2) {
          var ele = document.createElement('input');
          ele.setAttribute('type', 'text');
          ele.setAttribute('value', '');
          ele.setAttribute('name', 'Age');
          ele.setAttribute('id', 'Age');
          ele.setAttribute('value', data_all[j].Row_age); //newly added
          td.appendChild(ele);
          }
          else if (c == 3) {
          var button = document.createElement('input');
          button.setAttribute('type', 'button');
          button.setAttribute('value', 'Remove');

          button.setAttribute('onclick', 'removeRow(this)');

          td.appendChild(button);

          }

          }
          }
          }

          function removeRow(button) {
          var myTab = document.getElementById('myTable');
          myTab.deleteRow(button.parentNode.parentNode.rowIndex);

          // Use map to rebuild input values
          .slice.call(myTab.rows).map(
          function (item, index) {
          if (item.firstChild.firstChild.nodeType == 1) {
          item.firstChild.firstChild.value = "Row_" + index
          };
          return true;
          });
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 11:52

























          answered Nov 20 '18 at 10:07









          Fathma siddiqueFathma siddique

          887




          887













          • Hi @Fathima, thanks for your reply. Now as per your suggestion I am getting the JSON object. But how the rows should be added in the dynamic table without clicking addrow() button. As per existing code only CreateTable has onLoad and i have no problem to create an JSON object. When I have existing JSON object records I need to display the saved data to the table. Hope you get my point! Thanks.

            – user3280899
            Nov 21 '18 at 9:12













          • Check the newly edited version of my comment.. the code creates the whole table at the time of page load... perdon me if i made any mistakes or if i failed to understand you... hope it will serve your purpose. good luck.

            – Fathma siddique
            Nov 21 '18 at 11:50



















          • Hi @Fathima, thanks for your reply. Now as per your suggestion I am getting the JSON object. But how the rows should be added in the dynamic table without clicking addrow() button. As per existing code only CreateTable has onLoad and i have no problem to create an JSON object. When I have existing JSON object records I need to display the saved data to the table. Hope you get my point! Thanks.

            – user3280899
            Nov 21 '18 at 9:12













          • Check the newly edited version of my comment.. the code creates the whole table at the time of page load... perdon me if i made any mistakes or if i failed to understand you... hope it will serve your purpose. good luck.

            – Fathma siddique
            Nov 21 '18 at 11:50

















          Hi @Fathima, thanks for your reply. Now as per your suggestion I am getting the JSON object. But how the rows should be added in the dynamic table without clicking addrow() button. As per existing code only CreateTable has onLoad and i have no problem to create an JSON object. When I have existing JSON object records I need to display the saved data to the table. Hope you get my point! Thanks.

          – user3280899
          Nov 21 '18 at 9:12







          Hi @Fathima, thanks for your reply. Now as per your suggestion I am getting the JSON object. But how the rows should be added in the dynamic table without clicking addrow() button. As per existing code only CreateTable has onLoad and i have no problem to create an JSON object. When I have existing JSON object records I need to display the saved data to the table. Hope you get my point! Thanks.

          – user3280899
          Nov 21 '18 at 9:12















          Check the newly edited version of my comment.. the code creates the whole table at the time of page load... perdon me if i made any mistakes or if i failed to understand you... hope it will serve your purpose. good luck.

          – Fathma siddique
          Nov 21 '18 at 11:50





          Check the newly edited version of my comment.. the code creates the whole table at the time of page load... perdon me if i made any mistakes or if i failed to understand you... hope it will serve your purpose. good luck.

          – Fathma siddique
          Nov 21 '18 at 11:50




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389729%2fjson-object-to-display-the-row-data-in-existing-dynamic-table-in-javascript-alon%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?