我正在使用Grails的Datable 1.10服务器端处理。新版本的Datatable更改了ajax post请求格式。我正在解析像这样的params值。它给出了错误。
println params?.start
println params?.columns[1]?.search?.regex
在php中,他们使用count函数来循环这样的列数。
for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ )
在grails中循环使用POST params值的野兽是什么?
columns[2][search][regex]: false
columns[2][searchable]: true
columns[0][search][value]:
columns[1][orderable]: true
columns[2][name]:
search[value]:
columns[1][data]: lastName
columns[1][searchable]: true
draw: 1
columns[0][data]: firstName
length: 5
columns[0][name]:
columns[0][search][regex]: false
columns[2][data]: dateOfBirth
columns[1][search][value]:
columns[0][orderable]: true
columns[1][name]:
columns[2][search][value]:
order[0][column]: 0
columns[1][search][regex]: false
order[0][dir]: asc
start: 0
columns[0][searchable]: true
columns[2][orderable]: true
search[regex]: false
试图用grails重写这个php代码,
https://github.com/DataTables/DataTables/blob/master/examples/server_side/scripts/ssp.class.php
想出了这个逻辑。有没有一个简单而更好的逻辑呢?
int len= params.findAll{ it.key ==~ /columns\[\d+\]\[data\]/ }.size()
for(int i=0; i<len; i++){
def str="columns["+i+"][data]"
println i + " ==> " + params."$str"
}
由于 SR
答案 0 :(得分:0)
此代码适用于我:
int len= params.findAll{ it.key ==~ /columns\[\d+\]\[data\]/ }.size()
答案 1 :(得分:0)
我使用了上面的一些想法,并提出了以下建议。
这绝不是一个完美的解决方案。请注意,我的UI具有每列过滤/搜索功能,并且已禁用全局搜索。
class DataTablesParams {
Integer length
Integer draw
Long start
Map search
private DataTablesOrderParams _order
private Map<Integer, DataTablesColumnsParams> _columns
/**
* Gets the "order" DataTables data.
* <br/>
* <b>Note:</b> This method cannot be called getOrder() because Grails
* tries to use that for automatic data-binding which does not work with
* the syntax used by DataTables.
* @return DataTablesOrderParams
*/
DataTablesOrderParams getDataTablesOrder() {
if (_order == null) {
_order = new DataTablesOrderParams()
}
return _order
}
/**
* Gets the "columns" DataTables data.
* <br/>
* <b>Note:</b> This method cannot be called getColumns() because Grails
* tries to use that for automatic data-binding which does not work with
* the syntax used by DataTables.
*
* @return Map<Integer, DataTablesColumnsParams> The key to the map is the column number.
*/
Map<Integer, DataTablesColumnsParams> getDataTablesColumns() {
if (_columns == null) {
_columns = new HashMap()
}
return _columns
}
DataTablesColumnsParams getColumn(int column) {
if (getDataTablesColumns().get(column) == null) {
getDataTablesColumns().put(column, new DataTablesColumnsParams())
}
return getDataTablesColumns().get(column)
}
String getSearchValueForColumn(String columnName) {
for (DataTablesColumnsParams p : getDataTablesColumns().values()) {
if (p.data?.equals(columnName)) {
return p.searchValue
}
}
return null
}
int getColumnCount() {
return getDataTablesColumns().size()
}
void processAdditionalParams(Map params) {
try {
getDataTablesOrder().column = Integer.parseInt(params."order[0][column]")
getDataTablesOrder().dir = params."order[0][dir]"
int totalMatches
totalMatches= params.findAll{ it.key ==~ /columns\[\d+\]\[name\]/ }.size()
for(int i = 0; i < totalMatches; i++){
getColumn(i).name = params."columns[${i}][name]"
}
totalMatches= params.findAll{ it.key ==~ /columns\[\d+\]\[data\]/ }.size()
for(int i = 0; i < totalMatches; i++){
getColumn(i).data = params."columns[${i}][data]"
}
totalMatches= params.findAll{ it.key ==~ /columns\[\d+\]\[column\]/ }.size()
for(int i = 0; i < totalMatches; i++){
getColumn(i).column = Integer.parseInt(params."columns[${i}][column]")
}
totalMatches= params.findAll{ it.key ==~ /columns\[\d+\]\[searchable\]/ }.size()
for(int i = 0; i < totalMatches; i++){
getColumn(i).searchable = params."columns[${i}][searchable]"
}
totalMatches= params.findAll{ it.key ==~ /columns\[\d+\]\[orderable\]/ }.size()
for(int i = 0; i < totalMatches; i++){
getColumn(i).orderable = params."columns[${i}][orderable]"
}
totalMatches= params.findAll{ it.key ==~ /columns\[\d+\]\[search\]\[value\]/ }.size()
for(int i = 0; i < totalMatches; i++){
getColumn(i).searchValue = params."columns[${i}][search][value]"
}
totalMatches= params.findAll{ it.key ==~ /columns\[\d+\]\[search\]\[regex\]/ }.size()
for(int i = 0; i < totalMatches; i++){
getColumn(i).searchRegex = params."columns[${i}][search][regex]"
}
} catch (Throwable t) {
println("*** ERROR: processAdditionalParams() " + t)
throw t
}
}
int getDataTablesOrderColumn() {
return getDataTablesOrder().column
}
String getOrderColumnName() {
int orderColumn = getDataTablesOrderColumn()
return getColumnName(orderColumn)
}
String getColumnName(Integer columnNumber) {
/*
* The column name is in the 'data' element
*/
return getColumn(columnNumber).data
}
String getOrderDirection() {
return getDataTablesOrder().dir
}
boolean isOrderAscending() {
return getOrderDirection() == 'asc'
}
boolean isOrderDescending() {
return getOrderDirection() != 'asc'
}
int getDataTablesColumnsSize() {
return getDataTablesColumns().size() ?: 0
}
public String toString() {
StringBuffer b = new StringBuffer(getClass().getSimpleName())
b.append(" [")
b.append(" length:").append(length)
b.append(" draw:").append(draw)
b.append(" start:").append(start)
b.append(" search:").append(search)
b.append(" order:").append(getDataTablesOrder())
b.append(" columnsSize:").append(getDataTablesColumnsSize())
b.append(" columns:").append(getDataTablesColumns())
b.append(" getDataTablesOrderColumn():").append(getDataTablesOrderColumn())
b.append(" getOrderDirection():").append(getOrderDirection())
b.append(" getOrderColumnName():").append(getOrderColumnName())
b.append("]")
return b.toString()
}
}
另外
class DataTablesOrderParams {
Integer column
String dir
public String toString() {
StringBuffer b = new StringBuffer(getClass().getSimpleName())
b.append(" [")
b.append(" column:").append(column)
b.append(" dir:").append(dir)
b.append("]")
return b.toString()
}
}
和
class DataTablesColumnsParams {
String name
String data
Integer column
String searchValue
Boolean searchRegex
Boolean searchable
String orderable
public String toString() {
StringBuffer b = new StringBuffer(getClass().getSimpleName())
b.append(" [")
b.append(" name:").append(name)
b.append(" data:").append(data)
b.append(" column:").append(column)
b.append(" searchValue:").append(searchValue)
b.append(" searchRegex:").append(searchRegex)
b.append(" searchable:").append(searchable)
b.append(" orderable:").append(orderable)
b.append("]")
return b.toString()
}
}
最后,&#34;丑陋&#34;位:
def yourControllerMethod(SomeCommandObjectCommand command,
DataTablesParams dataTablesParams) {
/*
* IMPORTANT: DO THIS FIRST
*/
dataTablesParams.processAdditionalParams(params)
<rest of your code>
}
希望这会有所帮助,或者至少会给你一些想法。