javascript - 如何在 jQuery 中使用行和列索引创建表?

标签 javascript jquery html json

我的 JSON 看起来像这样:-

{
"tblDetails": [{
    "id": 1,
    "rowIndex": 1,
    "colIndex": 1
}, 
{
    "id": 2,
    "rowIndex": 1,
    "colIndex": 2
}, 
{
    "id": 3,
    "rowIndex": 1,
    "colIndex": 4
}, 
{
    "id": 4,
    "rowIndex": 1,
    "colIndex": 6
}
]}

HTML 结构如下:-

<table id="tblLayout"><tbody> </tbody></table>

我正在使用 jQuery ajax 来获取 JSON 数据。

 $.ajax(
 { 
   url : "/hello/data.json",
   type: "GET",
   success: function(data)
   {
       var tbl  = data.tblDetails;
       tbl.forEach(item)
       {
       var html = item.id;
       $('#tblLayout> tbody > tr:eq('+(item.rowIndex)+') td:eq('+(item.colIndex)+')').append(html);
       }

   },
   error:function(e)
   {

   }
 })

现在如何在表 tblLayout 中的指定行列附加 html。

最佳答案

我不确定这是否是确切的解决方案,但这是我为所需布局所做的。

我用 ul 替换了表:-

<ul id="tblLayout"></ul>

jQuery 技术

$.ajax(
{ 
url : "/hello/data.json",
type: "GET",
success: function(data)
{
   var tbl  = data.tblDetails;
   tbl.forEach(item)
   {
   var html ="<li class='seat style='position:absolute;top:" + (item.rowIndex * 50) + "px;left:" + (item.colIndex * 50) + "px;'>" + item.id+ "</li>";

   $('#tblLayout>).append(html);

   }
   },
  error:function(e)
  {

  }
  })

关于javascript - 如何在 jQuery 中使用行和列索引创建表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34176146/

相关文章:

javascript - chrome事件选项卡url传递到php文件

javascript - HTML 未从 JavaScript 脚本更改

html - 在隐藏范围内搜索文本 (Ctrl+F)

javascript - Jquery mouseleave setTimeout 不起作用

javascript - PHP 到 Javascript 数组的麻烦

php - 使用 JSON 和 PHP5 的 Kendo Grid

javascript - '未捕获的语法错误 : Unexpected Token }' when trying to access a function

javascript - 当ajax不返回任何内容时隐藏div

php - jQuery + JSON + Open flash chart,如何即时创建图表?

iphone - Mobile Safari 浏览器 - 视口(viewport)问题