将exactPageList分配给pagefield时如何将其从any转换为any []?
How to convert exactPageList from any to any[] when assign it to pagefield?
我在开发 angular 7 个应用程序 我在分页时遇到问题无法将 exactpagelist any 分配给 pagefield any[] .
因为 pagefield 是 array 类型,而 exactpagelist 是 any 类型。
这一行的函数 totalNoOfPages 的最后一行存在问题
this.pageField = this.exactPageList;
this.pageField = 2; not correct
我预计会
this.pageField = [1,2];
意味着我需要转换 this.exactPageList;到要接受的数组分配给页面字段
怎么做?
pageField:any[];
exactPageList:any;
totalNoOfPages() {
this.paginationData = Number(this.totalReportCount / this.ReportPerPage);
console.log("pagination data :" + this.paginationData)
let tempPageData = this.paginationData.toFixed();
console.log("tempPageData data :" + tempPageData)
if (Number(tempPageData) < this.paginationData) {
this.exactPageList = Number(tempPageData) + 1;
this.paginationService.exactPageList = this.exactPageList;
console.log("exactPageList1 data :" + this.exactPageList )
} else {
this.exactPageList = Number(tempPageData);
this.paginationService.exactPageList = this.exactPageList
console.log("exactPageList2 data" + this.exactPageList )
}
this.paginationService.pageOnLoad();
this.pageField = this.exactPageList;
}
上面代码的结果如下:
pagination data1.0666666666666667
reportdetails.component.ts:265 tempPageData data1
reportdetails.component.ts:269 exactPageList1 data2
reportdetails.component.ts:263 pagination data1.0666666666666667
reportdetails.component.ts:265 tempPageData data1
reportdetails.component.ts:269 exactPageList1 data2
预期结果
this.pageField = [1,2];
使用评论的一些解释:
this.paginationData = Number(this.totalReportCount / this.ReportPerPage);
console.log("pagination data :" + this.paginationData)
// Use Math.round instead. Then you get an numeral and not a string.
let tempPageData = this.paginationData.toFixed();
console.log("tempPageData data :" + tempPageData)
// Then Number() is not necessary here anymore.
// Moreover as far as I understand, you actually want to use always the 'bigger number'.
// That means, actually it would make sense to use Math.ceil() above.
// In that case, you don't need to use a condition at all.
if (Number(tempPageData) < this.paginationData) {
this.exactPageList = Number(tempPageData) + 1;
this.paginationService.exactPageList = this.exactPageList;
console.log("exactPageList1 data :" + this.exactPageList )
} else {
// You are doing almost the same like in the condition clause above.
// It would be enough to set this.exactPageList condtionally and do the rest afterwards.
this.exactPageList = Number(tempPageData);
this.paginationService.exactPageList = this.exactPageList
console.log("exactPageList2 data" + this.exactPageList )
}
this.paginationService.pageOnLoad();
this.pageField = this.exactPageList;
我会做什么:
this.paginationData = Number(this.totalReportCount / this.ReportPerPage);
console.log("pagination data :", this.paginationData)
let tempPageData = Math.ceil(this.paginationData);
console.log("tempPageData data :", tempPageData)
// Let's create an array from 1 .. N
// [...Array(4).keys()] would create an array with [0,1,2,3]
// but we want to have [1,2,3,4], that's why we are incrementing every value by using map().
// Think about moving this to an own method 'getOneToNArray' or so for a better readability.
this.exactPageList = [...Array(tempPageData).keys()].map(x => ++x);;
this.paginationService.exactPageList = this.exactPageList;
console.log("exactPageList data :", this.exactPageList )
this.paginationService.pageOnLoad();
this.pageField = this.exactPageList;
我在开发 angular 7 个应用程序 我在分页时遇到问题无法将 exactpagelist any 分配给 pagefield any[] .
因为 pagefield 是 array 类型,而 exactpagelist 是 any 类型。
这一行的函数 totalNoOfPages 的最后一行存在问题
this.pageField = this.exactPageList;
this.pageField = 2; not correct
我预计会
this.pageField = [1,2];
意味着我需要转换 this.exactPageList;到要接受的数组分配给页面字段 怎么做?
pageField:any[];
exactPageList:any;
totalNoOfPages() {
this.paginationData = Number(this.totalReportCount / this.ReportPerPage);
console.log("pagination data :" + this.paginationData)
let tempPageData = this.paginationData.toFixed();
console.log("tempPageData data :" + tempPageData)
if (Number(tempPageData) < this.paginationData) {
this.exactPageList = Number(tempPageData) + 1;
this.paginationService.exactPageList = this.exactPageList;
console.log("exactPageList1 data :" + this.exactPageList )
} else {
this.exactPageList = Number(tempPageData);
this.paginationService.exactPageList = this.exactPageList
console.log("exactPageList2 data" + this.exactPageList )
}
this.paginationService.pageOnLoad();
this.pageField = this.exactPageList;
}
上面代码的结果如下:
pagination data1.0666666666666667
reportdetails.component.ts:265 tempPageData data1
reportdetails.component.ts:269 exactPageList1 data2
reportdetails.component.ts:263 pagination data1.0666666666666667
reportdetails.component.ts:265 tempPageData data1
reportdetails.component.ts:269 exactPageList1 data2
预期结果
this.pageField = [1,2];
使用评论的一些解释:
this.paginationData = Number(this.totalReportCount / this.ReportPerPage);
console.log("pagination data :" + this.paginationData)
// Use Math.round instead. Then you get an numeral and not a string.
let tempPageData = this.paginationData.toFixed();
console.log("tempPageData data :" + tempPageData)
// Then Number() is not necessary here anymore.
// Moreover as far as I understand, you actually want to use always the 'bigger number'.
// That means, actually it would make sense to use Math.ceil() above.
// In that case, you don't need to use a condition at all.
if (Number(tempPageData) < this.paginationData) {
this.exactPageList = Number(tempPageData) + 1;
this.paginationService.exactPageList = this.exactPageList;
console.log("exactPageList1 data :" + this.exactPageList )
} else {
// You are doing almost the same like in the condition clause above.
// It would be enough to set this.exactPageList condtionally and do the rest afterwards.
this.exactPageList = Number(tempPageData);
this.paginationService.exactPageList = this.exactPageList
console.log("exactPageList2 data" + this.exactPageList )
}
this.paginationService.pageOnLoad();
this.pageField = this.exactPageList;
我会做什么:
this.paginationData = Number(this.totalReportCount / this.ReportPerPage);
console.log("pagination data :", this.paginationData)
let tempPageData = Math.ceil(this.paginationData);
console.log("tempPageData data :", tempPageData)
// Let's create an array from 1 .. N
// [...Array(4).keys()] would create an array with [0,1,2,3]
// but we want to have [1,2,3,4], that's why we are incrementing every value by using map().
// Think about moving this to an own method 'getOneToNArray' or so for a better readability.
this.exactPageList = [...Array(tempPageData).keys()].map(x => ++x);;
this.paginationService.exactPageList = this.exactPageList;
console.log("exactPageList data :", this.exactPageList )
this.paginationService.pageOnLoad();
this.pageField = this.exactPageList;