How can I get checked row ids? [closed]

僤鯓⒐⒋嵵緔 提交于 2019-12-13 08:55:30

问题


I'm using jqGrid, and I add a checkbox column, I want to be able to get checked rows so I can call the server with them...

My jqGrid Code:

<script type="text/javascript">
    $(function () {
        $("#UsersGrid").jqGrid({
            url: "Handler.ashx",
            datatype: 'json',
            height: '100%',
            width: '500',
            colNames: [' ', 'ref#', 'Module', 'TT#', 'AssignedOn', 'TrialNo'],
            colModel: [
                    { name: ' ', index: 'ChkBox', width: 16, sortable: false, editable: true, formatter: "checkbox", formatoptions: { disabled: false }, editable: true, edittype: "checkbox" },
                    { name: 'ref#', width: 50, sortable: true },
                    { name: 'Module', width: 50, sortable: true },
                    { name: 'TT#', width: 110, sortable: true },
                    { name: 'AssignedOn', width: 110, sortable: true },
                    { name: 'TrialNo', width: 50, sortable: true }
                ],
            rowNum: 10,
            rowList: [10, 20, 30],
            pager: '#UsersGridPager',
            sortname: ' ',
            viewrecords: true,
            sortorder: 'asc'
            //caption: "Cases"
        });

        $("#UsersGrid").jqGrid('navGrid', '#UsersGridPager', { edit: false, add: false, del: false });
</script>

Thanks in Advance...


回答1:


You should better you multiselect:true, because the functionality i can see you are implementing with check boxes is to select multiple rows.

here's how it will work for u. 1. Make multiselect:true in jqgrid Parameters.

  1. add one button to your html like this

button type="button" value="submit" id="clickMe" >Submit /button> //start and close the tags properly.

  1. Now on the click event of this button, get the data of selected rows and make one ajax request to your server.

$('#clickMe').click(function(){ var selRowIds = $('#grid').jqGrid('getGridParam', 'selarrrow');

if(selRowIds.length>0)
               {
                   for( var i=0;i<selRowIds.length;i++){
           var ref#=getCellValue(selRowIds[i],'ref#');
           var Module=getCellValue(selRowIds[i],'Module');
           var TT#=getCellValue(selRowIds[i],'TT#');


           var AssignedOn=getCellValue(selRowIds[i],'AssignedOn');
               var TrialNo=getCellValue(selRowIds[i],'TrialNo');

               $.ajax({
               type: 'POST',
               url: '@Url.Action("editMe")',
               contentType: 'application/json; charset=utf-8',
               data:JSON.stringify({ref#: ref#, Module:Module,TT#:TT#,AssignedOn:AssignedOn,TrialNo:TrialNo}),
               dataType: "json",
               success:function(){
               $('#grid').trigger("reloadGrid");
                },

                error: function () {
                       alert("error");
                    }
                }); 
                }
                }
           });

and your controller should look like this

 public ActionResult editMe(string ref#, string Module, string TT#, string AssignedOn, string TrialNo)
        {
          }

I'm assuming you have dataType for all the columns as string and they all are editable:true(you can mention this with colModal. So if only Module, AppliedOn is editable true, so you can get only these two values in button click. depending upon your need you can change the code.




回答2:


You don't have to add the checkbox column manually. Here's how I do it:

  1. Specify the editurl and multiselect options:

    $("#UsersGrid").jqGrid({
      // The grid will send modification data to this url
      //     
      editurl: "url which will handle the edit/delete operations",
      // This ensures there will be a checkbox column in your grid
      //
      multiselect: true,
      ...
    });
    
  2. Provide a handler for the modification operations (responding to the editurl requests). In my case, it is an action method with this signature:

    public ActionResult EditItems(string id, string oper, string source, string target)
    {
        // id: list of IDs of the selected items (e.g. "1234,5678")
        // oper: the requested operation ("edit" or "del", if you use the standard ones)
        // source, target: in case of the "edit" operation, these are the new values of the respective columns. I.e. these parameters are model-specific (I have "source" and "target" columns in my grid)
    }
    


来源:https://stackoverflow.com/questions/11949960/how-can-i-get-checked-row-ids

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!