Last active
January 11, 2019 12:22
-
-
Save Kuanlin-Chen/c1c799f55c6f2a859101a7bb449d13ed to your computer and use it in GitHub Desktop.
Generate Bootstrap Table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* BootstrapTableBuilder bsTable = new BootstrapTableBuilder(); | |
* bsTable.addTableHeader("Weight Table", 3, "Age", "Name", "Weight"); | |
* bsTable.addRowValues("26", "Corey", "75"); | |
* bsTable.addRowValues("25", "Daisy", "60"); | |
* bsTable.addRowValues("30", "Kevin", "100"); | |
* String table = bsTable.build(); | |
*/ | |
public class BootstrapTableBuilder { | |
private int columns; | |
private final StringBuilder table = new StringBuilder(); | |
private String DOC_TYPE = "<!DOCTYPE html>"; | |
private String HTML_START = "<html>"; | |
private String HTML_END = "</html>"; | |
private String HEAD_START = "<head>"; | |
private String HEAD_END = "</head>"; | |
private String META_START = "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>"; | |
private String LINK = "<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css\" integrity=\"sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==\" crossorigin=\"anonymous\">"; | |
private String META_END = "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"; | |
private String BODY_START = "<body>"; | |
private String BODY_END = "</body>"; | |
private String DIV_CONTAINER_START = "<div class=\"container \">"; | |
private String DIV_CONTAINER_END = "</div>"; | |
private String H1_START = "<h1>"; | |
private String H1_END = "</h1>"; | |
private String TABLE_START = "<table class=\"table table-bordered table-striped table-hover\">"; | |
private String TABLE_END = "</table>"; | |
private String TR_DANGER_START = "<tr class=\"danger\">"; //red | |
private String TR_INFO_START = "<tr class=\"info\">"; //blue | |
private String TR_WARNING_START = "<tr class=\"warning\">"; //yellow | |
private String TR_END = "</tr>"; | |
private String TH_START = "<th>"; | |
private String TH_END = "</th>"; | |
private String TD_START = "<td>"; | |
private String TD_END = "</td>"; | |
public BootstrapTableBuilder() { | |
table.append(DOC_TYPE); | |
table.append(HTML_START); | |
table.append(HEAD_START); | |
table.append(META_START); | |
table.append(LINK); | |
table.append(META_END); | |
table.append(HEAD_END); | |
table.append(BODY_START); | |
table.append(BODY_END); | |
table.append(HTML_END); | |
} | |
/** | |
* @param values | |
*/ | |
public void addTableHeader(String header, int columns, String... values) { | |
this.columns = columns; | |
if (values.length != columns) { | |
System.out.println("Error column lenth"); | |
} else { | |
int lastIndex = table.lastIndexOf(BODY_END); | |
if (lastIndex > 0) { | |
StringBuilder sb = new StringBuilder(); | |
sb.append(DIV_CONTAINER_START); | |
sb.append(H1_START+header+H1_END); | |
sb.append(TABLE_START); | |
sb.append(TR_INFO_START); | |
for (String value : values) { | |
sb.append(TH_START); | |
sb.append(value); | |
sb.append(TH_END); | |
} | |
sb.append(TR_END); | |
sb.append(TABLE_END); | |
sb.append(DIV_CONTAINER_END); | |
table.insert(lastIndex, sb.toString()); | |
} | |
} | |
} | |
/** | |
* @param values | |
*/ | |
public void addRowValues(String... values) { | |
if (values.length != columns) { | |
System.out.println("Error column lenth"); | |
} else { | |
int lastIndex = table.lastIndexOf(TR_END); | |
if (lastIndex > 0) { | |
int index = lastIndex + TR_END.length(); | |
StringBuilder sb = new StringBuilder(); | |
if(values[2].equals("100")){ | |
sb.append(TR_DANGER_START); | |
}else{ | |
sb.append(TR_WARNING_START); | |
} | |
for (String value : values) { | |
sb.append(TD_START); | |
sb.append(value); | |
sb.append(TD_END); | |
} | |
sb.append(TR_END); | |
table.insert(index, sb.toString()); | |
} | |
} | |
} | |
/** | |
* @return | |
*/ | |
public String build() { | |
return table.toString(); | |
} | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment