2011-10-31 3 views
5

내 ASP.NET MVC 3 응용 프로그램에서 내 포럼을 만들기 위해 Jquery의 pagination 플러그인을 사용하고 싶습니다 ...Jquery 페이지 매김과 히스토리 플러그인을 함께 사용하는 가장 효율적인 방법

그래서 Ajax로 모든 페이지 변경 작업을 수행하고 브라우저 작동을 위해 history 플러그인을 사용하고자합니다. 다음 버튼을 ... 그것에 대해

무엇을 가장 효율적으로 자바 스크립트/jQuery 코드?

클릭

$(document).ready(function() { 

     $.history.init(function (hash) { 

      if (hash != "") { 
       NavigateToPage(hash.substring(0,hash.indexOf("page")),hash.substring(hash.lastIndexOf("_")+1)); 
      } else { 
        if(firstTimeOpen==true){ 
         firstTimeOpen=false; 
        } 
        else 
        { 
         window.location.replace("/Forum"); 
        } 
      } 

     }); 

     $(".more-button").click(function() { 

      currentForumId=$("#pagination-td").parent().prev().attr("id").substring(5); 
      $.history.load($(this).parents("tr").attr("id").substring(5)+"page_1"); 

      }); 


     }); 


    function NavigateToPage(forumId,page) { 
      lastForumId=currentForumId; 
      currentForumId=forumId; 
      var elem=$("#forum"+forumId); 
      var elemStr="#forum"+forumId; 
      if($("#pagination-td").parent().prev().attr("id").substring(5)!=forumId) 
      { 
      forumChanged=true; 
      MakeChanges(elem,page); 
      } 
      else 
      { 
       if($(".pagination").html()==null) 
       { 
        if(elem.find("a").attr("id")>@MyProject.Constants.THREAD_COUNT) 
        { 
        $("#pagination-td").pagination(elem.find("a").attr("id"), { 
        items_per_page: @MyProject.Constants.THREAD_COUNT, 
        callback: handlePaginationClick 
        }); 
        } 
       } 
      } 


      $.get('/Forum/ForumThreads', { id: forumId, beginNumber: page - 1, count: @MyProject.Constants.THREAD_COUNT }, function (data) { 
        RemoveElements(elem.prev(), @MyProject.Constants.THREAD_COUNT); 
        elem.before(data); 
        elem.hide(); 
        $("tr:not("+elemStr+"):hidden").show(300); 

      }); 
      CorrectPagination(page); 

    } 

    function RemoveElements(element) 
    { 
     if (element.hasClass("forum-desc")) return false; 
     var prevElement=element.prev(); 
     element.remove(); 
     RemoveElements(prevElement); 

    } 

    function MakeChanges(elem,page) 
    { 

      var element=elem.prev(); 
      if(forumChanged==true) 
      { 

      $.get('/Forum/ForumThreads', { id: lastForumId, beginNumber:0, count: @MyProject.Constants.THREAD_VISIBLE_COUNT }, function (data) { 
        RemoveElements($("#pagination-td").parent().prev().prev()); 
        $("#pagination-td").parent().prev().before(data); 

        $("#pagination-td").parent().prev().hide(); 
        $("#pagination-td").parent().remove(); 
        elem.after('<tr style="display:none"><td id="pagination-td" colspan="3" style="border-bottom:none;"></td></tr>'); 
        if(elem.find("a").attr("id")> @MyProject.Constants.THREAD_COUNT) 
         { 
           $("#pagination-td").pagination(elem.find("a").attr("id"), { 
           items_per_page: @MyProject.Constants.THREAD_COUNT, 
           callback: handlePaginationClick 
           }); 
         } 
         $("tr:hidden").show(300); 
         CorrectPagination(page); 
         $("#pagination-td").parent().prev().hide(); 

      }); 
      } 
    } 

    function handlePaginationClick(new_page_index,pagination_container) 
    { 
     $.history.load(currentForumId+"page_"+(new_page_index+1)); 
     return false; 
    } 
function CorrectPagination(page) { 
    $(".pagination span.current:not(.prev):not(.next)").replaceWith('<a href="#">' + $(".pagination span.current:not(.prev):not(.next)").html() + '</a>'); 

    $(".pagination a:not(.prev):not(.next)").eq(page - 1).replaceWith('<span class="current">' + $(".pagination a:not(.prev):not(.next)").eq(page - 1).html() + "</span>"); 

    if ($(".pagination span.current:not(.prev):not(.next)").next().html() == "Next") { 
     $(".pagination span.prev").replaceWith('<a class="prev" href="#">Prev</a>'); 
     $(".pagination a.next").replaceWith('<span class="current next">Next</span>'); 

    } 
    else { 
     if ($(".pagination span.current:not(.prev):not(.next)").prev().html() == "Prev") { 
      $(".pagination a.prev").replaceWith('<span class="current prev" >Prev</span>'); 
      $(".pagination span.next").replaceWith('<a class="next" href="#">Next</a>'); 

     } 
     else { 
      $(".pagination span.prev").replaceWith('<a class="prev" href="#">Prev</a>'); 
      $(".pagination span.next").replaceWith('<a class="next" href="#">Next</a>'); 
     } 
    } 
} 

이 페이지에서 포럼의 쓰레드가 나는 (활성 스레드) 한 페이지 매김을 사용하는 ...이 코드 달 전에 쓴,하지만 지금은 나를 ugly..Check 보인다 더 다른 버튼은 현재 스레드는 스레드로 변경된다.

그래서 html로이

<table id="forum-table" class="border-top"> 
@foreach (var forum in Model) 
{ 

    <tr class="forum-desc"> 
    <td class="forum-name" colspan="4"><a class="label-h4 purple" href="/Forum/Forums/@forum.Key.Forum.Id">@forum.Key.Forum.Name</a> 
    @if (forum.Key.Forum.Description != null) 
    { 
     <span> - </span> 
     <span>@forum.Key.Forum.Description</span> 
    }</td> 
    </tr> 

    for(int index = 0; index < forum.Value.Count; index++) 
    { 
      <tr> 
      <td class="thread-pic"><img src="/img/forum/thread-icon.png" alt="" /></td> 
      <td class="thread-name"> 
      <a href="/Forum/Thread/@forum.Value[index].Thread.Id" class="blue linked">@forum.Value[index].Thread.Title</a> 
      </td> 
      <td class="thread-post-count"> 
      <span>Posts:</span>@forum.Value[index].PostCount 
      </td> 
      <td class="thread-information"> 
      <span>by </span><a class="blue" href="/Home/UserProfile/@forum.Value[index].LastPostUserName">@forum.Value[index].LastPostUserName</a> <br /> 
      @if (forum.Value[index].LastPostDate != DateTime.MinValue) 
      { 
       @forum.Value[index].LastPostDate 
      } 
      </td> 
      </tr> 
      } 

      if (forum.Key.ThreadCount > @MyProject.Constants.THREAD_VISIBLE_COUNT) 
      { 
       <tr id="[email protected](forum.Key.Forum.Id)"> 
       <td class="more-td"> 
       <a href="javascript:void" class="blue linked more-button" id="@forum.Key.ThreadCount">more</a> 
       </td> 
       </tr> 
      } 
      if (forum.Key.Forum.Id == Model.Keys.FirstOrDefault().Forum.Id) 
      { 
       <tr style="display:none"> 
       <td id="pagination-td" colspan="3" style="border-bottom:none;"></td> 
       </tr> 
      } 
      if (forum.Key.ThreadCount == 0) 
      { 
       <tr> 
       <td colspan="4"><span>No threads available in this forum</span></td> 
       </tr> 
      } 

} 
</table> 

그래서이 두 플러그인을 혼합하기위한 가장 짧은 (효율적인) javascript/jquery 코드 싶습니다.

+1

지금까지 시도한 코드를 게시하여 토론의 공통 기반을 마련하고 개선 할 수있는 방법을 확인하십시오 (필요한 경우)? –

+0

예, 당연히 지금 제 질문을 편집 할 것입니다.)) –

+0

방금 ​​질문을 편집했습니다 ... –

답변

2

딜런, 나는 ... closest()parent().prev()는 큰 문제가 아무것도 아닌 변화하는 생각에 인라인 CSS를 이동 .. . 예를 들어이

한 번 봐 ... 그 함수 내가 그것을 위해보다 효율적인 slutuion을 발견 그 질문에

function CorrectPagination(page) { 
$(".pagination span.current:not(.prev):not(.next)").replaceWith('<a href="#">' + $(".pagination span.current:not(.prev):not(.next)").html() + '</a>'); 

$(".pagination a:not(.prev):not(.next)").eq(page - 1).replaceWith('<span class="current">' + $(".pagination a:not(.prev):not(.next)").eq(page - 1).html() + "</span>"); 

if ($(".pagination span.current:not(.prev):not(.next)").next().html() == "Next") { 
    $(".pagination span.prev").replaceWith('<a class="prev" href="#">Prev</a>'); 
    $(".pagination a.next").replaceWith('<span class="current next">Next</span>'); 

} 
else { 
    if ($(".pagination span.current:not(.prev):not(.next)").prev().html() == "Prev") { 
     $(".pagination a.prev").replaceWith('<span class="current prev" >Prev</span>'); 
     $(".pagination span.next").replaceWith('<a class="next" href="#">Next</a>'); 

    } 
    else { 
     $(".pagination span.prev").replaceWith('<a class="prev" href="#">Prev</a>'); 
     $(".pagination span.next").replaceWith('<a class="next" href="#">Next</a>'); 
    } 
} 
} 

를 살펴 6,

jquery history plugin set current page possible?

은 적은 코드

var panel = jQuery(this); 
// Attach control functions to the DOM element 
this.selectPage = function(page_id){ pageSelected(page_id);} 

과 같은 기능이 있습니다 그리고

$("#Pagination")[0].selectPage(9); //0-based, 9 = page 10 

는 다른 기능이 너무 짧은 방법이 될 수있다 위해의이 ... 자신을 확인하자 ...

+0

감사합니다 Artur Keyan ... 정말 도움이되었습니다 ... 그 기능에 대한 귀하의 제안을 듣게되어 기쁘게 생각합니다. 내 자바 스크립트 코드도 ... –

4

전체적으로보기 흉하게 접근합니다. jQuery를 사용하면 더 많은 코드를 필요로하는 고급 자손 및 인접 선택기를 사용하여 체인을 더 많이 만들고 변경을 배울 수 있습니다.

대부분의 기능은 약간 장황하지만 작업을 보여주는 것이므로 나쁜 것은 아닙니다.

이 블록은 많은 개선의 여지가 있습니다.

RemoveElements($("#pagination-td").parent().prev().prev()); 
$("#pagination-td").parent().prev().before(data); 

$("#pagination-td").parent().prev().hide(); 
$("#pagination-td").parent().remove(); 
elem.after('<tr style="display:none"><td id="pagination-td" colspan="3" style="border-bottom:none;"></td></tr>'); 

당신은 closest() 대신 .parent().prev()의를 사용하여 해당을 정리 할 수 ​​있습니다. 나는 당신이 목표로하고있는 것을 정확히 따라갈 수는 없지만 이전 테이블 행을 찾고 있다고 생각합니다. 나는 당신이 숨기고 제거하려고하는 것을 정확하게 말할 수는 없지만 closest()를 사용하면 숨기기를 도와주고 애니메이션을 적용하여 뭔가를 제거하기 전에 완료 할 때까지 기다릴 수 있습니다.

RemoveElements($("#pagination-td").closest(".thread")); 
$("#pagination-td").closest(".thread").before(data); 

$("#pagination-td").closest(".thread").hide('fast', function() { 
    $("#pagination-td").closest(".itemToRemove").remove(); }); 

$('#formTable tr:last').after('<tr class="hidden"><td id="pagination-td" colspan="3"></td></tr>'); 

지금 큰 코드와 같은 큰 문제가 있습니다 스타일 시트

#pagination-td { border-bottom: none; } 
관련 문제