Eric Evans
Eric Evans

Reputation: 670

Refresh data without removing whole table

I'm getting the results that I want, when I press the button it removes the whole table which I don't want.

Here's my jquery

$(document).ready(function() {
$('#button1').click(function(event) {
    $('#list').empty();
    $.ajax({
        url : 'http://98.199.64.63/api/eric_api.php?q=series',
        type : 'GET',
        dataType : 'json',
        success : function(data) {
            var table = $("#list");
            //table.html('');
            $.each(data, function(idx, elem) {
                table.append("<tr><td>" + elem.id + "</td>" + "<td>" + elem.user + 
                "</td>" + "<td>" + elem.email + "</td>" + "<td>" + elem.summary + "</td>" +
                "<td>" + elem.due_date + "</td>" + "<td>" + elem.problem_type + "</td>"+
                "<td>" + elem.status + "</td></tr>");
            });
        },
        error : function() {
            alert('There was an error');
        }
    });
});
});

And here is my html

<!DOCTYPE html>


<html>
<head>
    <title></title>
    <script src="js/jquery.js"></script>
    <script src="js/api_calls.js"></script>
    <link rel="stylesheet" href="css/normalizer.css" />
    <link rel="stylesheet" href="style.css" />
</head>

<body>
    <div>
        <table id="list">
            <tr>
                <th>ID</th>
                <th>User</th>
                <th>Email</th>
                <th>Summary</th>
                <th>Due Date</th>
                <th>Problem Type</th>
                <th>Status</th>
            </tr>
        </table>
    </div>
    <div>
        <button id="button1">Get Data</button>
    </div>
</body>
 </html>

I just want to data in the td tags to refresh and not the th tags. Any help thanks

Upvotes: 0

Views: 1303

Answers (2)

mishxpie
mishxpie

Reputation: 38

$('#list').empty(); <---- you did this so the whole table would be empty.

I would have and separate and when button click just empty the

    $('#list > tbody').empty();

Upvotes: 0

MonkeyZeus
MonkeyZeus

Reputation: 20737

Option 1

You can always just remove any TRs after the first one and then append the table with new data.

var table = $("#list");
table.find('tr:gt(0)').remove();

$.each(data, function(idx, elem) {
    table.append("<tr><td>" + elem.id + "</td>" + "<td>" + elem.user + 
    "</td>" + "<td>" + elem.email + "</td>" + "<td>" + elem.summary + "</td>" +
    "<td>" + elem.due_date + "</td>" + "<td>" + elem.problem_type + "</td>"+
    "<td>" + elem.status + "</td></tr>");
});

Option 2

Another option is to re-write your table with <thead> and <tbody>

HTML

<table id="list">
    <thead>
        <tr>
            <th>ID</th>
            <th>User</th>
            <th>Email</th>
            <th>Summary</th>
            <th>Due Date</th>
            <th>Problem Type</th>
            <th>Status</th>
        </tr>
    </thead>
    <tbody>
        <!-- Throw stuff in here -->
    </tbody>
</table>

JS

var table = $("#list");

var tr = '';

$.each(data, function(idx, elem) {
    tr += "<tr><td>" + elem.id + "</td>" + "<td>" + elem.user + 
    "</td>" + "<td>" + elem.email + "</td>" + "<td>" + elem.summary + "</td>" +
    "<td>" + elem.due_date + "</td>" + "<td>" + elem.problem_type + "</td>"+
    "<td>" + elem.status + "</td></tr>";
});

table.find('tbody').html(tr);

Upvotes: 1

Related Questions