简体   繁体   中英

Updating a table of data with ajax/JQuery

I am trying to update a table using data from ajax/json.

Here is JQuery Code: Updated to use functions for simpler execution.

$(document).ready(function() {
    //var userid = $( ".migrating" ).data( "userid" );

    function ajaxUpdate(userid){
        window.setInterval(function(){
         $.ajax({
                url: "info.php?userid=" + userid + "",
                async: true,
                type: "POST",
                data: "{}",

                contentType: "application/json; charset=utf-8",
                dataType: "json",

                success: function(results) {
                     $("#uid" + userid).html(results.uid);
                     $("#moved" + userid).html(results.moved);
                     $("#percentmoved" + userid).html(results.percentmoved);
                     $("#avgspeed" + userid).html(results.avgspeed);
                     $("#eta" + userid).html(results.eta);
                }
            });
        }, 3000); 
    }

    ajaxUpdate($(".migrating").data("userid"));

});

HTML Code:

<table>
    <tr>
        <td><b>ID</b></td>
        <td><b>Moved</b></td>
        <td><b>Moved %</b></td>
        <td><b>Avg Speed</b></td>
        <td><b>ETA</b></td>
    </tr>

    <tr class="migrating" data-userid="101">
        <td><div id="uid101">Loading...</div></td>
        <td><div id="moved101">Loading...</div></td>
        <td><div id="percentmoved101">Loading...</div></td>
        <td><div id="avgspeed101">Loading...</div></td>
        <td><div id="eta101">Loading...</div></td>
    </tr>

    <tr class="migrating" data-userid="102">
        <td><div id="uid102">Loading...</div></td>
        <td><div id="moved102">Loading...</div></td>
        <td><div id="percentmoved102">Loading...</div></td>
        <td><div id="avgspeed102">Loading...</div></td>
        <td><div id="eta102">Loading...</div></td>
    </tr>

</table>

i want to issue "ajaxUpdate()" on each instance of class="migrating" from the tables.

My info.php file simply outputs random strings and the the userid from the post string, the only problem is i need todo this for each of the fields.

I'm assuming the cause of the problem is having multiple class="migration" fields.

I've tried searching but couldn't find anything specific to multiple rows.

You can user jQuery.each to loop through the results. Something similar to this:

success: function(results) {
   $.each(results, function(index) {
       // your logic goes here.
   })
}

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