2

I am able to display the table but the headers are bit misaligned and shifted to right. Can someone suggest what can be missing here.

Current o/p: (Header are misaligned)

                Server  name ID
S1 ABC 43
S2 XYZ 67

Code:

out.append("<table id=\"dtable\" class=\"tablesorter\">");
out.append("<thead>");
out.append("<tr>");
out.append("<th>Server</th>");
out.append("<th>name</th>");
out.append("<th>ID</th>");
out.append("</tr>");
out.append("</thead>");
out.append("<tbody>");

for (String s : returns) {
  String[] s2=s.split("-");

  out.append("<tr>");
  out.append("<td>");
  for(String results : s2)
  {

    out.append(results);
    out.append("&nbsp;");
  }
  out.append("</td>");
  out.append("</tr>");
} 

out.append("</table>");
3
  • Do not, under any circumstances, do this in code. You should not be creating markup in Java. Use a templating solution (e.g. FreeMarker, Mustache, JSF with JSTL, etc.) with CSS. Commented Aug 6, 2017 at 13:53
  • it seems you are missing out.append("</tbody>"); after the end of the loop Commented Aug 6, 2017 at 13:56
  • Added the out.append("</tbody>") but still misaligned Commented Aug 6, 2017 at 14:12

2 Answers 2

1

Try to add every string to separated column td like :

for (String s : returns) {
    String[] s2=s.split("-");

    out.append("<tr>");
    for(String results : s2)
    {
        out.append("<td>");
        out.append(results);
        out.append("&nbsp;");
        out.append("</td>");
    }
    out.append("</tr>");
} 

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

1

Here your creating a row and closing it “s2” times please check this:

    out.append("<tr>");
    out.append("<td>");
    for (String results : s2) {

        out.append(results);
        out.append("&nbsp;");
    }
    out.append("</td>");
    out.append("</tr>");
}

And there is no tbody close tag

3 Comments

Remember to give a comment when disagree 😉
This section of the table displays data fine. It is only the table headers that are right aligned.
This code will leads you to the invalid HTML too.. The right way to append nested HTML elements is what you looking, you'll find it in the other answer @user7471208.

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.