paggination: Как получить текущее значение страницы для следующей и предыдущей функции в angular 7? - PullRequest
0 голосов
/ 24 марта 2020

Здравствуйте, я новый ученик angular. Я делаю пользовательские страницы самостоятельно. я не получил никакой ссылки, как я получу текущую страницу для следующей и предыдущей функции.

 <div class="col-lg-6"> 
 <label for="pages">how many items do you want to see? : </label>
    <select id="pages" name="pages"  (change)="filter($event.target.value)" [(ngModel)]="selectedOption">
       <option value="2">2</option>
       <option value="4">4</option>
       <option value="6">6</option>
       <option value="8">8</option>
      </select>
        <table class="table">
            <thead>
            <tr>
                <th>#</th>
                <th>Name</th>
                <th>Email</th>
                <th>Details</th>
                <th>phoneNo</th>
                <th></th>
            </tr>
            </thead>
            <tbody>
            <tr *ngFor="let emp of indexArray; let i=index;">
                <th scope="row">{{emp.id}}</th>
                <td>{{emp.name}}</td>
                <td>{{emp.email}}</td>
                <td>{{emp.details}}</td>
                <td>{{emp.phoneNo}}</td>
                <td> 
                    <button type="button" class="btn btn-secondary" (click) = "edit(i)">Edit</button> 
                    <button type="button" class="btn btn-secondary" (click) = "delete(i)">Delete</button>
                 </td>
            </tr>
        </tbody>
    </table>




 <nav aria-label="Page navigation example">
                <ul class="pagination">
                 <li class="page-item">
                    <a class="page-link"  aria-label="Previous">
                      <span aria-hidden="true">&laquo;</span>
                      <span class="sr-only" (click)="Previous()">Previous</span>
                    </a>
                  </li>
               <li class="page-item" *ngFor="let hero of pageArray">
                      <a class="page-link" (click) = "indexData(hero)">{{ hero }}</a> </li>
                <li class="page-item">
                    <a class="page-link"  aria-label="Next" (click) = "next(currentPage)">
                      <span aria-hidden="true">&raquo;</span>
                      <span class="sr-only" >Next</span>
                    </a>
                  </li>
                </ul>
              </nav>
    </div> 

** МОЙ код машинописного текста **

employeDetail: any = [];
employe = {name: '', email: '', details: '', phoneNo: ''};
employes;
index;
Tpage: any; // total page
nPage; // current page
Tvalue; // total value of student
Items ; // Item per page
pageArray = [];
indexArray = [];
stratIndex;
endIndex;
selectedOption: string;
currPage: string;


page() {
  this.employes =  JSON.parse(localStorage.getItem('employeDetail'));
  this.Items =  JSON.parse(localStorage.getItem('itemsperpage'));
  this.Tvalue = this.employes.length;
  this.Tpage = this.Tvalue / this.Items;
 // console.log(this.Tpage);
    if (Number.isInteger(this.Tpage)) {
      this.Tpage = this.Tpage;
    } else {
      this.Tpage = Math.ceil(this.Tpage );
    }
    for (let i = 1; i <= this.Tpage ; i++) {
        this.pageArray.push(i);
      }
      this.selectedOption = this.Items;
}

I хочу значение currpage для следующей и предыдущей функции !!

next(currPage) {
      console.log(currPage);
    alert('kk');
    }
   previous(currPage) {
      console.log(currPage);
    alert('kk');
    }

** я получаю текущее значение страницы в этой функции **

 indexData(currentPage) {
          this.indexArray = [];
          this.currPage = currentPage;
        this.Items =  JSON.parse(localStorage.getItem('itemsperpage'));
        this.stratIndex = (currentPage - 1) * this.Items;
        // console.log(this.stratIndex);
        this.endIndex = parseInt(this.stratIndex) + parseInt(this.Items) ;
        this.endIndex = this.endIndex - 1;
        for (let i = this.stratIndex ; i <=  this.endIndex ; i++) {
          if (this.employes[i].length === '') {
          //  console.log('kk');
          } else {
        this.indexArray.push(this.employes[i]);
          }
        }

        }

** эта функция используется для фильтр по элементу на странице **

   filter(val) {
          localStorage.setItem('itemsperpage', JSON.stringify(val));
          this.indexArray = [];
          this.pageArray = [];
          this.endIndex = val - 1 ;
          for (let i = 0 ; i <=  this.endIndex ; i++) {
            this.indexArray.push(this.employes[i]);
            }

            this.Tpage = this.Tvalue / val;
            if (Number.isInteger(this.Tpage)) {
              this.Tpage = this.Tpage;
            } else {
              this.Tpage = Math.ceil(this.Tpage );
            }
            for (let i = 1; i <= this.Tpage ; i++) {
              this.pageArray.push(i);
            }
        }

        constructor() { }
       ngOnInit() {
        this.page();
             for (let i = 1; i <= this.Items ; i++) {
                 this.indexArray.push(this.employes[i]);
              }
        }

        createData(employe) {
          let id = 0;
          let employeDetail = JSON.parse(localStorage.getItem('employeDetail'));
          if (employeDetail === null || employeDetail === 0) {
           employeDetail = [];
           id = 0;
          } else {
           id = employeDetail[employeDetail.length - 1].id;
         }
        id++;
        employe.id = id;
        // this.employes = employeDetail.push(employe); this will return number of push employes.!!! so we will directly use employedetails
        employeDetail.push(employe);
         this.employes = employeDetail;
        localStorage.setItem('employeDetail', JSON.stringify(employeDetail));
        }
        }

1 Ответ

0 голосов
/ 25 марта 2020

Я получил ответ:

currPage: any = 1;

    next() {
      this.currPage = this.currPage + 1;
      this.indexArray = [];
      this.Items =  JSON.parse(localStorage.getItem('itemsperpage'));
      this.stratIndex =  (this.currPage - 1) * this.Items;
      this.endIndex = parseInt(this.stratIndex) + parseInt(this.Items) ;
      this.endIndex = this.endIndex - 1;
    for (let i = this.stratIndex ; i <=  this.endIndex ; i++) {
      if (this.employes[i].length === '') {
      } else {
    this.indexArray.push(this.employes[i]);
      }
    }

    }

здесь я уже получаю значение текущей страницы: так что здесь я должен присвоить значение this.currpage == cuurentPage

indexData(currentPage) {
  this.indexArray = [];
  **this.currPage = currentPage;**
this.Items =  JSON.parse(localStorage.getItem('itemsperpage'));
this.stratIndex = (currentPage - 1) * this.Items;
this.endIndex = parseInt(this.stratIndex) + parseInt(this.Items) ;
this.endIndex = this.endIndex - 1;
for (let i = this.stratIndex ; i <=  this.endIndex ; i++) {
  if (this.employes[i].length === '') {
  } else {
this.indexArray.push(this.employes[i]);
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...