6

I'm creating a registration page where someone can register up to 20 other people if they wanted to. So I have these text boxes:

First Name: <br/><input type="text" name="fname" id="fname" /> <br/>
Last Name: <br/><input type="text" name="lname" id="lname" /> <br/>
Email: <br/><input type="text" name="email" id="email"/> <br/>

This is my html table with my JQuery intialization of DataTables:

<div id="tables">
    <table id="table" border="1">
        <thead>
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
            </tr>
        </thead>
    </table>
</div>

$('#reg_more').dataTable({
    "bLengthChange": false,
    "bInfo": false
});

Now I want to put an add button so that the user can input the first and last name, and email and hit add, and it will be put into the table. How do I go about doing this?

3
  • 1
    @StephenSarcsamKamenar yah that's exactly what I did before posting, and I couldn't make heads or tails of the responses. I even tried a few solutions to no success. Which is why I came here so someone could explain a solution that I can understand. Google is always my first option Commented Sep 16, 2012 at 3:52
  • datatables.net/examples/api/add_row.html doesn't help? What part of that are you having trouble with? Commented Sep 16, 2012 at 3:53
  • @StephenSarcsamKamenar that is actually one of the codes I tried out. But after implementing and testing. I don't see how to use it. Do I have to create a button whose onclick event calls that function? Commented Sep 16, 2012 at 3:56

3 Answers 3

23
$(document).ready(function() {
    $('#example').dataTable();
    $('#addbtn').click(addrow);
} );

function addrow() {
    $('#example').dataTable().fnAddData( [
        $('#fname').val(),
        $('#lname').val(),
        $('#email').val() ] );

}

you need to call the addrow() in the button click.

Add this button in your html code

<input type="button" value="add" id="addbtn" />
Sign up to request clarification or add additional context in comments.

2 Comments

ahh much better, i'm brand new to jquery and datatables so it's very hard for me to do this kind of stuff. But this makes much better sense. So I can do this same thing for edit and remove. 1 more thing, how do I post all this data properly so i can add it to a mysql database? Let's say they register themselves and 20 other people, i need to add 21 entries to my mysql database.
to register, you can use the jQuery ajax to insert the data into the database.
6

Here is the way this is now done in DataTables 1.10

<input type="button" value="add" id="addbtn" />

var dTable = $('#example').DataTable();
$('#addbtn').on( 'click', function () {
    dTable.row.add([
        $('#fname').val(),
        $('#lname').val(),
        $('#email').val() 
    ]).draw();
});

Comments

0

This can also be helpful but not always at times. I have debugged this myself and will not take much time even if you have more data.

var table = $(".tableclassName")["1"]
var tableClone = $(".tableclassName")["3"]
$(yournewrowtext).insertAfter(table.children["1"].children[currentRowId]);
$(yourfirstcolumn).insertAfter(tableClone.children["1"]. 
children[currentRowId]);

Note: If you are not using fixed column then you can remove this line $(yourfirstcolumn).insertAfter(tableClone.children["1"].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.