【问题标题】:How to set webgrid row style inline如何内联设置 webgrid 行样式
【发布时间】:2014-08-09 09:01:21
【问题描述】:

我正在使用 WebGrid 显示项目列表,列表中的某些项目被禁用,所以我想让它在网格中变暗,为此我必须设置行如果项目被禁用,类会变暗,我不知道如何根据条件设置行类

这是我的示例代码

  var grid = new WebGrid(Model, canPage: true, rowsPerPage: 20);
                @grid.GetHtml(tableStyle: "grid",
                                rowStyle: "gridrow",
                                alternatingRowStyle: "gridrow_alternate",
                                mode: WebGridPagerModes.All,
                                numericLinksCount: 10,
                                columns: grid.Columns(
                                    grid.Column("Name", "Name", item => (item.LocationData[0].Name), canSort: true, style: "width:60%"),
                                    grid.Column("Url", "Url", canSort: true, style: "width:60%"),
                                    grid.Column("Edit", "", @<a href='../VenHome/Edit/@item.ID' ><img src='/content/icons/edit.png'
                                        alt='Edit' />
                                    </a>, style: "width:150px"),
                                    grid.Column("Delete", "", @<a href='#' id='Delete' itemId='@item.ID' title='@item.LocationData[0].Name'><img
                                        src='/content/icons/delete.png' alt='Delete' />
                                    </a>, style: "width:150px"),
                                    grid.Column("Details", "", @<a href="../VenHome/Details/@item.Id" title="Details">
                                        <img src="../../Content/Icons/Details.png" alt="Details" /></a>)
                                    ));

            }

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 webgrid


    【解决方案1】:

    我使用 JQuery 找到了解决此问题的方法,我为网格列内部 HTML 添加了 CSS 类属性 这是之前添加属性的代码


     @{
            var grid = new WebGrid(Model, canPage: true, rowsPerPage: 20);
                    @grid.GetHtml(tableStyle: "grid",
                                    rowStyle: "gridrow",
                                    alternatingRowStyle: "gridrow_alternate",
                                    mode: WebGridPagerModes.All,
                                    numericLinksCount: 10,
                                    columns: grid.Columns(
                                        grid.Column("Name", "Name", item => (item.LocationData[0].Name), canSort: true, style: "width:60%"),
                                        grid.Column("Url", "Url", canSort: false, style: "width:60%"),
                                        grid.Column("Edit", "", @<a href='../VenHome/Edit/@item.ID' ><img src='/content/icons/edit.png' alt='Edit' /></a>, style: "width:150px"),
                                        grid.Column("Delete", "", @<a href='#' id='Delete' class="temp" removed="@item.Removed" itemId='@item.ID' title='@item.LocationData[0].Name'><img src='/content/icons/delete.png' alt='Delete'  /></a>, style: "width:150px"),
                                        grid.Column("Details", "", @<a href="../VenHome/Details/@item.Id" title="Details">
                                            <img src="../../Content/Icons/Details.png" alt="Details" /></a>)
                                        ));
    
                }
    

    这是修改后的代码

      grid.Column("Delete", "", @<a href='#' id='Delete' class="temp" removed="@item.Removed" itemId='@item.ID' title='@item.LocationData[0].Name'><img src='/content/icons/delete.png' alt='Delete'  /></a>, style: "width:150px"),
    

    我在删除链接中添加了一个类“temp”,还添加了一个属性“removed”具有条件值,我添加了以下JQuery代码

     $(".temp").each(function (index, element) {
                if (($(element).attr("removed")) == "False") {
                    $(element).parent().parent().attr("class", "deleted");
    
                    $(element).find("img").attr("src", "../../content/icons/deleted.png");
                }
            });
    

    注意:如果该项目被删除,则该行显示为暗色

    【讨论】:

      【解决方案2】:

      我正在禁用定义为 WebGrid 中的列的链接,这也应该适用于您。当条件被禁用时,我将颜色设置为灰色,onclick 返回 false。否则颜色为黑色,onclick 返回 true,如下所示:

      @{
          bool enableLink = false;
          var link = "false";
          var color = "grey";
          if (enableLink) { link="true"; color="black"; }         
      }
      
                  <div>@grid.GetHtml( tableStyle: "grid",
                    headerStyle: "head",
                    alternatingRowStyle: "alt",
                    columns: grid.Columns(                
                    grid.Column(format: @<a href="http://www.whereverYouLinkTo.com" style="color:@color" onclick="return @link">Edit</a>), 
                    grid.Column("FirstName", header:"First"),
                    grid.Column("LastName", header:"Last")))
      
      </div>
      

      我希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 2017-01-27
        • 1970-01-01
        • 1970-01-01
        • 2014-08-19
        • 2014-02-27
        • 1970-01-01
        • 2020-07-22
        • 2018-06-18
        • 2013-12-29
        相关资源
        最近更新 更多