2016-09-09 69 views
0

我試圖構建ASP.NET MVC分頁系統;部分查看搜索表單,顯示數據的表格和分頁。我擁有這一切奠定了它還挺作品。我不是很好用jQuery,所以我真的不知道我要去的地方錯這個......後AJAX和後重新綁定jQuery的事件,jQuery的不再起作用

發帖時使用AJAX,分頁停止更新用戶界面後工作。我使用名爲bindAllEvents()的函數重新綁定了我的jQuery事件,但它似乎並未重新綁定我的事件。我的模型是第一次調用後更新,並且在第二次嘗試移動到使用「下一個」 >鏈接叫我的控制器的操作方法下一個頁面,但沒有在UI的變化。

jQuery的:

$(document).ready(function() { 
    bindAllEvents(); 
}); 

function pageEvent() { 
    this.goToPage = function() { 
    // Not yet implemented 
    return false; 
    } 
} 

function nextEvent(nextPage) { 
    this.nextPage = function() { 

    var url = '@Url.Action("Manage")' 
    var data = @Html.Raw(Json.Encode(Model.Data)); 
    var categories = @Html.Raw(Json.Encode(Model.Categories)); 
    var _page = @ (Model.pager.CurrentPage + 1); 

    var _model = { 
     Data: data, 
     Categories: categories, 
    }; 

    $.ajax({ 
     type: "POST", 
     contentType: "application/json; charset=utf-8", 
     url: url, 
     data: JSON.stringify({ 
     model: _model, 
     page: _page 
     }), 
     success: function(result) { 
     //console.log("Success " + result); 
     $('div[id="pagination"]').html($(result).find('div[id="pagination"]')); 
     $('div[id="full-table"]').html($(result).find('div[id="full-table"]')); 
     $('div[id="search-form"]').html($(result).find('div[id="search-form"]')); 
     bindAllEvents(); 
     }, 
     error: function(result) { 
     console.log("Error: " + result.responseText); 
     } 
    }); 
    } 
} 

function bindAllEvents() { 
    $('#date-boxes').slideUp(1); 

    // Have also tried $('body').on('click', '#next', ...) with no success 
    $('div[id="pagination"]').on('click', '#next', function(e) { 
    e.preventDefault(); 
    var onNextClickEvent = new nextEvent(); 
    onNextClickEvent.nextPage(@(Model.pager.CurrentPage + 1)); 
    }); 

    $("a[name='page-number']").on("click", function(e) { 
    e.preventDefault(); 
    var onPageClick = new pageEvent(); 
    onPageClickEvent.goToPage($(this).attr('id')); 
    }); 
} 

併爲分頁的局部視圖的HTML如下

@if (Model.EndPage > 1) 
{ 
    <div style="color:#337AB7; padding-bottom: 0px;" id="page-counter">Page @Model.CurrentPage of @Model.TotalPages</div> 
    <ul class="pagination"> 
    @if (Model.CurrentPage > 1) 
    { 
     <li> 
      @Html.ActionLink("<<", "Manage", null, new { id = "first" }) 
     </li> 
     <li> 
      @Html.ActionLink("<", "Manage", null, new { id = "prev" }) 
     </li> 
    } 
    @for (var _page = Model.StartPage; _page < Model.EndPage + 1; _page++) 
    { 
     <li class="@(_page == Model.CurrentPage ? "active" : "")"> 
      @Html.ActionLink(_page.ToString(), "Manage", null, new { name = "page-number", id = _page }) 
     </li> 
    } 
    @if (Model.CurrentPage < Model.TotalPages) 
    { 
     <li> 
      @Html.ActionLink(">", "Manage", "Announcements", null, new { id = "next" }) 
     </li> 
     <li> 
      @Html.ActionLink(">>", "Manage", null, new { id = "last" }) 
     </li> 
    } 
    </ul> 
} 

我將包括只是爲了完整性局部視圖的觀點,但我不我認爲這太需要了。

<div id="search-form"> 
    @{ Html.RenderPartial("_SearchForm", Model.Categories); } 
</div> 
<hr /> 
@if (Model.Data.Count == 0) 
{ 
    <h2>No existing announcements.</h2> 
} 
else 
{ 
    <div class="form-group" id="full-table"> 
     @{ Html.RenderPartial("_Manage-AncmtListing", Model.Data); } 
    </div> 
    <br /> 
    <div id="pagination"> 
     @{ Html.RenderPartial("_Pager", Model.pager); } 
    </div> 
} 

Before ajax call
After ajax call

+0

這不會解決您的具體問題,但我注意到,你沒有做任何事要傳遞到參數nextPage()函數,因爲nextPage()甚至沒有定義任何參數。通過在您的NextEvent函數定義中定義,該參數錯位。 – mhodges

+0

調試包含這種服務器端代碼的JavaScript非常困難。請包括一個呈現代碼的例子。 –

+0

另外,爲什麼在成功的ajax調用之後重新綁定事件?這將爲每個匹配的元素創建第二個事件處理程序。 – mhodges

回答

0

好了,所以沒有完全重構你的代碼,我也得到了類似你怎麼有它的工作狀態。您可能需要調整網頁最初設置的方式,但這個想法對您有用。你的主要問題是你沒有更新或傳遞下一個頁面號到你的函數,所以它重複使用相同的頁碼或每次都使用未定義的頁碼。有一些小的改動,所以要密切注意下面的代碼:

'use strict'; 
$(document).ready(function() { 
    bindAllEvents(); 
}); 

function nextEvent() { 
    this.nextPage = function(page) { 
    var url = '/Announcements/Manage' 
    var data = [{"WATypeName":"Web Announcements","Categories":[{"ID":6,"Name":"Physician/Medical","IsSelected":true}],"WAID":1,"WADate":"\/Date(1460959200000)\/","WANum":"1123","Topic":"Online Provider Enrollment Summary Page Updated","Link":"web_announcement_1123_20160418.pdf","ToolTip":"Online Provider Enrollment Summary Page Updated","WATypeID":1},{"WATypeName":"Web Announcements","Categories":[{"ID":11,"Name":"All Providers","IsSelected":true}],"WAID":2,"WADate":"\/Date(1460613600000)\/","WANum":"1122","Topic":"Providers Invited to Complete Health Information Exchange Small Business Impact Questionnaire by April 22, 2016","Link":"web_announcement_1122_20160414.pdf","ToolTip":"Providers Invited to Complete Health Information Exchange Small Business Impact Questionnaire by April 22, 2016","WATypeID":1},{"WATypeName":"Web Announcements","Categories":[{"ID":8,"Name":"Durable Medical Equipment (DME)","IsSelected":true}],"WAID":3,"WADate":"\/Date(1460613600000)\/","WANum":"1121","Topic":"Attention Provider Type 12: Claims for CPT Codes with Age Restrictions Will Be Enforced","Link":"web_announcement_1121_20160414.pdf","ToolTip":"Attention Provider Type 12: Claims for CPT Codes with Age Restrictions Will Be Enforced","WATypeID":1},{"WATypeName":"Web Announcements","Categories":[{"ID":4,"Name":"Dental/Orthodontia","IsSelected":true}],"WAID":4,"WADate":"\/Date(1460527200000)\/","WANum":"1120","Topic":"Attention Provider Types 24 and 77: Radiology Codes Billable Effective January 1, 2016","Link":"web_announcement_1120_20160413.pdf","ToolTip":"Attention Provider Types 24 and 77: Radiology Codes Billable Effective January 1, 2016","WATypeID":1},{"WATypeName":"Web Announcements","Categories":[{"ID":2,"Name":"Outpatient","IsSelected":true},{"ID":6,"Name":"Physician/Medical","IsSelected":true}],"WAID":5,"WADate":"\/Date(1460527200000)\/","WANum":"1119","Topic":"Attention Provider Type 75: Rates for Psychiatric/Substance Abuse Services Updated","Link":"web_announcement_1119_20160413.pdf","ToolTip":"Attention Provider Type 75: Rates for Psychiatric/Substance Abuse Services Updated","WATypeID":1},{"WATypeName":"Web Announcements","Categories":[{"ID":11,"Name":"All Providers","IsSelected":true}],"WAID":6,"WADate":"\/Date(1460354400000)\/","WANum":"1118","Topic":"EDI Medicare Crossover Claims Notification","Link":"web_announcement_1118_20160411.pdf","ToolTip":"EDI Medicare Crossover Claims Notification","WATypeID":1},{"WATypeName":"Web Announcements","Categories":[{"ID":6,"Name":"Physician/Medical","IsSelected":true}],"WAID":7,"WADate":"\/Date(1460095200000)\/","WANum":"1117","Topic":"Claims for ICD-10 「Z」 Codes and 「O09」 Codes Billed as the Primary/Principal/First-Listed Diagnosis That Denied Inappropriately Will Be Reprocessed (Updated April 14, 2016)","Link":"web_announcement_1117_20160408.pdf","ToolTip":"Claims for ICD-10 「Z」 Codes and 「O09」 Codes Billed as the Primary/Principal/First-Listed Diagnosis That Denied Inappropriately Will Be Reprocessed (Updated April 14, 2016)","WATypeID":1},{"WATypeName":"Web Announcements","Categories":[{"ID":11,"Name":"All Providers","IsSelected":true}],"WAID":8,"WADate":"\/Date(1460095200000)\/","WANum":"1116","Topic":"Medicaid Services Manual Chapters 400, 600 and 1900 Updated","Link":"web_announcement_1116_20160408.pdf","ToolTip":"Medicaid Services Manual Chapters 400, 600 and 1900 Updated","WATypeID":1},{"WATypeName":"Web Announcements","Categories":[{"ID":11,"Name":"All Providers","IsSelected":true}],"WAID":9,"WADate":"\/Date(1460008800000)\/","WANum":"1115","Topic":"Attention Provider Types 23 and 76: Procedure Codes Updated in MMIS","Link":"web_announcement_1115_20160407.pdf","ToolTip":"Attention Provider Types 23 and 76: Procedure Codes Updated in MMIS","WATypeID":1},{"WATypeName":"Web Announcements","Categories":[{"ID":1,"Name":"Inpatient","IsSelected":true},{"ID":2,"Name":"Outpatient","IsSelected":true}],"WAID":10,"WADate":"\/Date(1459922400000)\/","WANum":"1114","Topic":"Attention Provider Types 30 and 83: Service Plans Will No Longer Be Faxed to Providers","Link":"web_announcement_1114_20160406.pdf","ToolTip":"Attention Provider Types 30 and 83: Service Plans Will No Longer Be Faxed to Providers","WATypeID":1}]; 
    var categories = [{"ID":1,"Name":"Inpatient","IsSelected":false},{"ID":2,"Name":"Outpatient","IsSelected":false},{"ID":3,"Name":"Pharmacy","IsSelected":false},{"ID":4,"Name":"Dental/Orthodontia","IsSelected":false},{"ID":5,"Name":"Vision","IsSelected":false},{"ID":6,"Name":"Physician/Medical","IsSelected":false},{"ID":7,"Name":"Personal Care Services (PCS)","IsSelected":false},{"ID":8,"Name":"Durable Medical Equipment (DME)","IsSelected":false},{"ID":9,"Name":"Behavioral Health","IsSelected":false},{"ID":10,"Name":"Waiver Providers","IsSelected":false},{"ID":11,"Name":"All Providers","IsSelected":false}]; 
    var _page = page; 

    var _model = { 
     Data: data, 
     Categories: categories, 
    }; 

    $.ajax({ 
     type: "POST", 
     contentType: "application/json; charset=utf-8", 
     url: url, 
     data: JSON.stringify({ model: _model, page: _page }), 
     success: function(result) { 
     //console.log("Success " + result); 
     $('div#pagination').html($(result).find('div#pagination')); 
     $('div#full-table').html($(result).find('div#full-table')); 
     $('div#search-form').html($(result).find('div#search-form')); 
     _page = parseInt(_page) + 1; 
     bindAllEvents(_page); 
     }, 
     error: function(result) { console.log("Error: " + result.responseText); } 
    }); 
    } 
} 

function bindAllEvents(page) { 
    $('#date-boxes').slideUp(1); 

    $('div#pagination').off('click.pagination'); 
    $('div#pagination').on('click.pagination', '#next', function (e) { 
    e.preventDefault(); 
    var onNextClickEvent = new nextEvent(); 
    onNextClickEvent.nextPage(page || 2); 
    }); 

    $("a[name='page-number']").off("click.pagination"); 
    $("a[name='page-number']").on("click.pagination", function(e) { 
    e.preventDefault(); 
    var onPageClick = new pageEvent(); 
    onPageClickEvent.goToPage($(this).attr('id')); 
    }); 
} 
+0

太棒了!謝謝! – boost

相關問題