• Post Reply Bookmark Topic Watch Topic
  • New Topic

Populate a dynamic table with data using JQuery

 
alex.j romano
Greenhorn
Posts: 3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

First time on the forum, nice to meet ya.

I've encountered a problem when trying to dynamically add rows to a table and populating them with data taken from a database. I retrieved a list of objects from the database where each object represents a row, a row that I try to add into the table. Now, I know how to add a row using jquery $('#usersTable').append('<tr>..</tr>') and that works fine.

My question is how I can insert the data from the value-object into the script?
I added in the example what I tried to do, but the browser simple becomes unresponsive and tells me I need to shut down the script. It happened (browser freezing) when I tried it on firefox, explorer and chrome.

Any ideas?

Thanks in advance,

Alex.

I shrunk down the example to include only one field.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

A few things:
  • Java code in a JSP is a poor practice that has been discredited for almost 10 years now. Why are you not using the JSTL and EL?
  • As this is an HTML forum, server-side markup is usually not helpful. What's generally more important is the generated HTML. If the question concerns the server-side markup in the JSP, then the question should be in the JSP forum.
  • Because you are using JSP, why are you waiting until after the page is loaded to create elements using jQuery? Why aren't you just generating them on the server using JSP mechanisms?
  •  
    alex.j romano
    Greenhorn
    Posts: 3
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank's Bear.

    As for your questions:
    1. I started using servlets and jsp on my own, not a while ago. Must admit that I'm not familiar with JSTL and EL. I'll look it up.

    2. Alright. Although my problem is more with the javascript involved...

    3. My design for the page, is to allow the manager to enter a user name into the input field, search for all possible matches in the database and then, add rows to the table according to the number of matches found, each with his own details.
    Are you suggesting to create the whole table in the servlet ?

    Alex
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65524
    105
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    alex.j romano wrote:
    1. I started using servlets and jsp on my own, not a while ago. Must admit that I'm not familiar with JSTL and EL. I'll look it up.

    You are definitely starting off on the wrong foot. Java code in JSPs has been discredited since the introduction of JSP 2 in 2002. You need to find yourself an up-to-date tutorial that emphasizes the use of the JSTL and EL in modern JSPs rather than teaching scriptlets as if it were still 2001.

    2. Alright. Although my problem is more with the javascript involved...

    I'm not sure based upon the discussion of question 3.

    3. My design for the page, is to allow the manager to enter a user name into the input field, search for all possible matches in the database and then, add rows to the table according to the number of matches found, each with his own details.
    In this same page? You realize that the Java code in the page is executed before the page is displayed, right?

    I strongly recommend that people just learning servlets and JSP read the following articles:
  • The Secret Life of JSPs
  • The Front Man


  • After that, we should back up a bit and approach the problem from a higher level. Start by outlining the steps that you'd like to accomplish, and then we can discuss if the approach you started out with is appropriate or not.
     
    alex.j romano
    Greenhorn
    Posts: 3
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Did some reading. Nice articles.

    Java code in JSPs has been discredited since the introduction of JSP 2 in 2002

    Oh well. Better late then never, right?

    In this same page? You realize that the Java code in the page is executed before the page is displayed, right?

    I did realize. Planned to solve it by checking if it's null or not.

    Found some promising jstl examples (Which means this post should be moved to JSP forum... )
    But in case I want to leave the sql select in a servlet and not in the jsp: (see example)


    How should I retrieve the info and use it as part of the tag?


    Best Regards,

    Alex
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65524
    105
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Moved to JSP, as requested.

    And right, the <sql:> tags are for prototyping and shouldn't ever be used in real code. I just pretend that they don't exist and I suggest that you should too.

    You should fetch the entries from the DB in a model class that the page controller calls. Copy the entries to a Java collection before passing them out of the model -- never returns a result set to the controller or presentation layers.

    The page controller can then place the collection in request scope where the JSP can iterate over it using JSTL and EL.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!