Angular Datatables with different wrappers in Java Spring, along with the DataTablesInput and DataTablesOutput
Use Angular Datatables with different wrappers in Java's Spring framework, along with the DataTablesInput and DataTablesOutput methods when using custom queries.
Join the DZone community and get the full member experience.
Join For FreeFor the Angular 5 client-side, check out the below repository:
https://l-lin.github.io/angular-datatables/#/basic/server-side-angular-way
Server-Side Development With Spring
In the Spring controller, write a wrapper called NewWrapper
and use NewWrapperService
:
@RequestMapping(value = "/get", method = RequestMethod.POST)
public DataTablesOutput<NewWrapper> listPOST(@Valid @RequestBody DataTablesInput input) {
return newWrapperService.findAll(input);
}
In the Spring service, wrap it in the way shown below. Change the DataTablesInput
values, giving proper overrides of JSON input data and the DataTablesOutput
values, using ModelMapper
or your own mapper, finally overriding OldObjectPaginate
. Write your own custom query for repository currentRepo.findAll.
@Transactional
public DataTablesOutput<NewWrapper> findAll(DataTablesInput input) {
DataTablesInput input1 = new DataTablesInput();
input1.setDraw(input.getDraw());
input1.setOrder(input.getOrder());
input1.setStart(input.getStart());
input1.setLength(input.getLength());
input1.setSearch(input.getSearch());
List<Column> columns = input.getColumns();
for(Column column:columns) {
if (column.getData().equals("levelType")) {
column.setData("id.levelType");
}
else if (column.getData().equals("levelCode")) {
column.setData("id.levelCode");
}
}
input1.setColumns(columns);
DataTablesOutput<OldObjectPaginate> dataTableOutput = currentRepo.findAll(input1);
DataTablesOutput<NewWrapper> dataTableResult = new DataTablesOutput<NewWrapper>();
dataTableResult.setDraw(dataTableOutput.getDraw());
dataTableResult.setRecordsFiltered(dataTableOutput.getRecordsFiltered());
dataTableResult.setRecordsTotal(dataTableOutput.getRecordsTotal());
/*TypeMap<OldObjectPaginate, NewWrapper> typeMap = modelMapper.getTypeMap(OldObjectPaginate.class, NewWrapper.class);
if(typeMap==null) {
modelMapper.addMappings(new PropertyMap<OldObjectPaginate, NewWrapper>() {
@Override
protected void configure() {
map().setLevelCode(source.getId().getLevelCode());
map().setLevelType(source.getId().getLevelType());
}
});
}*/
List<NewWrapper> newWrapperList = new ArrayList<>();
for(OldObjectPaginate data: dataTableOutput.getData()) {
NewWrapper newWrapper = new NewWrapper();
newWrapper.setLevelCode(data.getId().getLevelCode());
newWrapper.setLevelType(data.getId().getLevelType());
newWrapperList.add(newWrapper);
}
//dataTableResult.setData(modelMapper.map(dataTableOutput.getData(), new TypeToken<List<NewWrapper>>() {}.getType()));
dataTableResult.setData(newWrapperList);
return dataTableResult;
}
Spring Framework
AngularJS
Java (programming language)
Opinions expressed by DZone contributors are their own.
Trending
-
The SPACE Framework for Developer Productivity
-
Getting Started With the YugabyteDB Managed REST API
-
Top 10 Pillars of Zero Trust Networks
-
How To Approach Java, Databases, and SQL [Video]
Comments