Display json data in html table using jQuery

Viewed 11069

How to display json data in html table using jQuery ? and How can i remove case sensitive while searching the result?

expected output

enter image description here How can i display the result in my table? How can i achieve this?

var data = [{
    "username": "John Doe",
    "email": "jn@gmail.com",
    "skills": "java,c,html,css"
  },
  {
    "username": "Jane Smith",
    "email": "js@gmail.com",
    "skills": "java,sql"
  },
  {
    "username": "Chuck Berry",
    "email": "cb@gmail.com",
    "skills": "vuejs"
  }
];



/* Get Result */
function getResult() {
  /* Read value from input fields */
  var skills = $("#skills").val() || '',
    email = $("#email").val() || '',
    username = $("#username").val() || '';

  var result = [],
    i;

  for (i = 0; i < data.length; i++) {
    if ((skills !== '' && data[i]["skills"].indexOf(skills) !== -1) || (data[i]["email"] === email) || (
        data[i]["username"] === username)) {
      result.push(data[i]);
    }
  }

  return result;
};

$('#submit').click(function onClick() {
  var output = getResult();
  console.log(output);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="skills" type="text" placeholder="skills">
<input id="email" type="email" placeholder="mail id">
<input id="username" type="text" placeholder="username">
<input id="submit" type="submit" value="submit">

3 Answers

You need to create a table and need to append coming data to this table using below code:-

$('#submit').click(function onClick() {
  var output = getResult();
  var html = '';
  $.each(output,function(key,value){
      html +='<tr>';
      html +='<td>'+ value.username + '</td>';
      html +='<td>'+ value.email + '</td>';
      html +='<td>'+ value.skills + '</td>';
      html +='</tr>';
  });
$('table tbody').html(html);
});

To do case-insensitive comparison use .toUpperCase()

Working snippet:-

var data = [{
    "username": "John Doe",
    "email": "jn@gmail.com",
    "skills": "java,c,html,css"
  },
  {
    "username": "Jane Smith",
    "email": "js@gmail.com",
    "skills": "java,sql"
  },
  {
    "username": "Chuck Berry",
    "email": "cb@gmail.com",
    "skills": "vuejs"
  }
];



/* Get Result */
function getResult() {
  /* Read value from input fields */
  var skills = $("#skills").val() || '',
    email = $("#email").val() || '',
    username = $("#username").val() || '';

  var result = [],
    i;

  for (i = 0; i < data.length; i++) {
    if ((skills !== '' && data[i]["skills"].toUpperCase().indexOf(skills.toUpperCase()) !== -1) || (data[i]["email"].toUpperCase() === email.toUpperCase()) || (
        data[i]["username"].toUpperCase() === username.toUpperCase())) {
      result.push(data[i]);
    }
  }

  return result;
};

$('#submit').click(function onClick() {
  var output = getResult();
  var html = '';
  $.each(output,function(key,value){
      html +='<tr>';
      html +='<td>'+ value.username + '</td>';
      html +='<td>'+ value.email + '</td>';
      html +='<td>'+ value.skills + '</td>';
      html +='</tr>';
  });
$('table tbody').html(html);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="skills" type="text" placeholder="skills">
<input id="email" type="email" placeholder="mail id">
<input id="username" type="text" placeholder="username">
<input id="submit" type="submit" value="submit">

<br>

<table>
  <thead>
    <tr>
      <th>Username</th>
      <th>Email ID</th>
      <th>Core Skills</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

You can use Data-table jQuery plugin to generate table from jsondirectly like

$('#tableId').DataTable({
    data: jsonData,
    columns: [
        { data: 'username',title:'Username'},
        { data: 'emailId',title:'EmailId'}, 
        { data: 'skils',title:'Core Skills'}
    ],
    "search": {
      "caseInsensitive": false
    }
});

For More detail follow Data-table jQuery Plugin.

Here is the code

var data = [{
        "username": "John Doe",
        "email": "jn@gmail.com",
        "skills": "java,c,html,css"
    },
    {
        "username": "Jane Smith",
        "email": "js@gmail.com",
        "skills": "java,sql"
    },
    {
        "username": "Chuck Berry",
        "email": "cb@gmail.com",
        "skills": "vuejs"
    }
];

function BindDataToTable(d,obj){
  var keys=Object.keys(d[0]);
  var table=document.createElement("table");
  var trHead=document.createElement("tr");
  jQuery(keys).each((index,item)=>{
    var th=document.createElement("th");
    th.innerHTML=item;
    trHead.appendChild(th)
  })
   table.appendChild(trHead)
   for(var i=0;i<d.length;i++){
   var tr=document.createElement("tr");
  jQuery(keys).each((index,item)=>{
    var td=document.createElement("td");
    td.innerHTML=d[i][item];
    tr.appendChild(td)
  })
   table.appendChild(tr)
   }
   
   jQuery(obj).append(table);
}
BindDataToTable(data,"#tableElement")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="skills" type="text" placeholder="skills">
<input id="email" type="email" placeholder="mail id">
<input id="username" type="text" placeholder="username">
<input id="submit" type="submit" value="submit">

<div id="tableElement">
</div>

Related