Monday, May 4, 2009

Using jQuery - Add row to table

During this days some people ask me about creating dynamic data entry ui . I try to explain it with a real Example step by step .

First Draw an HTML Table with two rows , header and a row with text boxes for data Entry .


Remember that last textbox in the second row must have a lastElement css class.
The simplest way for add a new row to a table is .append() method , but in this approach you should know the new row structure .Using .clone() method make it more easy and flexible. Now the only thing to do is getting the last row of table and call .clone() method on it.

The clone method gets an optional Boolean parameter to clone selected DOM elements with all event handlers or not .

OK , you got cloned element and now you should insert it at the end of the table using .insertAfter() method.



put this line of code to a function and call it on press enter on last text box of each row.
you should handle keypress event of last input that has lastElement class.



The condition "e.which == 13" checks if pressed key is Enter or not.
Test your code ... yes it works , but a little problem , if you enter some data in text boxes clone() method copy text boxes with their values and its not good . select the new added row (now is a last row in table) and select all inputs and clear their values .


Another point is focus must be set to first text box of new row , do it ...



I try to keep this post simple , but if you know how to do ajax save , you can add a method to save last row before add new row in UI .

and here is the completed code ...

10 comments:

  1. Hi my dear friend

    another interesting post from you.

    have you looked at this project before:
    http://gears.google.com/

    it may be interesting for you.

    ReplyDelete
  2. yes ... google do amazing jobs at all.
    thanks for your attention to my blog dear friend .

    ReplyDelete
  3. Salaam,
    this code works fine and it is a nice example.

    However I see a problem when I want to hit "submit". I would want each input item uniquely identified by an id, e.g.:

    id="name1" id="family1" id="age1"
    ....
    id="namex" id="familyx" id="agex"

    Would it be fairly easy to add these attributes to the HTML?

    Also a rowCount variable:

    &nblt;input type="hidden" name="count" value="x"&nbgt;

    What do you think?

    ReplyDelete
  4. Thanks for your comment .
    you can add id attribute to each input by using $([your object]).attr("id",[a unique number])
    and you can make it unique by rowCount or a code like this :
    $("input").attr("id", function (arr) {
    return "input_" + arr;
    });
    but jQuery recommend using cssclass instead of id .
    I try to post another sample and show how to use cssClass instead of id .

    ReplyDelete
  5. hello,
    nice post, very interesting
    could you please explain what should save() do?

    ReplyDelete
  6. a method that have ajax methods of jquery to send new row data to the server for save in db.

    I explain it in other post .

    thanks for your intrest

    ReplyDelete
  7. Very good indeed. Nice...
    I have more than one table in my document but this code only picks the last table in my document. I tried $.each(".tableClass tr:last", function(){...}); but it doesn't work also. I need help because I am fairly new to jquery. Thank you.

    ReplyDelete
  8. Very Useful post...thanks iman...keep it up man.....

    ReplyDelete
  9. Interesting, but the code is images, it can't be copied! :(

    ReplyDelete