2
\$\begingroup\$

Using a java servlet, I've been doing the same approach to building or updating HTML. I create the basic table on the HTML page, then call for the body from the servlet with ajax.

As an example: HTML

    <div id="divLocationMain" style="margin-top: 2%;">
        <table style="width: 99%;">
            <thead>
                <tr>
                    <th>Clinic</th>
                    <th>Location</th>
                    <th>Clinic</th>
                    <th>Location</th>
                </tr>
            </thead>
            <tbody id="tbodyAdminLocation" style="font-size: small;">

            </tbody>
        </table> 
    </div>

javascript

function updateLocationTable() {
    $.ajax({
        url: 'TCAUServlet',
        type: 'POST',
        data: {formType: "getLocationTable"},
        dataType: 'html',
        success: function (responseText) {
            $('#tbodyAdminLocation').html('');
            $('#tbodyAdminLocation').html(responseText);
        },
        error: function (request, status, error) {
            alert("An error occurred.  Error:  " + status + ", " + error);
        }
    });
}

Servlet

private String getLocationTable() throws ClassNotFoundException, SQLException {
    List<Location> locList = TCAUDatabaseUtil.getLocations();
    Collections.sort(locList, new LocationComparator());
    String htmlString = "";
    for (int x = 0; x < locList.size(); x++) {
        Location loc = locList.get(x);
        htmlString += "                                    <tr>\n"
                + "                                        <td>" + loc.getLocationClinic() + "</td>\n"
                + "                                        <td>" + loc.getLocationName() + "</td>\n";
        if (x < locList.size() - 1) {
            loc = locList.get(++x);
            htmlString += "                                    <td>" + loc.getLocationClinic() + "</td>\n"
                    + "                                        <td>" + loc.getLocationName() + "</td>\n";
        }
        htmlString += "                                    </tr>\n";
    }
    return htmlString;
}

It works well, but I can't help but wonder if there isn't a better/easier/more efficient way, especially when I'm dealing with objects with quite a few fields in them.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ $('#tbodyAdminLocation').html(''); is redundant as html(content) will replace the innerHTML of element, there is no need to first empty it. In Java, no need to add spaces just to indent the code in htmlString. \n has no effect(just adds space) in DOM thus, can be removed. \$\endgroup\$ Commented Mar 8, 2017 at 15:23
  • \$\begingroup\$ @Tushar: Agreed, but it makes it easier to read if I need to do troubleshooting. And agreed about the redundant clearing of the old tbody. . \$\endgroup\$ Commented Mar 8, 2017 at 15:25
  • 1
    \$\begingroup\$ You can use a StringBuffer instead of all those concatenations. Maybe can you create an 'HtmlTable' class to hide the string création. And/Or use a Dom library. For the jQuery part, I have the feeling that there is a 'load' function to do that. \$\endgroup\$ Commented Mar 9, 2017 at 21:09

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.