在网络网格中添加行

发布于 2024-11-03 11:16:51 字数 1273 浏览 2 评论 0原文

我正在使用 MVC 3 webgrid,我需要在 webgrid 中添加一个新行以显示产品表中的价格总和。
任何想法表示赞赏。
这是我的代码

@{
WebGrid grid = new WebGrid(source: Model,
                           rowsPerPage: 3,
                           canSort: true,
                           canPage: true,
                           ajaxUpdateContainerId: "ajaxgrid");  


@grid.GetHtml(
    alternatingRowStyle: "altrow",
    mode: WebGridPagerModes.All,
    firstText: "<< ",
    previousText: "< ",
    nextText: " >",
    lastText: " >>",
    columns: grid.Columns(
            grid.Column(format: (item) => new HtmlString(Html.ActionLink("Edit", "Edit", new { id = item.ProductId }).ToString() + " | " +
                                                        Html.ActionLink("Delete", "Delete", new { id = item.ProductId }).ToString()
                                                        )
                        ),
            grid.Column("ProductId", "Product Id"),
            grid.Column("CategoryId", "Category Name", format: (item) => item.Category.CategoryName),
            grid.Column("ProductName", "Product Name"),
            grid.Column("Price", "Price", format: @<text>@String.Format("{0:c}", item.Price)</text>)
    )
)

}

I'm working with MVC 3 webgrid and i need to add a new row into webgrid to show sum of price from product table.
Any ideas are appreciated.
Here's my code

@{
WebGrid grid = new WebGrid(source: Model,
                           rowsPerPage: 3,
                           canSort: true,
                           canPage: true,
                           ajaxUpdateContainerId: "ajaxgrid");  


@grid.GetHtml(
    alternatingRowStyle: "altrow",
    mode: WebGridPagerModes.All,
    firstText: "<< ",
    previousText: "< ",
    nextText: " >",
    lastText: " >>",
    columns: grid.Columns(
            grid.Column(format: (item) => new HtmlString(Html.ActionLink("Edit", "Edit", new { id = item.ProductId }).ToString() + " | " +
                                                        Html.ActionLink("Delete", "Delete", new { id = item.ProductId }).ToString()
                                                        )
                        ),
            grid.Column("ProductId", "Product Id"),
            grid.Column("CategoryId", "Category Name", format: (item) => item.Category.CategoryName),
            grid.Column("ProductName", "Product Name"),
            grid.Column("Price", "Price", format: @<text>@String.Format("{0:c}", item.Price)</text>)
    )
)

}

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

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

发布评论

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

评论(4

七禾 2024-11-10 11:16:51

我遇到了类似的问题,因此我创建了内置 MVC WebGrid 的扩展。代码和示例用法位于 https://gist.github.com/3211605。希望它有帮助。

I had a similar problem so I created an extension of the built in MVC WebGrid. Code and example usage is at https://gist.github.com/3211605. Hopefully it's helpful.

一场春暖 2024-11-10 11:16:51

如果您可以使用 JQuery 并且页脚内容不按列对齐,那么向 WebGrid 添加页脚有点麻烦,但并不困难:

@{
  var grid = new WebGrid(...);
}
@grid.GetHtml(
  ...,
  htmlAttributes: new { id = "myTable" }
)

<span id="footerContent">This will be in footer</span>

<script>
  $().ready(function () {
    $('#footerContent').appendTo('#myTable tfoot tr td:last');
  }
</script>

基础知识是:

  1. 设置表的 HTML ID 属性,使用 htmlAttributes 参数WebGrid.GetHtml()
  2. 将页脚的内容放入页面上的元素中
  3. 使用 JQuery 的 .appendTo() 方法将内容元素添加到页脚 TD 元素的末尾

更健壮解决方案应该确保这些元素存在,但您明白了。

假设您的模型已经包含总和(或获取总和的方法),只需构建页脚,如下所示:

<span id="footerContent">@Model.Total</span>

If you're okay with using JQuery and having the footer content not being column-aligned, adding a footer to a WebGrid is a bit hacky, but not difficult:

@{
  var grid = new WebGrid(...);
}
@grid.GetHtml(
  ...,
  htmlAttributes: new { id = "myTable" }
)

<span id="footerContent">This will be in footer</span>

<script>
  $().ready(function () {
    $('#footerContent').appendTo('#myTable tfoot tr td:last');
  }
</script>

The basics are:

  1. Set your table's HTML ID attribute, using the htmlAttributes parameter of WebGrid.GetHtml()
  2. Put the footer's contents in an element on your page
  3. Use JQuery's .appendTo() method to add the content element to the end of the footer's TD element

A more robust solution should make sure those elements exist, but you get the idea.

Assuming your model already contains the sum (or a method to get the sum), just build your footer like:

<span id="footerContent">@Model.Total</span>
很酷不放纵 2024-11-10 11:16:51

这是一个可怕的黑客,但它让我继续我的一天。我认为正确执行此操作的方法是编写一个 Html Helper,允许您传入页脚行。我对 WebGrid 没有内置页脚行感到有点失望。

 gridHtml = MvcHtmlString.Create(gridHtml.ToString().Replace("</table>", sbTotalsTable.ToString()));

    gridHtml is from   var gridHtml = @grid.GetHtml....blah..


    the sbTotalsTable is    var sbTotalsTable = new StringBuilder();

    sbTotalsTable.Append("<tr>");

    sbTotalsTable.Append("<td>");

    sbTotalsTable.Append(DataSource.Select(s=>s.PropToSum).Sum().ToString();//The total of that column
    sb.TotalsTable.Append("</td>");
    sb.TotalsTable.Append("</tr>");
    sb.TotalsTable.Append("</table>");//closing the table without opening it because the rest comes from the gridHtml.

只需确保与手工构建的表格行中的列数匹配即可。请注意,没有表开始标记。整个想法是劫持表关闭时创建的 html 并添加一行。
抱歉手写代码。我使用的是专有应用程序,我不敢粘贴其中的任何内容......

This is a terrible hack but it allowed me to get on with my day. I think the way to do this correctly is to write an Html Helper that allows you to pass in the footer row. I'm a little disappointed the WebGrid doesn't have a footer row built in.

 gridHtml = MvcHtmlString.Create(gridHtml.ToString().Replace("</table>", sbTotalsTable.ToString()));

    gridHtml is from   var gridHtml = @grid.GetHtml....blah..


    the sbTotalsTable is    var sbTotalsTable = new StringBuilder();

    sbTotalsTable.Append("<tr>");

    sbTotalsTable.Append("<td>");

    sbTotalsTable.Append(DataSource.Select(s=>s.PropToSum).Sum().ToString();//The total of that column
    sb.TotalsTable.Append("</td>");
    sb.TotalsTable.Append("</tr>");
    sb.TotalsTable.Append("</table>");//closing the table without opening it because the rest comes from the gridHtml.

Only make sure you match the number of columns in your hand built table row. Notice there is no table start tag.. The whole idea is to hijack the created html at the table close and add a row.
Sorry for the hand typed code. I'm on a proprietary app and I don't dare paste anything from it...

羁客 2024-11-10 11:16:51

将新项目添加到 IEnumerable 中,用作要绑定到的 WebGrid 的源。

var lineItems = Model.LineItems;
var totalRow = new SalesLineItem
                {
                    SaleType = "Total Sales",
                    Amount = Model.TotalSales
                };
lineItems.Add(totalRow);

var grid = new WebGrid(lineItems);

我还没有找到一种很好的方法来将格式应用于该总行。这可能必须在客户端完成。

Add a new item to the IEnumerable that you use as the source for the WebGrid to bind to.

var lineItems = Model.LineItems;
var totalRow = new SalesLineItem
                {
                    SaleType = "Total Sales",
                    Amount = Model.TotalSales
                };
lineItems.Add(totalRow);

var grid = new WebGrid(lineItems);

I haven't found a nice way to apply formatting to this total row yet. That might have to be done client-side.

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