Javascript 在TypeScript中创建一个双数组并填充它
我想导出一个ngx数据表,如果可能的话,我已经在网上看过了,但是显然,有了ngx数据表,用一个简单的函数是不可能的。因此,我尝试创建自己的cliclable按钮,以提取数组的数据 我网站上的阵列如下所示: 我想以相同的“外观”将此数组导出到xlsx文件中 因此,要在我的网站中用价值存量填充我的双数组(这不是我的代码,我只是一个初创公司的实习生,所以解释起来很复杂。)价值存量在行中,要有人的名字,例如,我必须使用row.first_name,如下面的示例所示Javascript 在TypeScript中创建一个双数组并填充它,javascript,Javascript,我想导出一个ngx数据表,如果可能的话,我已经在网上看过了,但是显然,有了ngx数据表,用一个简单的函数是不可能的。因此,我尝试创建自己的cliclable按钮,以提取数组的数据 我网站上的阵列如下所示: 我想以相同的“外观”将此数组导出到xlsx文件中 因此,要在我的网站中用价值存量填充我的双数组(这不是我的代码,我只是一个初创公司的实习生,所以解释起来很复杂。)价值存量在行中,要有人的名字,例如,我必须使用row.first_name,如下面的示例所示 this.rows.forEach(
this.rows.forEach(element => {
TableData.push([element.first_name + ' ' + element.last_name], [element.status], [element.address_from_locality], [element.address_to_locality]);
i++;
});
我也试着这样做
var TableData = [];
this.rows.forEach(element => {
TableData.push(element.first_name + ' ' + element.last_name);
TableData.push(element.status);
TableData.push(element.address_from_locality);
TableData.push(element.address_to_locality);
});
但这些方法不起作用,它们只给了我一个简单的数组,但我需要这种方法:
最后,我想导出xlsx文件中的双数组,使用xlsx,我使用以下代码:
const wb = xlsx.utils.book_new(), ws = xlsx.utils.aoa_to_sheet(TableData);
xlsx.utils.book_append_sheet(wb, ws);
xlsx.writeFile(wb, filename);
你能帮我找到解决办法吗
谢谢你的帮助。我前面就是这样用的
<ngx-datatable #myTable class="material" [columnMode]="'force'" [rows]="rows" [rowHeight]="'auto'"
[headerHeight]="'auto'" [footerHeight]="'auto'" [externalPaging]="true" [count]="paginationInfo.totalElements"
[offset]="paginationInfo.currentPage" [limit]="paginationInfo.pageSize" [scrollbarH]="true"
[loadingIndicator]="loadingIndicator" (sort)="onSort($event)" (page)="setTablePage($event)" id="exportable">
<ngx-datatable-column name="Name" [sortable]="true" [width]="200" [prop]="'first_name'" frozenLeft="true">
<ng-template let-row="row" ngx-datatable-cell-template>
<a [routerLink]="['/',auth.userRID,'talent',row.user_id]">
{{row?.first_name}}
{{row?.last_name}}
</a>
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="Status" [prop]="'status'" [width]="200">
<ng-template let-row="row" ngx-datatable-cell-template>
<span class="label" labelState [setType]="'talent'" [labelStatus]="row.status">
{{ row?.status.replace("_"," ") }}
</span>
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="From" [sortable]="true" [prop]="'address_from_locality'" [width]="200">
<ng-template let-row="row" ngx-datatable-cell-template>
{{ row?.address_from_locality }}
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="To" [sortable]="true" [prop]="'address_to_locality'" [width]="200">
<ng-template let-row="row" ngx-datatable-cell-template>
{{ row?.address_to_locality }}
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="Actions" [sortable]="false" [width]="200">
<ng-template let-row="row" ngx-datatable-cell-template>
<div class="btn-group" dropdown container="body">
<button class="btn btn-info btn-xs dropdown-toggle" dropdownToggle type="button">
More <span class="caret"></span>
</button>
<ul id="dropdown-basic" *dropdownMenu class="dropdown-menu" role="menu" aria-labelledby="button-basic">
<li role="menuitem"><a class="dropdown-item" [routerLink]="['/call',row.user_id]">Chat</a></li>
<li role="menuitem"><a class="dropdown-item" [routerLink]="['/',auth.userRID,'talent',row.user_id]">View
profile</a></li>
<li role="menuitem"><a class="dropdown-item"
[routerLink]="['/',auth.userRID, 'talent','tasks',row.user_id]">View Tasks</a></li>
<li role="menuitem"><a class="dropdown-item"
[routerLink]="['/',auth.userRID,'talent','edit',row.user_id]">Edit profile</a></li>
<li role="menuitem" dropdown triggers="mouseover"><a dropdownToggle
class="dropdown-item dropdown-toggle">Change
status to<span class="caret"></span></a>
<ul *dropdownMenu class="dropdown-menu" role="menu">
<li role="menuitem" *ngFor="let status of statuses|keyvalue">
<a class="dropdown-item" (click)="switchStatus(row.user_id,status.value)">{{status.key}}</a>
</li>
</ul>
</li>
</ul>
</div>
</ng-template>
</ngx-datatable-column>
<!--<ngx-datatable-column name="Start Date" [sortable]="true" [prop]="'move_date'">
<ng-template let-row="row" ngx-datatable-cell-template>
{{ row?.move_date | date }}
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="End Date" [sortable]="true" [prop]="'end_date'">
<ng-template let-row="row" ngx-datatable-cell-template>
{{ row?.end_date | date }}
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="Email" [sortable]="true" [prop]="'user_email'">
<ng-template let-row="row" ngx-datatable-cell-template>
{{ row?.email }}
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="Onboard Date" [sortable]="true" [prop]="'created_at'">
<ng-template let-row="row" ngx-datatable-cell-template>
{{ row?.created_at | date }}
</ng-template>
</ngx-datatable-column>-->
</ngx-datatable>
{{row?.first_name}}
{{row?.last_name}}
{{row?.status.replace(“\”,“)}
{{row?.address{u from{u locality}}
{{row?.address_to_locality}
更多
聊天室
查看
侧面图
查看任务
编辑配置文件
更改
地位
-
{{status.key}}
我想将所有这些行值导出到xlsx文件中如果我理解正确,这应该适用于您:
let resultArray = this.rows.map( {first_name, last_name, status, address_from_locality, address_to_locality} =>
([first_name, last_name, status, address_from_locality, address_to_locality]) );
如果您需要第一个内部数组来包含实际的文件名(我的意思是看起来像[“first\u name”、“last\u name”、“status”、“address\u from”、“address\u to”]
),请在下面的后面添加这一行:
resultArray.unshift(Object.keys(this.rows[0])代码>-->这将把元素
对象的键添加到数组的第一位
编辑:
另外,由于您是初学者,我在这里添加了一些资源,以便您更好地了解我所做的工作:
好的,我做完了
这是您需要的代码:
ExportToExcel() {
var filename = 'TalentTable' + '.xlsx';
var i = 0;
var TableData: string[][];
TableData = [];
this.rows.forEach(element => {
for (var i = 0; i < 10; i++) {
TableData[i] = [];
for (var j = 0; j < 4; j++) {
TableData[i][j] = null;
}
}
});
this.rows.forEach(element => {
TableData[i][0] = (element.first_name + ' ' + element.last_name);
TableData[i][1] = element.status;
TableData[i][2] = element.address_from_locality;
TableData[i][3] = element.address_to_locality;
i++;
});
var wscols = [
{ wpx: 150 },
{ wch: 30 },
];
const wb = xlsx.utils.book_new(), ws = xlsx.utils.aoa_to_sheet(TableData);
ws['!cols'] = wscols;
xlsx.utils.book_append_sheet(wb, ws);
xlsx.writeFile(wb, filename);
}
ExportToExcel(){
var filename='TalentTable'+'.xlsx';
var i=0;
var表格数据:字符串[];
TableData=[];
this.rows.forEach(元素=>{
对于(变量i=0;i<10;i++){
TableData[i]=[];
对于(var j=0;j<4;j++){
TableData[i][j]=null;
}
}
});
this.rows.forEach(元素=>{
TableData[i][0]=(element.first\u name+“”+element.last\u name);
TableData[i][1]=元素状态;
TableData[i][2]=元素地址来自位置;
TableData[i][3]=element.address\u to\u locality;
i++;
});
var wscols=[
{wpx:150},
{wch:30},
];
const wb=xlsx.utils.book_new(),ws=xlsx.utils.aoa_to_sheet(TableData);
ws['!cols']=wscols;
xlsx.utils.book\u-append\u-sheet(wb,ws);
xlsx.writeFile(wb,文件名);
}
从您编写的所有内容来看,我仍然不知道您的源数据看起来如何,以及您试图用它构建什么。很好,你告诉我们你尝试了什么,但是我们仍然需要更多关于你尝试做什么的信息。请把你的帖子写得更清楚:)哦,对不起,我心里很清楚,我会修改我的帖子的!:)那么您想要数组的数组,每个内部数组代表示例xlsx文件中的一行?如果你只是在JS结构中展示你期望的东西,这真的是最好的。。。比如“我得到的对象数组看起来像[{firstname:'a',lastname:'b',to:'destination',from:'source'},{firstname:'a2',lastname:'b2',to:'destination2',from:'source2'}]
我希望得到这样的数组:[['a','b','destination','source from'],['a2 b2 destination2','source2']]
只要给我们你想要转换的数据结构就行了。如果我在网站上看到它,那就没有帮助了,我仍然不知道它是如何存储在你的代码中的xlsx文件中例如,A1是第一行的名字,B1是第一行的状态,A2是第二行的名字,B2是第二行的状态,l就像我在excel屏幕上给出的例子一样,我阅读lmao需要的代码太多了。我只需要你console.log(element)
,这样我就可以看到它是什么样子。我现在找到了你,在一秒内为你构建一个解决方案我不需要一个循环来填充数组?我做console.log(element)就像你问的,这就是我所拥有的:不,array.map()
为你做这件事-它创建一个新的数组,其中每个元素都是在原始数组的相应元素上执行的某个函数(你提供的)的结果。它基本上是:newArray[x]=you\u func(oldArray[x])
。阅读我为您添加的源代码以阅读更多:)如果有更多问题,或者如果我的解决方案不是您所期望的,请随时在此处发表评论:)如果成功了,请不要忘记勾选绿色V并向上投票,谢谢:谢谢:D非常感谢,祝您愉快NP:)如果有帮助,请接受答案,以便其他用户可以看到此问题我们也做得很好