10

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/

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Anggagewor
  • 167
  • 1
  • 2
  • 15

4 Answers4

10

You can find the pseudo code below.

$('#button_id').on('click', function(e) {
    $.ajax({
        url : yourUrl,
        type : 'GET',
        dataType : 'json',
        success : function(data) {
            $('#table_id tbody').append("<tr><td>" + data.column1 + "</td><td>" + data.column2 + "</td><td>" + data.column3 + "</td></tr>");
        },
        error : function() {
            console.log('error');
        }
    });
});
8

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.

Padmanathan J
  • 4,614
  • 5
  • 37
  • 75
0
$("<tr><td>.....content...<td><a class='remove'>remove</a>").appendTo("#tableid tbody").find('.remove').click(function () {
    $(this).parent().parent().remove();
});
ledzep2
  • 801
  • 7
  • 12
0

In your ajax response you can do this

$("#myTable > tbody").append('<tr><td>my data</td><td>more data</td></tr>');

'#myTable' will be replaced by your table id or class and <td>my data</td><td>more data</td> will be replaced by your content

Suvash sarker
  • 3,140
  • 1
  • 18
  • 21