how to select an element from json array and display on html using nodejs

喜夏-厌秋 提交于 2019-12-19 10:52:06

问题


I have a table in DynamoDB local named userGroup. The items are as follows

{
    "Items": [{
        "id": "A004",
        "name": "ACC LR2",
        "userId": ["1", "2", "3", "4"],
        {
            "id": "0001",
            "name": "ABG IT",
            "userId": ["8", "9", "10", "11"]
        }
    }]
}

There are more than 20 lines like this.
This is an entire row

    {
        "id": "A004",
        "name": "ACC LR2",
        "userId": ["1", "2", "3", "4"]
}

this is my routes users.js

router.get('/groups', function (req, res, next) {
    var params = {
        TableName: 'userGroup',
    };
    dynamodb.scan(params, function (err, data) {
        if (err) {
            console.log(err);
        } else {
            console.log("List of Groups: " + console.log(JSON.stringify(data.Items)));
            res.render('groups', {
                _uG: data.Items
            });
        }
    });
});

my group.html is like this

<table>
<thead>
<tr role="row">
<th>Id</th>
<th>User ID</th>
<th>Name</th>
<th>Details</th>
</tr>
</thead>

<tbody>     
<% for(var i = 0; i < _uG.length; i++) { %>                           
<tr>
<td ><%= _uG[i].id.S %></td>            
<td><%= _uG[i].name.S %></td>
<td><a href="/users/groups/group-info?<%= _uG[i].id.S %>">Details</a></td>
</tr>
<% } %>
</tbody>
</table>

this displays values of ID and name and details in a list format in every <td> on the group.html page. NOW when i click on details i should go to group-info page which would display only the all details of only userId from dynamoDB only for that particular row which is selected.

my second routes in users.js

    router.get('/groups/group-info', function (req, res, next) {
        console.log("id " + req.query.id);
        var params = {
            TableName: 'userGroup',
        };
        console.log("PARAMS: " + JSON.stringify(params));
        dynamodb.scan(params, function (err, data) {
            if (err) {
                console.log(err);
            } else {
                // console.log("These are Groups: "+ console.log(JSON.stringify(data.Items)));
                console.log("User DETAILS: " + console.log(JSON.stringify(data.Items)));
                res.render('group-info', {
                    _userD: data.Items
                });
            }
        });
    });

HTML page table 2 for displaying details of data pertaining to is like

<table>
<thead>
<tr role="row">
<th>Id</th>
<th>User ID</th>
<th>Name</th>
<th>Details</th>
</tr>
</thead>

<tbody>     
<% for(var i = 0; i < _uG.length; i++) { %>                           
<tr>
<td ><%= _uG[i].id.S %></td>            
<td><%= _uG[i].name.S %></td>
<td><a href="/users/groups/group-info?<%= _uG[i].id.S %>">Details</a></td>
</tr>
<% } %>
</tbody>
</table>

So what do I do in group-info.js page to display userId of selected <td> of particular row.??

Help is appreciated ..

来源:https://stackoverflow.com/questions/33678695/how-to-select-an-element-from-json-array-and-display-on-html-using-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!