Rip*_*bra 12 html javascript angular angular5
我正在使用div开发一个n级嵌套表的脚本.
因此,有5到6列有n行数,每个第一列都必须展开/折叠按钮,点击后我调用API,它给出了与所选行过滤器相对应的数据.
以前当我使用核心JavaScript和jQuery时,我使用find文档选择器的方法来识别展开/折叠按钮的父级,并在仅使用innerHTML或appendjQuery方法之后推送动态创建的HTML之后的特定div
我对角度有点新意,并没有多少工作.请帮我解决这个问题.
splitOpt 是一个对象数组,我将根据该数组拆分报告数据.
this.splitOpt = [
{
id: "country",
label: "Country"
},
{
id:"os".
label:"Operating System"
},
{
id:"osv".
label:"Operating System Version"
}
]
Run Code Online (Sandbox Code Playgroud)
获取报告的功能
getReport() {
// apiFilters are array of object having some values to filter report data
var apiFilters: any = [{}];
for (var i = 0; i < this.sFilters.length; i++) {
if (this.sFilters[i][0].values.length > 0) {
var k;
k = this.sFilters[i][0].id
apiFilters[0][k] = this.sFilters[i][0].values;
}
}
var split = this.splitOpt[0].id;
this._apis.getReportData(split, apiFilters[0]).subscribe(response => {
if (response.status == 1200) {
this.reportData = response.data.split_by_data;
}
})
}
Run Code Online (Sandbox Code Playgroud)
检查是否有更多分裂的功能
checkIfHaveMoreSplits(c){
if(this.splitOpt.length > 0) {
var index = this.splitOpt.findIndex(function(v) {
return v.id == c
})
if (typeof(this.splitOpt[index+1]) != "undefined"){
return this.splitOpt[index+1];
} else {
return 0;
}
}
}
Run Code Online (Sandbox Code Playgroud)
基于拆分和报告数据绘制表的代码.
让我们假设对象中的国家只有一个对象而splitopt不是checkIfHaveMoreSplits()返回0,这意味着我不必给出扩展按钮,如果不是0那个扩展按钮就会出现在那里.
单击展开按钮我将从中选择下一个元素splitopt并调用API以获得具有拆分参数作为载体的报告,依此类推.
<div class="table" >
<div class="row" *ngFor="let rData of reportData; let i = index;" >
<div class="col" >
<button
class="btn btn-sm"
*ngIf="checkIfHaveMoreSplits(splitbykey) !== 0"
(click)="splitData(splitbykey)"
>+</button>
{{rData[splitbykey]}}
</div>
<div class="col">{{rData.wins}}</div>
<div class="col">{{rData.conversions}}</div>
<div class="col">{{rData.cost}}</div>
<div class="col">{{rData.bids}}</div>
<div class="col">{{rData.impressions}}</div>
<div class="col">{{rData.rev_payout}}</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我正在管理一个数组,它可以识别我可以扩展崩溃元素的深度
我们假设数组有三个元素,即country,carrier和os
因此,我将绘制的第一个表格中包含表格中所有国家/地区的点击按钮,我将发送所选国家/地区并获取该特定国家/地区的运营商.获得响应后,我想根据响应创建自定义HTML,并在选定行后附加html.
以下是截图,包括完整的工作流程:)
第2步
第3步
我建议为每个要显示的动态 HTML 片段编写一个自定义角度组件。然后,您可以编写一个循环组件,该组件将*ngIf根据您提供的类型列表来嵌套组件。就像这样:
// dynamic.component.ts
export type DynamicComponentType = 'country' | 'os' | 'osv';
export interface IOptions { /* whatever options you need for your components */ }
export type DynamicComponentOptions = { type: DynamicComponentType, options: IOptions};
@Component({
selector: 'app-dynamic',
template = `
<app-country *ngIf="current.type == 'country'" [options]="current.options" />
<app-os *ngIf="current.type == 'os'" [options]="current.options" />
<app-osv *ngIf="current.type == 'osv'" [options]="current.options" />
<ng-container *ngIf="!!subTypes">
<button (click)="dynamicSubComponentShow = !dynamicSubComponentShow" value="+" />
<app-dynamic *ngIf="dynamicSubComponentShow" [options]="subOptions" />
</ng-container>`,
// other config
})
export class DynamicComponent {
@Input() options: DynamicComponentOptions[];
get current(): DynamicComponentOptions {
return this.options && this.options.length && this.options[0];
}
get subOptions(): DynamicComponentOptions[] {
return this.options && this.options.length && this.options.slice(1);
}
dynamicSubComponentShow = false;
// component logic, other inputs, whatever else you need to pass on to the specific components
}
Run Code Online (Sandbox Code Playgroud)
的示例CountryComponent。其他组件看起来类似。
// country.component.ts
@Component({
selector: 'app-country',
template: `
<div>Country label</div>
<p>Any other HTML for the country component using the `data` observable i.e.</p>
<span>x: {{ (data$ | async)?.x }}</span>
<span>y: {{ (data$ | async)?.y }}</span>
`,
})
export class CountryComponent {
@Input() options: IOptions;
data$: Observable<{x: string, y: number}>;
constructor(private countryService: CountryService) {
// load data specific for this country based on the input options
// or use it directly if it already has all your data
this.data$ = countryService.getCountryData(this.options);
}
}
Run Code Online (Sandbox Code Playgroud)
// my.component.ts
@Component({
template: `
<div class="table" >
<div class="row" *ngFor="let rData of reportData$ | async; let i = index;" >
<div class="col" >
<app-dynamic [options]="options$ | async"></app-dynamic>
</div>
...
</div>
</div>`,
// other cmp config
})
export class MyComponent {
options$: Observable<DynamicComponentOptions[]>;
reportData$: Observable<ReportData>;
constructor(private reportService: ReportService){
// simplified version of your filter calculation
let apiFilters: {} = this.sFilters
.map(f => f[0])
.filter(f => f && f.values && f.values.length)
.reduce((f, acc) => acc[f.id] = f.values && acc, {});
this.reportData$ = reportService.getReportData(this.splitOpt[0].id, apiFilters).pipe(
filter(r => r.status == 1200),
map(r => r.data.split_by_data)
);
this.options$ = this.reportData$.pipe(map(d => d.YOUR_OPTIONS));
}
}
Run Code Online (Sandbox Code Playgroud)
现在让你的 api 返回类似的内容
{
"status": 1200,
"data": {
"YOUR_OPTIONS": [{
"type": "country"
"options" { "id": 1, ... } // options for your country component initialization
}, {
"type": "os",
"options" { "id": 11, ... } // options for your os component initialization
}, ...],
// your other report data for the main grid
}
}
Run Code Online (Sandbox Code Playgroud)
请根据您的具体需求进行调整。例如,您必须管理通过组件层次结构的状态传递(使用组件状态、可观察服务、MobX、NgRx - 选择您的毒药)。
希望这有所帮助 :-)