根據類名與列表返回table

.qtipTable {
	margin: 0;
	padding: 0;
	font-size: 12px;
}
.qtipBody {
	border-right: 1px solid #b5daff;
	border-top: 1px solid #b5daff;
    font-family: 微軟雅黑,宋體,tahoma, Arial, Verdana, sans-serif;
    font-size: 12px;
}
.qtipBody th {
	border-left: 1px solid #b5daff;
	border-bottom: 1px solid #b5daff;
	vertical-align: middle;
	background-color: #d0e8ff;
	text-align:center;
	width:100px;
}
.qtipBody td {
	border-left: 1px solid #b5daff;
	border-bottom: 1px solid #b5daff;
	color: black;
}
.money_right{
	text-align:right;
	padding-right:2px;
}

	/**
	 * 根據list 返回table
	 * @param titlenm 標題名稱
	 * @param pType Class
	 * @param pList 結果集
	 * @param pFieldNames 列名
	 * 例如:makeHTMLTable("rsvmain.title.rsvoccudtl", Rsvoccudtl.class, rsvoccudtlList, "blockId","rmcnt")
	 * @return
	 */
	public <T> String makeHTMLTable(String titlenm, Class<T> pType, List<T> pList, String... pFieldNames)
			throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException,
			NoSuchMethodException {
		String wFuncnm, wResult = "<div style='margin-bottom: 5px;'><span style='font-size:14px;font-weight:bold;'>"
				+ ApplicationContextUtil.getMessage(titlenm)
				+ "</span></div><div class='qtipTable'><table class='qtipBody' cellspacing='0' border=0 width='100%'><tr>";
		//獲得Class
		String wClassnm = pType.getName().substring(pType.getName().lastIndexOf(".") + 1);
		//遍歷標題
		for (String fnm : pFieldNames) {
			wResult += "<th>" + ApplicationContextUtil.getMessage(wClassnm + "." + fnm) + "</th>";
		}
		wResult += "</tr>";
		Iterator<T> iterator = pList.iterator();
		while (iterator.hasNext()) {
			wResult += "<tr>";
			T t = (T) iterator.next();
			for (String fnm : pFieldNames) {
				wFuncnm = "get" + fnm.substring(0, 1).toUpperCase() + fnm.substring(1);
				String tmpString = pType.getMethod(wFuncnm).invoke(t).toString();
				//判斷如果是數字,則右對齊
				if (tmpString.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$")) {
					wResult += "<td class='money_right'>" + tmpString + "</td>";
				} else {
					wResult += "<td>" + tmpString + "</td>";
				}
			}
			wResult += "</tr>";
		}
		wResult += "</table></div>";
		return wResult;
	}


發佈了55 篇原創文章 · 獲贊 13 · 訪問量 36萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章