how to use some html element tag inside the json v

2019-08-12 14:17发布

问题:

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"
}
]

回答1:

You could use this

row.append($("<td><a href='" + rowData.link + "'><button class='btn'>Go</button></a></td>"));


回答2:

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



回答3:

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

Click this link to view output