如果为空,则在webgrid内自定义消息

时间:2011-07-12 14:16:55

标签: asp.net-mvc-3 webgrid

  @if (Model.ActivityCollection.Count > 0)
        {
        var grid = new WebGrid(source: Model.ActivityCollection, rowsPerPage: 12, canSort: false);   
            @grid.GetHtml(tableStyle: "webGrid",
            headerStyle: "header",
            alternatingRowStyle: "alt",
            columns: grid.Columns(
            grid.Column("EffectiveDate", "Effective Date", style: "date"),
            grid.Column("PremiumPaymentAmount", "Premium Payment Amount", style: "amount"),
            grid.Column("PaymentType", "Payment Type", style: "date")
           ));
        }
else
        {

        }

我想在上面的else语句中在网格中显示一条消息“找不到支付信息”。有人可以帮我这个吗?

3 个答案:

答案 0 :(得分:6)

<div class="grid" style="margin-left:5px;" id="grid">          
        @if (Model.ActivityCollection.Count > 0)
        {
            var grid = new WebGrid(source: Model.ActivityCollection, rowsPerPage: 12, canSort: false);   
            @grid.GetHtml(tableStyle: "webGrid",
            headerStyle: "header",
            alternatingRowStyle: "alt",
            columns: grid.Columns(
            grid.Column("EffectiveDate", "Effective Date", style: "date"),
            grid.Column("PremiumPaymentAmount", "Premium Payment Amount", style: "amount"),
            grid.Column("PaymentType", "Payment Type", style: "date")
           ));
        }
        else
        {
            <div class="grid">
            <table cellspacing="0" width="80%">
               <thead>
                    <tr>
                        <th>Effective Date</th>
                        <th>Premium Payment Amount</th>
                        <th>Payment Type</th>
                        </tr>
                </thead>
                <tbody>
                 <tr>
                        <td colspan="3" align="center" ><br />No payment information found<br /><br /> </td>
                 </tr>                     
                </tbody>
            </table>
            <br/><br/><br/><br/>
            </div>
        }
        </div>

答案 1 :(得分:3)

我使这个jQuery函数在网格加载时被触发,如果表中没有数据,它将在空表中插入一个新行来显示文本。比在HTML中构建WebGrid的副本更容易管理。

我在Webgrid中添加了一种行样式,以识别行(不需要标题+页脚):“webGridDataRow”。

function addNoDataTextIfNeeded(){
    if($(".webGrid .webGridDataRow").length < 1){ //if there are no data-rows in table, our text should be displayed
        var newCell = $("<td>") //create the cell
            .attr("colspan", $(".webGrid tr:first th").length) //set colspan so it will span entire grid width (counting the number of column headers)
            .text("No data found"); //add the text to be displayed
        $("<tr>").append(newCell).appendTo(".webGrid"); //add the cell to a row to the grid :)
    }
}

答案 2 :(得分:0)

你可以做这样的事吗?

public class MyWebGrid : WebGrid
{
    public WebGridMkf(IEnumerable<dynamic> source = null,
        IEnumerable<string> columnNames = null,
        string defaultSort = null,
        int rowsPerPage = 10,
        bool canPage = true,
        bool canSort = true,
        string ajaxUpdateContainerId = null,
        string ajaxUpdateCallback = null,
        string fieldNamePrefix = null,
        string pageFieldName = null,
        string selectionFieldName = null,
        string sortFieldName = null,
        string sortDirectionFieldName = null) :
        base(source, columnNames, defaultSort, rowsPerPage, canPage, canSort, ajaxUpdateContainerId, ajaxUpdateCallback, fieldNamePrefix, pageFieldName, sortFieldName, sortDirectionFieldName)
    {
    }

    public IHtmlString Table(string tableStyle = null,
        string headerStyle = null,
        string footerStyle = null,
        string rowStyle = null,
        string alternatingRowStyle = null,
        string selectedRowStyle = null,
        string caption = null,
        bool displayHeader = true,
        bool fillEmptyRows = false,
        string emptyRowCellValue = null,
        IEnumerable<WebGridColumn> columns = null,
        IEnumerable<string> exclusions = null,
        Func<dynamic, object> footer = null,
        object htmlAttributes = null)
    {
        if (this.TotalRowCount.Equals(0))
        {
            return new HtmlString("<div>teste</div>");
        }

        return base.Table(tableStyle, headerStyle, footerStyle, rowStyle, alternatingRowStyle, selectedRowStyle, caption, displayHeader, fillEmptyRows, emptyRowCellValue, columns, exclusions, footer, htmlAttributes);
    }
}

并致电:

var grid = new MyWebGrid(
        ajaxUpdateContainerId: "grid-test",
        rowsPerPage: 10,
        defaultSort: "id"
    );
    grid.Bind(
        source: Model.ActivityCollection,
        rowCount: Model.ActivityCollection.Count
    );
@grid.Table(
alternatingRowStyle: "color1",
rowStyle: "color0",
tableStyle: "webgrid table table-striped table-hover table-condensed",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
footer: @<span></span>,
columns: grid.Columns(
    grid.Column(columnName: "id",
                header: "ID",
                style: "column-id",
                canSort: true)
    )
)