插入来自数组元素表中的jQuery / JS [英] insert elements from array to table in jquery/js

查看:147
本文介绍了插入来自数组元素表中的jQuery / JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为reservations.js一个js文件,在这个文件中我是有保留的数组,如:

I have a js file called reservations.js, in this file I have an array of reservations, like:

var reservations = [
  { "HotelId": "01", "HotelName": "SPA", "ReservNum": "0166977", "Guest Name": "Jonny", "Room": null, "Type": "SUIT", "Rooms": "1", "Board": "BB", "Status": "IH", "Pax": "2,0,0,0", "Arrival": "07/08/12", "Departure": "09/08/12", "AgentDesc": "FIT", "AgentCode": "FIT", "Group": null, "Balance": "0", "Requests": "", "Remarks": null, "Fit/Group": "FIT", "ReturnGuestName": "", "StatusColor": "LightCoral" },
  { "HotelId": "01", "HotelName": "SPA", "ReservNum": "H000192", "Guest Name": null, "Room": null, "Type": "Folio", "Rooms": "0", "Board": "", "Status": "IH", "Pax": "0,0,0,0", "Arrival": "07/08/12", "Departure": "10/09/12", "AgentDesc": "movies", "AgentCode": "001", "Group": null, "Balance": "0", "Requests": "", "Remarks": "", "Fit/Group": "FIT", "ReturnGuestName": "", "StatusColor": "LightCoral" }
];

我需要做的是创建一个表(HTML)6 colomns:RES。号,客户名称,状态,到达日期,出发日期,房间类型。
及该数组中的元素插入到表中的匹配栏第

What I need to do is to create a table(in html) with 6 colomns: Res. Number, Guest Name, Status, Arrival Date, Departure Date, Room Type. and insert the element from the array into the matching col in the table.

实施例:ReservNum:0166977,那么数0166977将在
  第一个关口水库。号。

Example: ReservNum": "0166977", So the number 0166977 will be in the first col Res. Number.

我的表是这样的:

<table id="reservations">
        <thead>
            <tr>
                <th>Res. Number</th><th>Guest Name</th><th>Status</th><th>Arrival Date</th><th>Departure Date</th><th>Room Type</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>resnum</td><td>guestname</td><td>status</td><td>arrivaldate</td><td>departuredate</td><td>roomtype</td>
            </tr>
        </tbody>
    </table>

我不知道该怎么做。
我尝试做财产以后像这样的js文件:

I don't know what how to do it. I have try to do somthing like this in the js file:

$('#reservations tr').each(function (i) {
    $(this).find('td').html(reservations[i]);
});

但它不是工作。(也许我的HTML表格错误或JS的,甚至是两者)

But it is not works.(maybe my html table is wrong or the js, or even both of them)

我在JS / jQuery的,所以我有点不确定我在做什么新鲜事。
谢谢大家!

I'm new in js/jquery so i'm a little unsure what I'm doing. Thank You All!!!

推荐答案

类似下面( 检查工作演示

var tbody = $('#reservations tbody'),
    props = ["ReservNum", "Guest Name", "Status", "Arrival", "Departure", "Type"];
$.each(reservations, function(i, reservation) {
  var tr = $('<tr>');
  $.each(props, function(i, prop) {
    $('<td>').html(reservation[prop]).appendTo(tr);  
  });
  tbody.append(tr);
});

这篇关于插入来自数组元素表中的jQuery / JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆