Skip to content

Commit 8782ce3

Browse files
1006724-Nextjs-sample-update
1 parent 752d2dd commit 8782ce3

2 files changed

Lines changed: 50 additions & 88 deletions

File tree

  • Binding_Next_JS_Server

Binding_Next_JS_Server/angular_client/src/app/doctors/doctors.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
GridComponent,
1010
GridModule,
1111
ToolbarService,
12+
RowInfo,
1213
} from '@syncfusion/ej2-angular-grids';
1314
import {
1415
PageService,
@@ -95,7 +96,7 @@ export class Doctors {
9596
search: [],
9697
};
9798
this.fetchData(initialState).then((res) => {
98-
this.data = res;
99+
this.gridInstance.dataSource = res;
99100
});
100101
}
101102

@@ -133,7 +134,7 @@ export class Doctors {
133134
(args as any).dataSource(res.result);
134135
} else {
135136
// For paging, sorting and other data actions
136-
this.data = res;
137+
this.gridInstance.dataSource = res;
137138
}
138139
}
139140

@@ -169,9 +170,9 @@ export class Doctors {
169170
}
170171

171172
// Navigate to patients for the selected doctor
172-
btnClick(event: any) {
173-
const rowData: any = this.gridInstance.getRowInfo(event.target).rowData;
174-
const doctorID = rowData.DoctorId;
173+
btnClick(event: MouseEvent) {
174+
const currentRowInfo: RowInfo = this.gridInstance.getRowInfo(event.target as HTMLElement);
175+
const doctorID = (currentRowInfo.rowData as any).DoctorId;
175176
this.router.navigate(['patients'], {
176177
queryParams: { doctorID },
177178
});

Binding_Next_JS_Server/next_js_server/app/api/health_care/route.ts

Lines changed: 44 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,67 @@
11
import { Predicate } from '@syncfusion/ej2-data';
22
import { NextResponse, NextRequest } from "next/server";
33
import { DataManager, Query } from '@syncfusion/ej2-data';
4-
import { doctorDetails } from '../../../data/health_care_Entities';
4+
import { doctorDetails } from '../../../data/health_care_Entities';
55

66
// CORS headers configuration
77
const CORS_HEADERS = {
8-
'Access-Control-Allow-Origin': '*',
9-
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
10-
'Access-Control-Allow-Headers': 'Content-Type',
8+
'Access-Control-Allow-Origin': '*',
9+
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
10+
'Access-Control-Allow-Headers': 'Content-Type',
1111
};
1212

1313
// Create a method to send responses with CORS headers
1414
const corsResponse = (body: any, status = 200) => {
15-
return new NextResponse(JSON.stringify(body), {
16-
status,
17-
headers: { 'Content-Type': 'application/json', ...CORS_HEADERS },
18-
});
15+
return new NextResponse(JSON.stringify(body), {
16+
status,
17+
headers: { 'Content-Type': 'application/json', ...CORS_HEADERS },
18+
});
1919
};
2020

2121
// Handle the preflight request for CORS
2222
export async function OPTIONS(request: NextRequest) {
23-
return new NextResponse(null, { headers: CORS_HEADERS });
23+
return new NextResponse(null, { headers: CORS_HEADERS });
2424
}
25-
26-
// Helper function: Apply filtering based on predicates
27-
const performFiltering = (filterArray: any[], query: any) => {
28-
const predicateCollection = Array.isArray(filterArray) ? filterArray[0] : filterArray;
29-
30-
if (!predicateCollection || !Array.isArray(predicateCollection.predicates) || predicateCollection.predicates.length === 0) {
31-
return;
32-
}
33-
34-
const condition = (predicateCollection.condition || 'and').toLowerCase();
35-
const ignoreCase = predicateCollection.ignoreCase !== undefined ? !!predicateCollection.ignoreCase : true;
36-
37-
let combinedPredicate: any = null;
38-
39-
predicateCollection.predicates.forEach((p: any) => {
40-
if (p.isComplex && Array.isArray(p.predicates)) {
41-
const nested = buildNestedPredicate(p, ignoreCase);
42-
if (nested) {
43-
combinedPredicate = combinedPredicate
44-
? (condition === 'or' ? combinedPredicate.or(nested) : combinedPredicate.and(nested))
45-
: nested;
46-
}
47-
return;
48-
}
49-
50-
const singlePredicate: any = new Predicate(p.field, p.operator, p.value, true);
51-
combinedPredicate = combinedPredicate
52-
? (condition === 'or' ? combinedPredicate.or(singlePredicate) : combinedPredicate.and(singlePredicate))
53-
: singlePredicate;
54-
});
55-
56-
if (combinedPredicate) {
57-
query.where(combinedPredicate);
58-
}
25+
// Normalize condition string (default to 'and')
26+
const normalize = (condition?: string) => (condition || 'and').toLowerCase();
27+
28+
// Recursively build predicate tree
29+
const buildPredicate = (node: any, ignoreCase: boolean): any =>
30+
node?.isComplex && node.predicates?.length
31+
? node.predicates
32+
.map((p: Predicate) => buildPredicate(p, ignoreCase))
33+
.filter(Boolean)
34+
.reduce((acc: any, cur: any) =>
35+
acc ? (normalize(node.condition) === 'or' ? acc.or(cur) : acc.and(cur)) : cur, null)
36+
: (node?.field && node?.operator ? new Predicate(node.field, node.operator, node.value, ignoreCase) : null);
37+
38+
// Apply filtering based on predicates
39+
const performFiltering = (input: any, query: Query) => {
40+
const filter = Array.isArray(input) ? input[0] : input;
41+
if (!filter?.predicates?.length) return;
42+
const ignoreCase = filter.ignoreCase !== undefined ? !!filter.ignoreCase : true;
43+
const condition = normalize(filter.condition);
44+
const combined = filter.predicates
45+
.map((p: Predicate) => buildPredicate(p, ignoreCase))
46+
.filter(Boolean)
47+
.reduce((acc: any, cur: any) => acc ? (condition === 'or' ? acc.or(cur) : acc.and(cur)) : cur, null);
48+
if (combined) query.where(combined);
5949
};
6050

61-
// Helper function: Build nested predicates for complex filtering
62-
function buildNestedPredicate(block: any, ignoreCase: boolean) {
63-
const condition = (block.condition || 'and').toLowerCase();
64-
let mergedPredicate: any | null = null;
65-
66-
block.predicates.forEach((p: any) => {
67-
let node;
68-
if (p.isComplex && Array.isArray(p.predicates)) {
69-
node = buildNestedPredicate(p, ignoreCase);
70-
} else {
71-
node = new Predicate(p.field, p.operator, p.value, ignoreCase);
72-
}
73-
if (node) {
74-
mergedPredicate = mergedPredicate
75-
? (condition === 'or' ? mergedPredicate.or(node) : mergedPredicate.and(node))
76-
: node;
77-
}
78-
});
79-
80-
return mergedPredicate;
81-
}
82-
8351
// Helper function: Apply search functionality
8452
const performSearching = (searchParam: any, query: any) => {
8553
const { fields, key, operator, ignoreCase } = searchParam[0];
8654
query.search(key, fields, operator, ignoreCase);
8755
};
8856

8957
// Helper function: Apply sorting
90-
const performSorting = (sortArray: any[], query: any) => {
58+
const performSorting = (sortArray: any[], query: Query) => {
9159
for (let i = 0; i < sortArray.length; i++) {
9260
const { name, direction } = sortArray[i];
9361
query.sortBy(name, direction);
9462
}
9563
};
9664

97-
// Helper function: Apply paging
98-
const performPaging = (data: any[], gridState: any) => {
99-
if (!gridState.take || gridState.take <= 0) {
100-
return data;
101-
}
102-
const pageSkip = gridState.skip || 0;
103-
const pageSize = gridState.take;
104-
return data.slice(pageSkip, pageSkip + pageSize);
105-
};
106-
10765
// GET - Retrieve all data
10866
export async function GET(request: NextRequest) {
10967

@@ -134,13 +92,16 @@ export async function GET(request: NextRequest) {
13492
performSorting(gridState.sorted, query);
13593
}
13694

137-
// Execute query on data
138-
const resultantData = new DataManager(doctorDetails).executeLocal(query);
139-
const count: any = resultantData.length;
140-
let result: any = resultantData;
141-
14295
// Paging
143-
result = performPaging(result, gridState);
96+
if (gridState.take && gridState.take > 0) {
97+
const skip = gridState.skip || 0;
98+
const take = gridState.take;
99+
query.page(skip / take + 1, take);
100+
}
101+
102+
// Execute query on data
103+
const result: object[] = new DataManager(doctorDetails).executeLocal(query);
104+
const count: number = result.length;
144105

145106
return corsResponse({ result, count });
146107
}
@@ -183,7 +144,7 @@ export async function DELETE(request: NextRequest) {
183144
}
184145
const deletedDoctor = doctorDetails[doctorIndex];
185146
doctorDetails.splice(doctorIndex, 1);
186-
return corsResponse({ message: "Doctor deleted" });
147+
return corsResponse({ message: "Doctor deleted" });
187148
}
188149
}
189150

0 commit comments

Comments
 (0)