需要有关带有嵌套数据的 Angular 数据表的帮助。
我想对表中的数据进行排序。
我正在使用来自 - https://www.npmjs.com/package/angular2-datatable 的数据表
数据表适用于单一数组类型的数据。 (用于许多 Angular 应用)
问题:我有嵌套的 json(实际上,我有复杂的 json,让这里变得简单)
感谢您对此进行调查。
如有任何建议或帮助,我们将不胜感激。
JSON
records = [
[
{
"name": "Subject Name",
"type": "text",
"id": "subjectName",
"value": "DavidJ",
"firstName": "David",
"lastName": "John"
},
{
"name": "QC Name",
"type": "hidden",
"id": "qcName",
"value": "JosephT",
"firstName": "Joseph",
"lastName": "Tom"
}
],
[
{
"name": "Subject Name",
"type": "text",
"id": "subjectName",
"value": "TigerC",
"firstName": "Tiger",
"lastName": "Chan"
},
{
"name": "QC Name",
"type": "hidden",
"id": "qcName",
"value": "ThomasR",
"firstName": "Thomas",
"lastName": "Richard"
}
]
]
HTML
<table class="table table-responsive table-hover" [mfData]="this.records | dataFilter : filterQuery" #mf="mfDataTable" [mfRowsOnPage]="rowsOnPage" [(mfSortBy)]="sortBy" [(mfSortOrder)]="sortOrder">
<thead>
<tr>
<th>#</th>
<th>
<mfDefaultSorter by="subjectName">subject Name</mfDefaultSorter>
</th>
<th>
<mfDefaultSorter by="qcPerson">QC Person</mfDefaultSorter>
</th>
</tr>
</thead>
<tbody *ngIf="!isLoading">
<tr class="border" *ngFor="let sample of mf.data; let i='index'">
<td>{{i + 1}}</td>
<ng-container *ngFor="let item of sample">
<td *ngIf="item.id ==='subjectName'">
{{item.firstName}} {{item.lastName}}
</td>
<td *ngIf="item.id ==='qcPerson'">
{{item.firstName}} {{item.lastName}}
</td>
</ng-container>
</tr>
</tbody>
</table>
类型脚本文件
import { Component, OnInit } from '@angular/core';
import { OrderBy } from '../all_services/OrderByPipe';
@Component({
selector: 'app-userdashboard',
templateUrl: './userdashboard.component.html',
styleUrls: ['../header-footer/css/external.style.css']
})
export class UserdashboardComponent implements OnInit {
constructor() {}
ngOnInit() {}
/** Sorting functions */
public data;
public filterQuery = "";
public rowsOnPage = 10;
public sortBy = "subjectName";
public sortOrder = "asc";
public toInt(num: string) {
return +num;
}
}
Datafilterpipe.ts
import * as _ from "lodash";
import {Pipe, PipeTransform} from "@angular/core";
@Pipe({
name: "dataFilter"
})
export class DataFilterPipe implements PipeTransform {
transform(array: any[], query: string): any {
if (query) {
return _.filter(array, row=>row.name.indexOf(query) > -1);
}
return array;
}
}
最佳答案
我遇到过同样的问题。我用过w3school在 DOM 中呈现表格后的表格排序逻辑。
它与 angular2-datatable 一起工作非常顺利,因为我在我的项目中使用相同的数据表。它的用法非常直接,如果您遇到任何问题,请告诉我。
提前致谢。
下面是实现 TS 文件中的函数。
columnSorter(n) {
let table, rows, switching, i, x, y, shouldSwitch, dir, switchCount = 0;
switching = true;
this.clickSort = !this.clickSort
dir = "asc";
table = document.querySelector('.smallTable');
while (switching) {
switching = false;
rows = table.rows;
for (i = 0; i < (rows.length - 1); i++) {
shouldSwitch = false;
x = rows[i].getElementsByTagName("TD")[n];
y = rows[i + 1].getElementsByTagName("TD")[n];
if (dir == 'asc') {
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
} else if (dir == 'desc') {
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
}
}
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
switchCount++;
} else {
if (switchCount == 0 && dir == 'asc') {
dir = 'desc';
switching = true;
}
}
}
}
在您的 HTML 表格中,只需在下面添加,其中 0 是列号。
(click)="columnSorter(0)
关于javascript - 从嵌套的 json 中排序的 Angular 4 数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51822924/
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc
我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的
我有一个名为posts的模型,它有很多附件。附件模型使用回形针。我制作了一个用于创建附件的独立模型,效果很好,这是此处说明的View(https://github.com/thoughtbot/paperclip):@attachment,:html=>{:multipart=>true}do|form|%>posts中的嵌套表单如下所示:prohibitedthispostfrombeingsaved:@attachment,:html=>{:multipart=>true}do|at_form|%>附件记录已创建,但它是空的。文件未上传。同时,帖子已成功创建...有什么想法吗?
我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle
我有一个非常简单的RubyRack服务器,例如:app=Proc.newdo|env|req=Rack::Request.new(env).paramspreq.inspect[200,{'Content-Type'=>'text/plain'},['Somebody']]endRack::Handler::Thin.run(app,:Port=>4001,:threaded=>true)每当我使用JSON对象向服务器发送POSTHTTP请求时:{"session":{"accountId":String,"callId":String,"from":Object,"headers":