Skip to content

Instantly share code, notes, and snippets.

@sqzr
Created June 30, 2015 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sqzr/a57f4d3b16002bb1a8e2 to your computer and use it in GitHub Desktop.
Save sqzr/a57f4d3b16002bb1a8e2 to your computer and use it in GitHub Desktop.
/**
* 理dataTables传过来的字段参数,返回封装好的TableCoulumEntity Map
* 数据:
* columns[0][data]:id
* columns[0][name]:xxx
* columns[1][data]:patientMember_person_name
* columns[1][name]:xxx
* --------------------------------
* 转换成:
* Map<Integer, TableCoulumEntity>
* Integer columns[] 括号中的参数
* TableCoulumEntity 自动赋值好的各种字段
*
* @param request HttpServletRequest 对象
* @return 封装好的TableCoulumEntity Map
*/
public Map<Integer, TableCoulumEntity> parseColumn(HttpServletRequest request) {
Map<String, Object> columnParams = WebUtils.getParametersStartingWith(request, "columns");
Map<Integer, Map<String, String>> groupParams = new HashMap<Integer, Map<String, String>>();
Map<Integer, TableCoulumEntity> columEntityMap = new HashMap<Integer, TableCoulumEntity>();
Set<Integer> paramsIndexSet = new HashSet<Integer>();
for (Map.Entry<String, Object> entry : columnParams.entrySet()) {
Integer paramIndex = getColumnParamIndex(entry.getKey());
if (paramIndex == null) {
continue;
}
paramsIndexSet.add(paramIndex);
}
for (Integer paramsIndex : paramsIndexSet) {
Map<String, String> groupMap = new HashMap<String, String>();
for (Map.Entry<String, Object> entry : columnParams.entrySet()) {
Integer paramIndex = getColumnParamIndex(entry.getKey());
if (paramIndex == null) {
continue;
}
if (paramsIndex.equals(paramIndex)) {
groupMap.put(entry.getKey(), String.valueOf(entry.getValue()));
}
}
groupParams.put(paramsIndex, groupMap);
}
for (Map.Entry<Integer, Map<String, String>> entry : groupParams.entrySet()) {
Map<String, String> paramMap = entry.getValue();
TableCoulumEntity tableCoulumEntity = new TableCoulumEntity();
tableCoulumEntity.setName(paramMap.get(StrUtil.format("[{}][name]", entry.getKey())));
tableCoulumEntity.setData(paramMap.get(StrUtil.format("[{}][data]", entry.getKey())));
tableCoulumEntity.setSearchable(Boolean.valueOf(paramMap.get(StrUtil.format("[{}][searchable]", entry.getKey()))));
tableCoulumEntity.setOrderable(Boolean.valueOf(paramMap.get(StrUtil.format("[{}][orderable]", entry.getKey()))));
tableCoulumEntity.setSaerchValue(paramMap.get(StrUtil.format("[{}][search][value]", entry.getKey())));
tableCoulumEntity.setSearchRegex(Boolean.valueOf(paramMap.get(StrUtil.format("[{}][search][regex]", entry.getKey()))));
columEntityMap.put(entry.getKey(), tableCoulumEntity);
}
return columEntityMap;
}
/**
* 处理dataTables传过来的字段参数,并自动赋值到tableEntity中
*
* @param request HttpServletRequest 对象
* @param tableEntity 需要自动赋值的tableEntity对象
*/
public void parseColumn(HttpServletRequest request, TableEntity tableEntity) {
tableEntity.setCoulumMap(parseColumn(request));
}
/**
* 处理dataTables传过来的字段参数,并自动赋值到tableEntity中
*
* @param tableEntity 需要自动赋值的tableEntity对象
*/
public void parseColumn(TableEntity tableEntity) {
tableEntity.setCoulumMap(parseColumn());
}
/**
* 处理dataTables传过来的字段参数,自动读取当前请求request对象
*
* @return 封装好的TableCoulumEntity Map
*/
public Map<Integer, TableCoulumEntity> parseColumn() {
return parseColumn(getRequest());
}
/**
* 取出datatables column的id
* Str = "columns[0][data]"
* Str2 = "columns[1][orderable]"
* getColumnParamIndex(str) -> 0
* getColumnParamIndex(str2) -> 1
*
* @param column 字段string
* @return index
*/
public Integer getColumnParamIndex(String column) {
String pattern = "\\[(\\d+)\\]";
Pattern pt = Pattern.compile(pattern);
Matcher matcher = pt.matcher(column);
if (matcher.find()) {
return Integer.valueOf(matcher.group(1));
} else {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment