4

I would like to position the view form of a jqgrid to the center of a screen. Because the height is set automatically (depending on the amount of data), I need to calculate its center position after it is rendered. Hence I would like to use the afterShowForm. Unfortunality, it's not being called whatsoever. I tried older versions of jqGrid (maybe the newest one contains a bug), but it didn't solve my problem. Anyone got a clue why it's not firing? Please note that when I change 'afterShowForm' in 'beforeShowForm' it does get fired. But then the height is not calculated yet. Thanks! (of course you are more than welcome to suggest another way to achieve my goal!). Code:

        $("#grid").jqGrid("navGrid", "#pager",
        { view: true, addfunc: additem, editfunc: edititem, delfunc: deleteitem },
        {},
        {},
        {},
        {},
        { width: 350,
            afterShowForm: function (formid) {
                alert('yey');
                // Here I want to center the form
            }
        }
        );

Edit: On the wiki I found out that the View form doesn't have a afterShowForm event; it only has onClose and beforeShowForm :( so I guess it's not possible to set the position to the center this way..

user825887
  • 219
  • 2
  • 9
  • read this http://stackoverflow.com/questions/1234461/jqgrid-default-add-edit-buttons-processing-server-response –  Jul 29 '14 at 12:28

2 Answers2

2

The reason of the problem is very easy: the afterShowForm is really not exist in the View Form. Only tree events are supported: onClose, beforeShowForm and beforeInitData. So you should use beforeShowForm, but do what you need inside another thread:

beforeShowForm: function($form) {
    setTimeout(function() {
        // do here all what you need (like alert('yey');)
    },50);
}

In the most cases you can use even 0 (instead of 50) as the second parameter of the setTimeout JavaScript function.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Yeah.. I guess that's the best fix so far ;) Let's just hope for a afterShowForm implementation somewhere soon. Thanks! – user825887 Jul 21 '11 at 20:41
  • @user825887: You are welcome! You can place the corresponding change request in the [trirand forum](http://www.trirand.com/blog/?page_id=393). – Oleg Jul 21 '11 at 20:43
0

Okay, I got it solved this way. I'm using a second setTimeout() because otherwise you will get a 'flash' when the window moves from old position to the center of the screen. Thanks!

var viewform = "#viewmodgrid";

        function centerViewForm() {
            $(viewform).css('visibility', 'hidden');
            setTimeout(function () {
                $(viewform).css('left', ($(document).width() / 2) - ($(viewform).width() / 2));
                $(viewform).css('top', ($(document).height() / 2) - ($(viewform).height() / 2));
                $(viewform).css('visibility', 'visible');
            });
        }

...                 beforeShowForm: function (formid) {
                    centerViewForm();
                }
user825887
  • 219
  • 2
  • 9