pagination JS vs Java

Jiwontwopunch·2022년 1월 3일
0

TIL

목록 보기
23/92
post-thumbnail

JS 전체 코드

// pageno, pagesize, totalcount, pagePerBlock;
// /contacts?pageno=1&pagesize=10
const PAGE_SIZE = 10;
const PAGE_PER_BLOCK = 5;

function getPagination(pageno, totalcount) {
   // countOfPage -> blockNo -> sp, ep
  let countOfPage = Math.floor(totalcount/PAGE_SIZE)+1;
  if(totalcount%PAGE_SIZE==0) {
      countOfPage--;
  }
            
  let blockNo = Math.floor(pageno/PAGE_PER_BLOCK);
  if(pageno%PAGE_PER_BLOCK==0) {
          blockNo--;
  }

const startPage = blockNo * PAGE_PER_BLOCK + 1;
const prev = startPage - 1;
let endPage = startPage + PAGE_PER_BLOCK - 1;
let next = endPage + 1;

if(endPage>=countOfPage) {
     endPage = countOfPage;
     next = 0;
}

// prev, sp, ep, next를 리턴해야 한다
// 여러개의 값을 리턴할 수는 없다 
// -> 객체를 만들어서 리턴
// return { prev: prev, startPage : startPage,
//          endPage: endPage, next: next };

// ES6에서는 객체 속성이름과 값을 저장한 변수 이름이 같을 때
// 아래처럼 짧게 적을 수 있다 -> 구조분해할당
return { prev, startPage, endPage, next};
        }

const pagination = getPagination(11, 123);
        console.log(pagination);

Java 전체 코드

package com.icia.example2;

class Pagination {
	int prev;
	int startPage;
	int endPage;
	int next;
}
public class PagingUtil {
   final int PAGE_SIZE = 10;
   final int PAGE_PER_BLOCK = 5;
	
// prev, startPage, endPage, next
Pagination getPagination(int pageno, int totalcount) {
  // countOfPage
  int countOfPage = totalcount/PAGE_SIZE + 1;
  if(totalcount%PAGE_SIZE==0) {
	countOfPage--;
  }
		
// blockNo 계산
int blockNo = pageno/PAGE_PER_BLOCK;
if(pageno%PAGE_PER_BLOCK==0) {
	blockNo--;
}
		
Pagination p = new Pagination();
p.startPage = blockNo * PAGE_PER_BLOCK + 1;
p.prev = p.startPage - 1;
p.endPage = p.startPage + PAGE_PER_BLOCK - 1;
p.next = p.endPage + 1;
if(p.endPage>=countOfPage) {
	p.endPage = countOfPage;
	p.next = 0;
}
		
return p;
}
}

JS와 Java 코드의 차이점

// JS
const PAGE_SIZE = 10;
const PAGE_PER_BLOCK = 5;
// Java
// 자료형 필수
final int PAGE_SIZE = 10;
final int PAGE_PER_BLOCK = 5;
// class에 접근하려면 new 객체부터 생성
Pagination p = new Pagination();
p.startPage = blockNo * PAGE_PER_BLOCK + 1;
p.prev = p.startPage - 1;
p.endPage = p.startPage + PAGE_PER_BLOCK - 1;
p.next = p.endPage + 1;

0개의 댓글