使用jQuery在html表中显示json数据

时间:2018-09-19 05:58:36

标签: javascript jquery

如何使用jQuery在html表中显示json数据?以及如何在搜索结果时删除区分大小写的内容?

预期输出

enter image description here 如何在表格中显示结果?我该如何实现?

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 个答案:

答案 0 :(得分:1)

您需要创建一个表,并需要使用以下代码将即将到来的数据追加到该表中:-

$('#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);
});

要进行不区分大小写的比较,请使用.toUpperCase()

工作片段:-

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>

答案 1 :(得分:0)

这是代码

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>

答案 2 :(得分:0)

您可以使用数据表jQuery插件直接从json生成表,如

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

有关更多详细信息,请遵循数据表jQuery插件。