Commit 2176373c by Ooh-Ao

ดึงข้อมูล

parent 469b489b
...@@ -594,7 +594,7 @@ export class DashboardManagementComponent implements OnInit { ...@@ -594,7 +594,7 @@ export class DashboardManagementComponent implements OnInit {
this.dashboardData = dashboard; this.dashboardData = dashboard;
this.panels = this.mapWidgetsToPanels(dashboard.widgets || []); this.panels = this.mapWidgetsToPanels(dashboard.widgets || []);
if (dashboard.datasetId) { if (dashboard.datasetId) {
this.dashboardStateService.selectDataset(dashboard.datasetId); // this.dashboardStateService.selectDataset(this.dashboardData);
} }
} }
}); });
...@@ -739,17 +739,104 @@ export class DashboardManagementComponent implements OnInit { ...@@ -739,17 +739,104 @@ export class DashboardManagementComponent implements OnInit {
} }
} }
onDatasetSelected(datasetId: string): void { onDatasetSelected(dataset: DatasetModel): void {
console.log('Dataset selected:', datasetId); // Added for debugging/clarity console.log('Dataset selected:', dataset); // Added for debugging/clarity
if (this.dashboardData) { if (this.dashboardData) {
this.dashboardData.datasetId = datasetId; this.dashboardData.datasetId = dataset.itemId;
this.dashboardStateService.selectDataset(datasetId); this.dashboardStateService.selectDataset(dataset);
this.getDatasetByTemplate(datasetId); this.getDatasetByTemplate(dataset);
} }
} }
getDatasetByTemplate(datasetId: string): void { getDatasetByTemplate(dataset: DatasetModel): void {
this.datasetService.getDatasetByTemplate(datasetId).subscribe(dataset => {
this.datasetService.getDatasetByTemplate(dataset.templateId, dataset.fileName).subscribe(dataset => {
if (dataset && this.dashboardData) {
// Update the config of each widget with the new data
this.dashboardData.widgets.forEach(widget => {
if (widget.config) {
widget.config.source.data = dataset; // Assuming the dataset is the data source
}
});
// Remap panels to reflect the changes
this.panels = this.mapWidgetsToPanels(this.dashboardData.widgets);
}
}, (error) => {
console.error('Error fetching dataset by template:', error);
let dataset = [
{
"id": "E001",
"name": "Alice Smith",
"department": "Sales",
"salary": 60000,
"hireDate": "2020-01-15",
"performanceScore": 85,
"gender": "Female",
"age": 30,
"salesAmount": 120000,
"region": "North"
},
{
"id": "E002",
"name": "Bob Johnson",
"department": "Marketing",
"salary": 55000,
"hireDate": "2019-03-20",
"performanceScore": 92,
"gender": "Male",
"age": 35,
"salesAmount": 0,
"region": "East"
},
{
"id": "E003",
"name": "Charlie Brown",
"department": "Sales",
"salary": 62000,
"hireDate": "2021-07-01",
"performanceScore": 78,
"gender": "Male",
"age": 28,
"salesAmount": 110000,
"region": "West"
},
{
"id": "E004",
"name": "Diana Prince",
"department": "HR",
"salary": 70000,
"hireDate": "2018-11-10",
"performanceScore": 95,
"gender": "Female",
"age": 40,
"salesAmount": 0,
"region": "South"
},
{
"id": "E005",
"name": "Eve Adams",
"department": "Marketing",
"salary": 58000,
"hireDate": "2022-05-25",
"performanceScore": 88,
"gender": "Female",
"age": 25,
"salesAmount": 0,
"region": "North"
},
{
"id": "E006",
"name": "Frank White",
"department": "Sales",
"salary": 65000,
"hireDate": "2019-09-01",
"performanceScore": 90,
"gender": "Male",
"age": 32,
"salesAmount": 130000,
"region": "East"
}
]
if (dataset && this.dashboardData) { if (dataset && this.dashboardData) {
// Update the config of each widget with the new data // Update the config of each widget with the new data
this.dashboardData.widgets.forEach(widget => { this.dashboardData.widgets.forEach(widget => {
......
...@@ -156,7 +156,7 @@ export class DashboardViewerComponent implements OnInit { ...@@ -156,7 +156,7 @@ export class DashboardViewerComponent implements OnInit {
this.dashboardName = dashboard.thName; this.dashboardName = dashboard.thName;
if (dashboard.datasetId) { if (dashboard.datasetId) {
this.dashboardStateService.selectDataset(dashboard.datasetId); // this.dashboardStateService.selectDataset(dashboard.datasetId);
} }
// Process widgets and fetch data if necessary // Process widgets and fetch data if necessary
......
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable, of } from 'rxjs'; import { BehaviorSubject, Observable, of } from 'rxjs';
import { switchMap, map } from 'rxjs/operators'; import { switchMap, map, shareReplay } from 'rxjs/operators';
import { DatasetService } from './dataset.service'; import { DatasetService } from './dataset.service';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { DatasetModel } from '../models/widgets.model'; import { DatasetModel } from '../models/widgets.model';
...@@ -14,27 +14,35 @@ export interface SelectedDataset { ...@@ -14,27 +14,35 @@ export interface SelectedDataset {
providedIn: 'root' providedIn: 'root'
}) })
export class DashboardStateService { export class DashboardStateService {
private selectedDatasetId = new BehaviorSubject<string | null>(null); private selectedDatasetId = new BehaviorSubject<DatasetModel | null>(null);
public selectedDataset$: Observable<SelectedDataset | null>; public selectedDataset$: Observable<SelectedDataset | null>;
constructor(private datasetService: DatasetService, private http: HttpClient) { constructor(private datasetService: DatasetService, private http: HttpClient) {
// this.selectedDataset$ = this.selectedDatasetId.pipe( this.selectedDataset$ = this.selectedDatasetId.pipe(
// switchMap(id => { switchMap(dataset => {
// if (id) { if (dataset) {
// console.log('Selected dataset ID:', id); // Log selected dataset ID return this.datasetService.getDatasetByTemplate(dataset.templateId, dataset.fileName).pipe(
// return this.datasetService.getDatasetByTemplate(id).pipe( map(response => {
// switchMap((dataset: DatasetModel | undefined) => { // The API might return an array directly, or an object with a 'data' property.
const dataArray = Array.isArray(response) ? response : (response && Array.isArray(response.data)) ? response.data : null;
// return of(null); if (dataArray && dataArray.length > 0) {
// }) return {
// ); data: dataArray,
// } columns: Object.keys(dataArray[0])
// return of(null); };
// }) }
// ); return null; // Return null if data is not in the expected format
})
);
}
return of(null); // No dataset ID selected
}),
shareReplay(1) // Cache and replay the last emitted value
);
} }
selectDataset(datasetId: string): void { selectDataset(dataset: DatasetModel): void {
this.selectedDatasetId.next(datasetId); this.selectedDatasetId.next(dataset);
} }
} }
...@@ -28,8 +28,8 @@ export class DatasetService { ...@@ -28,8 +28,8 @@ export class DatasetService {
return this.http.get<DatasetModel[]>('https://portal.myhr.co.th/api/template-file/menuitem/mini/lists?companyid=DEMO'); return this.http.get<DatasetModel[]>('https://portal.myhr.co.th/api/template-file/menuitem/mini/lists?companyid=DEMO');
} }
getDatasetByTemplate(id: string): Observable<DatasetModel | undefined> { getDatasetByTemplate(templateId : string , fileName : string): Observable<any> {
// return this.http.get<DatasetModel>(`https://portal.myhr.co.th/api/template-file/dataset/${id}`); // return this.http.get<DatasetModel>(`https://portal.myhr.co.th/api/template-file/dataset/${id}`);
return this.http.get<DatasetModel>('https://portal.myhr.co.th/api/template-file/dataset/3/5D5TITDVNH.xlsm?companyid=DEMO') return this.http.get<any>(`https://portal.myhr.co.th/api/template-file/dataset/${templateId}/${fileName}?companyid=DEMO`)
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment