DataStructure for the DataTable server side proces

2019-08-05 04:41发布

问题:

I am using DataTables 1.10 with Spring MVC. When using server side processing it send set of parameters to the controller. It is set of two dimention array but the column being the string like this:

columns[0][data]=
columns[0][name]=
columns[0][orderable]=false
columns[0][search][regex]=false
columns[0][search][value]=
columns[0][searchable]=true
columns[1][data]=
columns[1][name]=
columns[1][orderable]=false
columns[1][search][regex]=false
columns[1][search][value]=
columns[1][searchable]=true
order[0][column]=4
order[0][dir]=desc
order[1][column]=4
order[1][dir]=desc
search[regex]=false
search[value]=

How can I represent this as DTO in Java? Any suggestion?

回答1:

you can change the data (especially search part which is 3dimension) that you send to the server and make them all as a 2d array. Next you can easily use this link to create your DTO.



回答2:

public class Columns {
    private int index;
    private String data;
    private String name;
    private boolean orderable;
    private Search search;
    private boolean searchable;

    public Columns(int index) {
        this.index = index;
    }

    public Columns(int index, boolean orderable, Search search,boolean searchable) {
        this.index = index;
        this.orderable = orderable;
        this.search = search;
        this.searchable = searchable;
    }
}

public class Search {
    private boolean regex;
    private int value;

    public Search(boolean regex) {
        this.regex = regex;
    }
}

public class Order {
    private int index;
    private int column;
    private String dir;

    public Order(int index) {
        this.index = index;
    }

    public Order(int index, int column, String dir) {
        this.index = index;
        this.column = column;
        this.dir = dir;
    }
}


回答3:

It may be an idea to resolving the problem.

public class DataTableOptionsDto implements Serializable {

    private static final long serialVersionUID = 1012699939925216984L;

    private List<Map<String, Object>> columns;

    private List<Map<String, Object>> order;

    private Map<String, Object> search;

    // add getters and setters

}

@Controller
public class SearchController{

    @RequestMapping("/search")
    public String search(DataTableOptionsDto dataTableOptionsDto){
        //do some things here
        return jsonBuilder.json(searchData);
    }

}