代码之家  ›  专栏  ›  技术社区  ›  Sathish

Read GridMVC select row in javascript按钮单击

  •  0
  • Sathish  · 技术社区  · 6 年前

    当点击按钮时,我需要阅读选中的复选框的行。 我试图在按钮点击中读取GridMVC中的所有数据,但没有工作。

    我的代码

    @using GridMvc.Html
    
    @{
        if (Model != null)
        {
            <div class="table-responsive">
                @Html.Grid(Model).Columns(columns =>
                {
                   columns.Add()
                   .Encoded(false)
                   .Sanitized(false)
                   .SetWidth(10)
                   .RenderValueAs(o => Html.CheckBox("checked", o.select));
                   columns.Add(c => c.product_code).Titled(Resources.Resource.product_id);
                   columns.Add(c => c.product_name).Titled(Resources.Resource.product_name);
                   columns.Add(c => c.unit).Titled(Resources.Resource.unit);
                }).WithPaging(10).Sortable(true).EmptyText("No data found").Named("GridSearch")
            </div>
        }
        else
        {
            <label>No Data Found</label>
        }
    
    }
    

    查看

     <div id="newgrid" class="col-sm-12 table-responsive" style="width:100%;height:300px;margin-top:3%;padding-left:0px;padding-right:0px;">
        @{Html.Partial("ResultGrid", new List<SCM_MVC.Models.Search>()); }
       </div>
       <div class="form-actions" style="margin-top:2%;padding-left:2%;">
            <button type="button" class="btn btn-success btn-sm" id="btnok" style="width:100px"><i class="fas fa-check"></i> OK </button>
       </div>
    

    Java脚本

     $('#btnok').click(function () {
    
            var items = $('.grid-mvc').val();
            alert(items);
        })
    

    1 回复  |  直到 6 年前
        1
  •  1
  •   Divyang Patel    6 年前

    HTML元素

    网格

    @Html.Grid(Model).Columns(columns =>
       {
           columns.Add()
           .Encoded(false)
           .Sanitized(false)
           .SetWidth(10)
           .RenderValueAs(o => new HtmlString
                   (
                     "<input id='hiddenValue' type='hidden' class='check' value='" + o.Id + "'/>"+
                     "<input type='checkbox' class='check' value=" +o.select+"/>"
                   ));
           columns.Add(c => c.FirstName).Titled("First Name");
           columns.Add(c => c.LastName).Titled("Last Name");
       }).WithPaging(10).Sortable(true).EmptyText("No data found").Named("GridSearch")
    

    <div id="newgrid" class="col-sm-12 table-responsive" style="width:100%;height:300px;margin-top:3%;padding-left:0px;padding-right:0px;">
        @Html.Partial("ResultGrid", Model)
    </div>
    

    JavaScript语言

    $('#btnok').click(function () {        
        //for find all row
        var items = $(".grid-mvc").find(".grid-table > tbody").children();
    
        $.each(items, function (i, row) {            
            if ($(row).find('.check').is(':checked')) {
                //get text of firstname column using inner text.
                alert($(row).children()[1].innerText);
                //get text of lastname column using inner text.
                alert($(row).children()[2].innerText);
                //using each loop get data with checked row.
                alert($(row).find('#hiddenValue').val())
            }
        })        
    })