天天看點

動态添加 表格+将 request.getParameterMap();轉化為map

-------------------------------------------------------------------------------------------------------jsp-------------------------------------------------------------------------------------------------------------

function addElements(){

var randomNum = new Date().getTime();

var td1 = $("<td></td>");

var td2 = $("<td></td>");

var td3 = $("<td></td>");

var codeInput = $("<input type='text'/>");

var nameInput = $("<input type='text' />");

var optButton = $("<input type='button' οnclick='delThis(this);'/>");

$(codeInput).attr("id", "code_" + randomNum);

$(codeInput).attr("name", "code_" + randomNum);

$(nameInput).attr("id", "name_" + randomNum);

$(nameInput).attr("name", "name_" + randomNum);

$(optButton).attr("value", "删除");

$(optButton).attr("class", "btn");

$(td1).append(codeInput);

$(td2).append(nameInput);

$(td3).append(optButton);

var trEle = $("<tr></tr>");

$(trEle).append(td1);

$(trEle).append(td2);

$(trEle).append(td3);

$("#tableTwo").append(trEle); 

}

function delThis(obj) {

$(obj).parent().parent().remove();

}

<tr>

<td class="width-15 active"><label class="pull-right">網絡類型:</label></td>

<td class="width-35" colspan="2">

<table class="table table-border" id="tableTwo">

<tr>

<th>編碼</th>

<th>名稱</th>

<th>操作</th>

</tr>

<c:forEach items="${giAppStorageRules.giAppPackageSpecification}" var="codeI">

<tr>

<td><input type="text" id="code_${codeI.shopProduct}"

name="code_${codeI.shopProduct}" value="${codeI.shopProduct}" /></td>

<td><input type="text" id="name_${codeI.shopProduct}"

name="name_${codeI.shopProduct}" value="${codeI.shopProductName}" /></td>

<td><input type="button" οnclick="delThis(this);"

class="btn" value="删除" /></td>

</tr>

</c:forEach>

</table>

</td>

<td class="width-15 active">

<input name="sb2" class="btn btn-sm btn-default" id="sb2"

οnclick="addElements();" type="button" value="增加" />

</td>

</tr>

-------------------------------------------------------------------------------------------------------------java----------------------------------------------------------------------------------------------------------------

private Map<String, Object> getParameterMap(HttpServletRequest request) {

// 參數Map

Map<String, String[]> properties = request.getParameterMap();

// 傳回值Map

Map<String, Object> returnMap = new HashMap<String, Object>();

Iterator entries = properties.entrySet().iterator();

Map.Entry entry;

String name = "";

String value = "";

while (entries.hasNext()) {

entry = (Map.Entry) entries.next();

name = (String) entry.getKey();

Object valueObj = entry.getValue();

if (null == valueObj) {

value = "";

} else if (valueObj instanceof String[]) {

String[] values = (String[]) valueObj;

for (int i = 0; i < values.length; i++) {

value = values[i] + ",";

}

value = value.substring(0, value.length() - 1);

} else {

value = valueObj.toString();

}

if(StringUtils.isBlank(value)){

value = null;

}

returnMap.put(name, value);

}

return returnMap;

}

--------------------------------------------------------------------------------------------------------------對map解析(特殊下使用)----------------------------------------------------------------------------------

Iterator entries = map.entrySet().iterator();

Map.Entry entry;

while (entries.hasNext()) {

entry = (Map.Entry) entries.next();

String name = (String) entry.getKey();

if (name.contains("code")) {

GiAppPackageSpecification packageSpeci = new GiAppPackageSpecification();

Object valueObj = entry.getValue();

packageSpeci.setShopProduct((String)valueObj);

packageSpeci.setShopProductName((String)map.get("name_" + name.split("_")[1]));

packageSpeci.setCreateTime(new Date());

packageSpeci.setStatus("0");

packageSpeci.setStorageRulesId(giAppStorageRules.getId());

giAppPackageSpecificationService.save(packageSpeci);

}

}

繼續閱讀