Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make jqGrid scroll to the bottom when a new row is added?

I have a jqGrid on a page and users can click a button to add a new row. If there are already enough rows on the page to fill the visible portion of the grid the new row is added and a scroll bar appears but the user needs to scroll to see the new row.

Is there a way to do this programmatically?

like image 648
HitLikeAHammer Avatar asked Mar 30 '10 23:03

HitLikeAHammer


People also ask

What is rowNum in jqGrid?

jqGrid exposes a property rowNum where you can set the number of rows to display for each page.

How do I select rows in jqGrid?

Rows can be selected in ParamQuery Grid either by user action or programmatically. All rows can be selected using Ctrl - A or Cmd - A (Mac).

How do I highlight a row in jqGrid?

You can select or highlight rows based on data using the queryCellInfo event or can also select a row using an external button click event. The queryCellInfo is triggered every time a request is made to access particular cell information, element and data.

How do I delete a row in jqGrid?

jqxGrid('getrowid', selectedrowindex); var commit = $("#jqxgrid"). jqxGrid('deleterow', id); $("#popupWindow"). jqxWindow('hide'); } }); $("#cancel"). click(function () { $("#popupWindow").


2 Answers

A quick and easy way to do this using the jqGrid API is to:

  • Call editRow (which will set focus to the edited row)
  • And then immediately call restoreRow (because you do not really want to edit the row)

Otherwise you should be able to use jQuery's focus function to set focus to the row, for example: jQuery("#" + row_id).focus() - but I have not tested this method, so YMMV.

Actually focus will not scroll the grid's div. But you can use the following code to guarantee that the grid scrolls such that the row with a given id is viewable:

function getGridRowHeight (targetGrid) {
    var height = null; // Default

    try{
        height = jQuery(targetGrid).find('tbody').find('tr:first').outerHeight();
    }
    catch(e){
     //catch and just suppress error
    }

    return height;
}

function scrollToRow (targetGrid, id) {
    var rowHeight = getGridRowHeight(targetGrid) || 23; // Default height
    var index = jQuery(targetGrid).getInd(id);
    jQuery(targetGrid).closest(".ui-jqgrid-bdiv").scrollTop(rowHeight * index);
}
like image 170
Justin Ethier Avatar answered Oct 23 '22 23:10

Justin Ethier


//i. Set newly added row (with id = newRowId) as the currently selected row
$('#myGrid').jqGrid('setSelection', newRowId);
//ii. Set focus on the currently selected row
$("#" + $('#myGrid').jqGrid('getGridParam', 'selrow')).focus();
like image 1
ShivanandSK Avatar answered Oct 23 '22 23:10

ShivanandSK