简体   繁体   中英

How to make href link clickable for cell table in javascript?

I have a table to show all the data from my API. My code is look like this:

<div class="table-responsive">
        <h1>List Existing Node</h1>
        <br/>
        <table class="table table-bordered table-striped" id="node_table">
            <tr>
                <th>Node Id</th>
                <th>Latitude</th>
                <th>Longitude</th>
                <th>Location</th>
            </tr>
        </table>
    </div>
<script>
    $(document).ready(function() {
        $.getJSON("/api/node/", function(data){
            var node_data = '';
            $.each(data, function(key, value){
                node_data += '<tr>';
                node_data += '<td>'+value.node_id;
                node_data += '<td>'+value.latitude;
                node_data += '<td>'+value.longitude;
                node_data += '<td>'+value.location;
                node_data += '</tr>';
            });
            $('#node_table').append(node_data);
            console.log(data);

        });
    });<script>

The problem is I want all of the cell table in Node Id column can be clicked using href link.

For example when I click a cell (ex: node 1 or node 2 or node n) in Node Id column and then the page will be redirect to https://facebook.com

How can I do that?

$('#node_table').on('click', 'tr', function() {
    var href = $(this).data('href');
    window.location.href = href;
})

$.getJSON("/api/node/", function(data){
    var node_data = '';
    $.each(data, function(key, value){
        node_data += '<tr data-href="your link here">';
        node_data += '<td>'+value.node_id;
        node_data += '<td>'+value.latitude;
        node_data += '<td>'+value.longitude;
        node_data += '<td>'+value.location;
        node_data += '</tr>';
    });
    $('#node_table').append(node_data);
    console.log(data);

});

You can do this:

<div class="table-responsive">
    <h1>List Existing Node</h1>
    <br/>
    <table class="table table-bordered table-striped" id="node_table">
        <tr>
            <th>Node Id</th>
            <th>Latitude</th>
            <th>Longitude</th>
            <th>Location</th>
        </tr>
    </table>
</div>
<script>
    $(document).ready(function() {
        $.getJSON("/api/node/", function(data){
            var node_data = '';
            $.each(data, function(key, value){
                node_data += '<tr>';
                node_data += '<td> <a href="https://facebook.com/">'+value.node_id+'</a></td>';
                node_data += '<td>'+value.latitude;
                node_data += '<td>'+value.longitude;
                node_data += '<td>'+value.location;
                node_data += '</tr>';
            });
            $('#node_table').append(node_data);
            console.log(data);

        });
    });<script>

Modify the ready callback as follows:

$.getJSON( '/api/node/', function(data){
    var node_data = '';
    var href = 'https://facebook.com';

    $.each(data, function(key, value){
        node_data += '<tr>';
        node_data += '<td> <a href="' + href + '">' + value.node_id + '</a></td>';
        node_data += '<td>' + value.latitude + '</td>';
        node_data += '<td>' + value.longitude + '</td>';
        node_data += '<td>' + value.location + '</td>';
        node_data += '</tr>';
    });
    $('#node_table').append(node_data);
});

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM