Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX: add new row to table or remove using AJAX

This is the logic: I input something to form, and the form is AJAX live search. after I found value, I click on add button and it creates new row in existing table / tbody.

<table class="standard">
    <thead>
        <tr>
            <td colspan="2">
                Start Input barcode / Product Name
            </td>
            <td colspan="4">
                <input type="text" size="90" value="" placeholder="Barcode / Product Name">
            </td>
            <td>
                <button class="tambah"><i class="icon-plus"></i> Add</button>
            </td>
        </tr>

        <tr>
            <td>
                No.
            </td>
            <td>
                Kode Barang
            </td>
            <td>
                Nama Barang
            </td>
            <td>
                Qty
            </td>
            <td>
                Harga
            </td>
            <td>
                Disc %
            </td>
            <td>
                Total
            </td>
        </tr>
    </thead>
    <tbody>

    <!-- when button add is click that will add <tr></tr> here -->
    </tbody>
</table>

can i do that? if so, how?

Fiddle Example: http://jsfiddle.net/anggagewor/cauPH/

like image 210
Anggagewor Avatar asked Sep 25 '13 06:09

Anggagewor


People also ask

What is the use of AJAX () method?

The ajax() method is used to perform an AJAX (asynchronous HTTP) request. All jQuery AJAX methods use the ajax() method. This method is mostly used for requests where the other methods cannot be used.

How do I add a row to a table in button click?

To add a row, define a variable that keeps the count of the total number of that now exists in the table. Then we will use the jQuery “click” event to detect a click on the add row button and then use the . append() method of jQuery to add a row in the table.

How append AJAX response to dataTable?

First store the dataTable object in a variable, var dataTable = $("#viewTable"). DataTable(); Then on ajax success, use dataTable.


1 Answers

Try this

var scntDiv = $('#p_scents');
var i = $('#p_scents tr').size() + 1;

$('#addScnt').click(function() {
    scntDiv.append('<tr><td><select name="type" id="type"><option value="Debit">Debit</option><option value="Credit">Credit</option></select></td><td><select name="accounts" id="accounts"><option value="">SELECT</option><option value="One">One</option><option value="Two">Two</option></select></td><td><input type="text" name="debit_amount" id="debit_amount"/></td><td><input type="text" name="credit_amount" id="credit_amount"/></td><td><a href="#" id="remScnt">Remove</a></td></tr>');   
    i++;
    return false;
});

//Remove button
$(document).on('click', '#remScnt', function() {
    if (i > 2) {
        $(this).closest('tr').remove();
        i--;
    }
    return false;
});​

Here's a working example, including remove row functionality: DEMO.

like image 79
Padmanathan J Avatar answered Sep 27 '22 18:09

Padmanathan J