简体   繁体   中英

Read GridMVC select row in javascript button click

I have GridMVC in project with checkbox. while click button i need to read row which checkbox selected. I tried to read all data from GridMVC in button click but not working.

MY Code

GRID

@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>
    }

}

View

 <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 Script

 $('#btnok').click(function () {

        var items = $('.grid-mvc').val();
        alert(items);
    })

How to read GRIDMVC in javascript.

You can find row using HTML Element .

Grid

@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")

View

<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())
        }
    })        
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM