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

标签 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">

最佳答案

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

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

关于javascript - 使用jQuery在html表格中显示json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52399080/

相关文章:

javascript - JavaScript "save"如何通过变量赋值?

php - 使用ajax和php上传文件

javascript - 在没有节点服务器的情况下使用 angular-cli 在 Chrome 上运行本地构建的 angular 2 应用程序

javascript - Fancybox: 无法加载请求的内容。请稍后再试

"border-radius"值的 Javascript/jQuery 正则表达式

javascript - ionic : Disallow all emoticons

javascript - 如何编写一个函数,通过返回 boolean 值来检查数组是否包含数字

javascript - 如何让联系我们提交按钮发送电子邮件到我的电子邮件?

jquery - 我的 header 收缩转换未能触发我错过了什么?

php - CSS/PHP 用 div 填充屏幕