i bind json data to html table using Ajax,js. In my project i have 4 table headers(S.no,name,year,download link). first three columns sync and working without error.but,last column i need bootstrap download button with inserted link . how i do this using json?
exact what i need: if i load the page, s.no,name,year,bootstrap download button + inserted link i need.
html
<body>
<div class="container">
<div class="first bg-primary">
<h2>A R Rahman All Movies</h2>
<p>Download songs free</p>
</div>
<!-- -->
<table id="personDataTable" class="table table-dark table-hover">
<thead>
<tr>
<th>S.No</th>
<th>Movie Name</th>
<th>Year</th>
<th>Download Link</th>
</tr>
</thead>
</table>
<!-- table end -->
<script>
$.ajax({
//url: 'https://jsonplaceholder.typicode.com/posts',
url: 'https://10rs.000webhostapp.com/json-data.json',
type: "get",
dataType: "json",
success: function (data) {
drawTable(data);
}
});
function drawTable(data) {
for (var i = 0; i < data.length; i++) {
drawRow(data[i]);
}
}
function drawRow(rowData) {
var row = $("<tr />")
$("#personDataTable").append(row);
row.append($("<td>" + rowData.id + "</td>"));
row.append($("<td>" + rowData.name + "</td>"));
row.append($("<td>" + rowData.year + "</td>"));
row.append($("<td>" + rowData.link + "</td>"));
}
</script>
<footer class="bg-danger">
<p>Design and Developed by Rajadurai</p>
</footer>
</div>
<!-- container end -->
</body>
And this is the output of data
[
{
"id": 1,
"name": "Roja",
"year": "1992",
"link": "<a href="#" class="btn btn-info" role="button">Link Button</a>"
},
{
"id": 2,
"name": "Gentleman",
"year": "1993"
},
{
"id": 3,
"name": "Kizhakku Cheemayile",
"year": "1993"
},
{
"id": 4,
"name": "Pudhiya Mugam",
"year": "1993"
},
{
"id": 5,
"name": "Thiruda Thiruda",
"year": "1993"
}
]
你可以用这个
row.append($("<td><a href='" + rowData.link + "'><button class='btn'>Go</button></a></td>"));
you have to modify the link in the data by putting just the link or by replacing the double cote (") with simple (')
1- if you want to put just the link in your data
// modify your code like this
$.ajax({
//url: 'https://jsonplaceholder.typicode.com/posts',
url: 'https://10rs.000webhostapp.com/json-data.json',
type: "get",
dataType: "json",
success: function (data) {
drawTable(data);
}
});
function drawTable(data) {
for (var i = 0; i < data.length; i++) {
drawRow(data[i]);
}
}
function drawRow(rowData) {
var row = $("<tr />")
$("#personDataTable").append(row);
row.append($("<td>" + rowData.id + "</td>"));
row.append($("<td>" + rowData.name + "</td>"));
row.append($("<td>" + rowData.year + "</td>"));
row.append($("<td><a href='" + rowData.link + "' class='btn btn-info' role='button'>Link Button</a></td>"));
}
2- if you want to replace the double cote (") with simple cote (') leave your code without changing and it will be functional
Finally i got my complete answer and project running well.
HTML
<div class="container">
<div class="header">
<h2>A R Rahman All Movie Songs</h2>
<p>Download songs free</p>
</div>
<!-- header end -->
<div class="table-responsive-md">
<table id="personDataTable" class="table table-dark">
<thead>
<tr>
<th>S.No</th>
<th>Movie Name</th>
<th>Year</th>
<th>Download</th>
</tr>
</thead>
</table>
</div>
<!-- table end -->
<footer class="bg-success">
<p style="color:#ffffff;">Design and Developed by
<span style="color:red;background-color:yellow;">Rajadurai</span>
</p>
</footer>
</div>
JAVASCRIPT
<script>
$.ajax({
//url: 'https://jsonplaceholder.typicode.com/posts',
url: 'https://10rs.000webhostapp.com/ar/json-data.json',
//url: 'json-data.json',
type: "get",
dataType: "json",
success: function (data) {
drawTable(data);
}
});
function drawTable(data) {
for (var i = 0; i < data.length; i++) {
drawRow(data[i]);
}
}
function drawRow(rowData) {
var row = $("<tr />")
$("#personDataTable").append(row);
row.append($("<td>" + rowData.id + "</td>"));
row.append($("<td>" + rowData.title + "</td>"));
row.append($("<td>" + rowData.year + "</td>"));
row.append($("<td><a href='" + rowData.link + "' class='btn btn-info' role='button' target='_blank'>Download</a></td>"));
}
</script>
JSON
[{
"id": 1,
"title": "Roja",
"year": "1992"
},
{
"id": 2,
"title": "Gentleman",
"year": "1993"
},
{
"id": 3,
"title": "Kizhakku Cheemayile",
"year": "1993"
},
{
"id": 4,
"title": "Pudhiya Mugam",
"year": "1993"
},
{
"id": 5,
"title": "Thiruda Thiruda",
"year": "1993"
}]
OUTPUT
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.