0

I work on angular 7 app I face issue on paging cannot assign exactpagelist any to pagefield any[] .

because pagefield is type array and exactpagelist is type any .

issue exist on last line of function totalNoOfPages on this line

this.pageField = this.exactPageList;
this.pageField = 2; not correct

I expected to be

this.pageField = [1,2];

meaning i need to convert this.exactPageList; to array to be accepted assign to pagefield How to do that ?

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;    

  }  

Result of code above as below :

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

Expected result

this.pageField = [1,2];
abo elham
  • 51
  • 1
  • 7
  • 1
    I'm not sure, what you actually want, but you can create an array with a single number entry by doing `this.pageField = [ this.exactPageList ]` - even though I would assume, that `this.exactPageList` is an array and no number due to its name. I would either already define `exactPageList` as array or rename it. – Paul May 29 '20 at 17:53
  • Please explain what this.exactPageList contains. I echo @Paul. If it contains array of value Paul's answer will work. – vinothvs May 29 '20 at 17:55
  • It does contain a number. Check the lines `this.exactPageList = Number(tempPageData) + 1; ` and `this.exactPageList = Number(tempPageData)` What I don't unterstand is, why it should contain `[1, 2]`, where are those number come from? – Paul May 29 '20 at 17:56
  • exactpagelist with type any contain number as 2 i need to be [1,2] to assign to pagefield – abo elham May 29 '20 at 17:56
  • So, if `exactpagelist` was `4` should `pageField = [1, 2, 3, 4]` then? – Paul May 29 '20 at 17:58
  • yes this correct – abo elham May 29 '20 at 17:58
  • Then check this question: https://stackoverflow.com/questions/3746725/how-to-create-an-array-containing-1-n – Paul May 29 '20 at 18:00
  • until now cannot do that can any one help me on solving – abo elham May 29 '20 at 18:04

1 Answers1

0

Some explanation by using comments:

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; 

What I would do:

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; 
Paul
  • 2,086
  • 1
  • 8
  • 16