开发者

jqgrid afterShowForm not being called

开发者 https://www.devze.com 2023-03-21 22:04 出处:网络
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 i

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. Unfortunalit开发者_Python百科y, 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..


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消