admin管理员组文章数量:1434900
I am able to show the data returned from the ajax call as an alert, however I would like to show it in a table format using ejs
.
Can someone help?
AJAX Call
$('#submit').on('click', function() {
$.ajax({
url: '/render',
type: 'get',
dataType: 'json',
success: function(data) {
for (var i = 0; i < data.length; i++) {
alert(data[i].item);
}
}
});
});
HTML
<div class="container">
<div class="row">
<div class="col-md-4">
<label style="margin-left: 20px;">Shopping item</label>            
<label>Quantity</label>
</div>
</div>
<div class="row">
<div class="col-md-8">
<input type="text" id="item" name="item" />    
<input type="text" id="quan" name="quan" />    
<input type="submit" id="submit" value="Add item" style="background-color: #a3a3c2; color:white; width:170px;" />
</div>
</div>
<div class="row">
<div class="col-md-8">
<table>
<thead>
<th>Item</th>
<th>Quantity</th>
</thead>
<tbody id="tbody">
</tbody>
</table>
</div>
</div>
</form>
I am able to show the data returned from the ajax call as an alert, however I would like to show it in a table format using ejs
.
Can someone help?
AJAX Call
$('#submit').on('click', function() {
$.ajax({
url: '/render',
type: 'get',
dataType: 'json',
success: function(data) {
for (var i = 0; i < data.length; i++) {
alert(data[i].item);
}
}
});
});
HTML
<div class="container">
<div class="row">
<div class="col-md-4">
<label style="margin-left: 20px;">Shopping item</label>            
<label>Quantity</label>
</div>
</div>
<div class="row">
<div class="col-md-8">
<input type="text" id="item" name="item" />    
<input type="text" id="quan" name="quan" />    
<input type="submit" id="submit" value="Add item" style="background-color: #a3a3c2; color:white; width:170px;" />
</div>
</div>
<div class="row">
<div class="col-md-8">
<table>
<thead>
<th>Item</th>
<th>Quantity</th>
</thead>
<tbody id="tbody">
</tbody>
</table>
</div>
</div>
</form>
Share
Improve this question
edited Sep 19, 2017 at 7:25
mplungjan
179k28 gold badges182 silver badges240 bronze badges
asked Sep 19, 2017 at 6:47
M gowdaM gowda
2031 gold badge6 silver badges14 bronze badges
2 Answers
Reset to default 3try this
$('#submit').on('click',function() {
$.ajax({
url: '/render',
type: 'get',
dataType: 'json',
success: function(data) {
for(var i=0;i<data.length;i++)
{
var Html="<tr>
<td>"+data[i].item+"</td>
<td>"+data[i].Quantity+"</td>
</tr>";
$('#tbody').append(Html);
}
}
});
});
I'm not entirely sure what you are trying to achieve. But if you want to use ejs, then you should create an ejs template where you can pass the data.
Get your data and call the template with it;
$('#submit').on('click',function() {
$.ajax({
url: 'data.json',
type: 'get',
dataType: 'json',
success: function(data) {
var result = new EJS({url: 'productstemplate.ejs'}).render({products:data});
document.getElementById('product_list').innerHTML = result
}
});
});
I made a working sample of an ajax request, that uses an ejs to render in this plunkr sample, maybe this helps you out
本文标签: javascripthow to print this data in table format using ajaxStack Overflow
版权声明:本文标题:javascript - how to print this data in table format using ajax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745628044a2667095.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论