Razor WebGrid 不保留滚动条位置

发布于 2024-10-22 04:41:36 字数 1010 浏览 0 评论 0原文

每当我对支持 ajax 的 webgrid 进行分页或排序时,我的滚动条位置就会重置到页面顶部。有没有办法保持滚动条的位置?

@model Registrar.WebUI.Models.InstructorPageViewModel

@{
var grid = new WebGrid(canPage: true, 
                        canSort: true, 
                        rowsPerPage: Model.PagingInfo.ItemsPerPage, 
                        ajaxUpdateContainerId: "grid");
grid.Bind(Model.Instructors, rowCount:Model.PagingInfo.TotalItems, autoSortAndPage:false);
grid.Pager(WebGridPagerModes.All);
}

<div id="grid">
 @grid.GetHtml(columns: grid.Columns(
                        grid.Column(format: x => Html.ActionLink("Edit", "Edit", new {id=x.Id})),
                        grid.Column("FirstName", "First Name"),
                        grid.Column("LastName", "Last Name"),
                        grid.Column("Email"),
                        grid.Column("UserName", "User Name"),
                        grid.Column("Phone")                            )
                  )                      
</div>

Whenever I page or sort my ajax enabled webgrid, my scrollbar position is reset to the top of the page. Is there a way to maintain the scrollbar position?

@model Registrar.WebUI.Models.InstructorPageViewModel

@{
var grid = new WebGrid(canPage: true, 
                        canSort: true, 
                        rowsPerPage: Model.PagingInfo.ItemsPerPage, 
                        ajaxUpdateContainerId: "grid");
grid.Bind(Model.Instructors, rowCount:Model.PagingInfo.TotalItems, autoSortAndPage:false);
grid.Pager(WebGridPagerModes.All);
}

<div id="grid">
 @grid.GetHtml(columns: grid.Columns(
                        grid.Column(format: x => Html.ActionLink("Edit", "Edit", new {id=x.Id})),
                        grid.Column("FirstName", "First Name"),
                        grid.Column("LastName", "Last Name"),
                        grid.Column("Email"),
                        grid.Column("UserName", "User Name"),
                        grid.Column("Phone")                            )
                  )                      
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱殇璃 2024-10-29 04:41:36

这是因为标题中生成的排序链接具有 href="#"。我有同样的问题,这是我的解决方案。

这是我的 _grid.cshtml 部分视图

@{
  var grid = new WebGrid(Model.LeaseOffers, canPage: false, ajaxUpdateContainerId: "offerGrid", ajaxUpdateCallback: "afterLoad()");
}
<div id="offerGrid" class="offer-table-wrapper">
@grid.GetHtml(
  rowStyle: "offer-table-tr-odd",
  alternatingRowStyle: "offer-table-tr-even",
  columns: grid.Columns(
    grid.Column("ProviderLogo", "", format: @<img src="@Url.Content(string.Format(Constants.LeaseOfferProviderLogoContentUrlFormat, item.ProviderId))" />, style: "col-first", canSort: false),
    grid.Column("ProviderName", "Leasingodawca", format: @<div>@item.ProviderName</div>, style: "col-name"),
    grid.Column("DownPayment", "Udział Własny", format: @<div>@item.DownPayment.ToString(Constants.PercentDataToStringFormat)</div>, style: "col-third"),
    grid.Column("LeasePeriod", "Okres leasingu", format: @<div>@string.Format(Constants.LeasePeriodDataFormat, item.LeasePeriod)</div>),
    grid.Column("LeasePayment", "Rata leasingu", format: @<div>@string.Format(Constants.MoneyDataFormat, item.LeasePayment)</div>),
    grid.Column("ResidualValue", "Wartość wykupu", format: @<div>@item.ResidualValue.ToString(Constants.PercentDataToStringFormat)</div>),
    grid.Column("TotalLeasePayments", "Suma opłat", format: @<div>@item.TotalLeasePayments.ToString(Constants.PercentDataToStringFormat)</div>),
    grid.Column("Actions", "", format: @<div><a href="@Url.LeaseAssetDetailsWithLeaseOffer(Model.LeaseAssetId, (int)item.Id)"><img src="@Href("~/Modules/LeaseBroker.Orchard.Module/Content/Images/Mockups/wezleasing.png")"/></a></div>, style: "col-last", canSort: false)
  ),
  htmlAttributes: new { cellpadding = "0", cellspacing = "0" }
)
<script language="javascript">
  function afterLoad() {
    $("#offerGrid > table > thead > tr > th > a").attr('href', 'javascript:void(0)');
  }
  jQuery(document).ready(function ($) {
    afterLoad();
  });
</script>

网格加载后,我将标题链接中的所有 href 属性替换为“javascript:void(0)”

This is because generated sort link in your header has href="#". I've got the same problem and this is my solution.

This is my _grid.cshtml partial view

@{
  var grid = new WebGrid(Model.LeaseOffers, canPage: false, ajaxUpdateContainerId: "offerGrid", ajaxUpdateCallback: "afterLoad()");
}
<div id="offerGrid" class="offer-table-wrapper">
@grid.GetHtml(
  rowStyle: "offer-table-tr-odd",
  alternatingRowStyle: "offer-table-tr-even",
  columns: grid.Columns(
    grid.Column("ProviderLogo", "", format: @<img src="@Url.Content(string.Format(Constants.LeaseOfferProviderLogoContentUrlFormat, item.ProviderId))" />, style: "col-first", canSort: false),
    grid.Column("ProviderName", "Leasingodawca", format: @<div>@item.ProviderName</div>, style: "col-name"),
    grid.Column("DownPayment", "Udział Własny", format: @<div>@item.DownPayment.ToString(Constants.PercentDataToStringFormat)</div>, style: "col-third"),
    grid.Column("LeasePeriod", "Okres leasingu", format: @<div>@string.Format(Constants.LeasePeriodDataFormat, item.LeasePeriod)</div>),
    grid.Column("LeasePayment", "Rata leasingu", format: @<div>@string.Format(Constants.MoneyDataFormat, item.LeasePayment)</div>),
    grid.Column("ResidualValue", "Wartość wykupu", format: @<div>@item.ResidualValue.ToString(Constants.PercentDataToStringFormat)</div>),
    grid.Column("TotalLeasePayments", "Suma opłat", format: @<div>@item.TotalLeasePayments.ToString(Constants.PercentDataToStringFormat)</div>),
    grid.Column("Actions", "", format: @<div><a href="@Url.LeaseAssetDetailsWithLeaseOffer(Model.LeaseAssetId, (int)item.Id)"><img src="@Href("~/Modules/LeaseBroker.Orchard.Module/Content/Images/Mockups/wezleasing.png")"/></a></div>, style: "col-last", canSort: false)
  ),
  htmlAttributes: new { cellpadding = "0", cellspacing = "0" }
)
<script language="javascript">
  function afterLoad() {
    $("#offerGrid > table > thead > tr > th > a").attr('href', 'javascript:void(0)');
  }
  jQuery(document).ready(function ($) {
    afterLoad();
  });
</script>

Just after grid is loading i'm replacing all href attributes in header links into "javascript:void(0)"

奢华的一滴泪 2024-10-29 04:41:36

您需要确保禁用自动回发,听起来您的页面正在重定向,您可能没有注意到,因为它发生得太快了。

You need to make sure that autopostback is disabled, It sounds like your page is redirecting, you just may not be noticing as it is happening so fast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文