Roch
Roch

Reputation: 22061

Adding a row to a table dynamically

$(document).ready(function(){
var refreshId = setInterval(function() {
    periodicRefresh();
}, 9000);
})

.

function periodicRefresh()
{
    $.ajax({
        type: 'GET',
        url: 'include/ajaxActions.php',
        data: "action=displayLastEvent",

        success: function(msg){
            var newid = $(msg).attr("id");
                    current = $("#list tr:first").get(0).id;
                    if(newid != currentfirstrow){
                        $("#list").prepend(msg);
                lastrow = current-19;
                $('#'+lastrow).remove();
                    }
        }
    });
}

Here is an example of what the table row looks like:

<tr id="5094"  style="background:White;">
  <td class="tdstd">
  </td>
</tr>

Upvotes: 0

Views: 1042

Answers (2)

WesleyJohnson
WesleyJohnson

Reputation: 1548

That's a little hard to answer, without knowing how the XML that is returned from "ajaxActions.php" and your AJAX call is formatted. Also, I'm assuming that "currentfirstrow" is a global javascript variable accessible inside any function?

function periodicRefresh()
{
    $.ajax({
        type: 'GET',
        url: 'include/ajaxActions.php',
        data: "action=displayLastEvent",

        success: function(xml){
            var newfirstrow = $(xml).find("name_of_xml_node_that_contains_newly_returned_id").text();
            if(newfirstrow != currentfirstrow){
                //do add new row()
            }
        },
}

Upvotes: 3

kgiannakakis
kgiannakakis

Reputation: 104196

At first ids aren't allowed to be numbers. They should start with a letter, then followed by a number. So you need to follow a different naming convention for your ids.

I am not sure if that's what you are asking for, but you can get the id of the first row with something like this:

$("table tr:first").get(0).id

Upvotes: 1

Related Questions