티스토리 뷰

카테고리 없음

페이징 클래스

James Wetzel 2014. 4. 13. 14:33
728x90
반응형

Model    

class Paging

{

    private int currentPageNo;

    private int totalPageNo;

    private int startPageNo;

    private int endPageNo;


    public int CurrentPageNo { get { return this.currentPageNo; } }

    public int TotalPageNo { get { return this.totalPageNo; } }

    public int StartPageNo { get { return this.startPageNo; } }

    public int EndPageNo { get { return this.endPageNo; } }        


    public Paging(int totalRowCount, int pageSize, int currentPageNo, int pagingSectionSize)

    {   

        int totalPageNo = (int)Math.Ceiling((double)totalRowCount / pageSize);

        int currentPagingSection = (int)Math.Ceiling((double)currentPageNo / pagingSectionSize);

        int startPageNo = ((currentPagingSection - 1) * pagingSectionSize) + 1;

        int endPageNo = (startPageNo + pagingSectionSize) - 1 <= totalPageNo ? (startPageNo + pagingSectionSize) - 1 : totalPageNo;


        this.currentPageNo = currentPageNo;

        this.totalPageNo = totalPageNo;

        this.startPageNo = startPageNo;

        this.endPageNo = endPageNo;

    }        

}


Controller

Paging paging = new Paging((int)param.TotalCount, facilitiesPageSize, currentPageNo, facilitiesPagingSectionSize);

ViewData["currentPageNo"] = paging.CurrentPageNo;

ViewData["totalPageNo"] = paging.TotalPageNo;

ViewData["startPageNo"] = paging.StartPageNo;

ViewData["endPageNo"] = paging.EndPageNo;

ViewData["totalCount"] = param.TotalCount;


return View();


View

<%

int startPageNo = (int)ViewData["startPageNo"];

int endPageNo = (int)ViewData["endPageNo"];

int currentPageNo = (int)ViewData["currentPageNo"];

int totalPageNo = (int)ViewData["totalPageNo"];


string preViewLink = currentPageNo > 1 ? Url.Action("RoomServiceList", new { currentPageNo = currentPageNo - 1 }) : "javascript:alert('이전&nbsp;페이지가&nbsp;존재하지않습니다.')";

string preView = string.Format("<a href={0}>이전</a>", preViewLink);

Response.Write(preView);


for (int i = startPageNo; i <= endPageNo; i++)

{

string pagingForm = string.Format("<a href={0} style='padding:0px 5px 0px 5px'>{1}</a>", Url.Action("RoomServiceList", new{ currentPageNo=i }), i);

Response.Write(pagingForm);

}


string nextViewLink = currentPageNo < totalPageNo ? Url.Action("RoomServiceList", new { currentPageNo = currentPageNo + 1 }) : "javascript:alert('다음&nbsp;페이지가&nbsp;존재하지않습니다.')";

string nextView = string.Format("<a href={0}>다음</a>", nextViewLink);

Response.Write(nextView);

%>


Query

/*

작성자 : 장정훈

설명 : 자유 게시판 목록


exec FreeBoard_List 1, 10, '', ''

*/

ALTER PROCEDURE dbo.FreeBoard_List

@currentPage int,

@pageSize int,

@searchType varchar(10),

@searchWord varchar(20),

@totalCount bigint output

AS

select @totalCount = count(*)

from FreeBoard with(nolock)

where 1=1 and

(

@searchType = 'subject' and @searchWord is not null and subject like '%'+@searchWord+'%'

or

@searchType = 'contents' and @searchWord is not null and contents like '%'+@searchWord+'%'

or

@searchType = 'subject' and @searchWord = '' and 1=1

or 

@searchType = 'contents' and @searchWord = '' and 1=1

or

@searchType = '' and @searchWord = '' and 1=1

)


select *

from

(

select 

row_number() over(order by seq asc) as No

, row_number() over(order by seq desc) as subSEQ

, seq

, subject

, contents

, writer

, regDate

from FreeBoard with(nolock)

where 1=1 and

(

@searchType = 'subject' and @searchWord is not null and subject like '%'+@searchWord+'%'

or

@searchType = 'contents' and @searchWord is not null and contents like '%'+@searchWord+'%'

or

@searchType = 'subject' and @searchWord = '' and 1=1

or 

@searchType = 'contents' and @searchWord = '' and 1=1

or

@searchType = '' and @searchWord = '' and 1=1

)

) as tempTable

where tempTable.subSEQ between ((@currentPage -1) * @pageSize) + 1 and @currentPage * @pageSize

728x90
반응형