Grid:
<script>
Vue.use(ejs.grids.GridPlugin);
var vm = new Vue({
el: '#app',
template: `<ejs-grid ref='grid' :dataSource='data' :allowPaging='true' :pageSettings='PageSettings'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' :isPrimaryKey='true' width=100 ></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
<e-column field='ShipCity' headerText='Ship City' width=150></e-column>
</e-columns>
</ejs-grid>`,
data() {
return {
data: new ejs.data.DataManager({
url: "/Home/UrlDatasource",
adaptor: new ejs.data.UrlAdaptor()
}),
PageSettings: { pageSize: 50 }
}
}
});
</script>
Controller:
public IActionResult UrlDatasource([FromBody]DataManagerRequest dm)
{
IEnumerable DataSource = order;
DataOperations operation = new DataOperations();
if (dm.Search != null && dm.Search.Count > 0)
{
DataSource = operation.PerformSearching(DataSource, dm.Search); //Search
}
if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
{
DataSource = operation.PerformSorting(DataSource, dm.Sorted);
}
if (dm.Where != null && dm.Where.Count > 0) //Filtering
{
DataSource = operation.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
}
int count = DataSource.Cast<Orders>().Count();
if (dm.Skip != 0)
{
DataSource = operation.PerformSkip(DataSource, dm.Skip); //Paging
}
if (dm.Take != 0)
{
DataSource = operation.PerformTake(DataSource, dm.Take);
}
return dm.RequiresCounts ? Json(new { result = DataSource, count = count }) : Json(DataSource);
} |
public class Test extends HttpServlet {
List<JsonObject> list = new ArrayList<JsonObject>();
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action"); //get current grid action that's given in the datamanager url and perform actions based on that and return the result to grid
if(action.equalsIgnoreCase( "insert" )) {
this.Insert(request, response); // insert action
}
else { //generate datasource and pass to grid
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
String json = "";
if(br != null){
json = br.readLine();
}
// 2. initiate jackson mapper
ObjectMapper mapper1 = new ObjectMapper();
if(list.size() < 1) {
int ord = 10214;
for(int i=0;i<5;i++){
JsonObject or = new JsonObject();
or.addProperty("CustomerID", "VINET" + i);
or.addProperty("OrderID", ord + i);
or.addProperty("EmployeeID", 1 + i);
list.add(or);
}
}
// return the data
. . .
}
}
. . .
public void Insert(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1. get received JSON data from request
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
String json = "";
if(br != null){
json = br.readLine();
}
// 2. initiate jackson mapper
ObjectMapper mapper = new ObjectMapper();
// 3. Convert received JSON to args
Argss args = mapper.readValue(json, Argss.class); // Argss class contains value from client
Order or = (((Argss)args).value);
JsonObject addNew = new JsonObject();
addNew.addProperty("CustomerID", or.CustomerID);
addNew.addProperty("OrderID", or.OrderID);
addNew.addProperty("EmployeeID", or.EmployeeID);
list.add(addNew);
}
} |