问题描述
我有jqgrid,它会在调整大小时触发一个函数,以便它根据窗口大小调整大小.这个函数很好,但是当jqgrid加载时,我需要相同的函数才能工作所以我这样调用函数
Hi I have jqgrid which on resize trigger a function so that it resizes according to window size. This function fine but I need that same function to work when the jqgrid loadsso I am calling the function like this
$(window).bind("load", function() {
$('#' + grid_id).setGridWidth(width, true); //Back to original width
$('#' + grid_id).setGridWidth($('#' + div_id).width(), true); //Resized to new width as per window
});
但是它不起作用.
像这样调整窗口大小时会调用相同的函数
The same function is called when the window is resized like this
$(window).bind('resize', function () {
$('#' + grid_id).setGridWidth(width, true); //Back to original width
$('#' + grid_id).setGridWidth($('#' + div_id).width(), true); //Resized to new width as per window
}).trigger('resize');
它工作正常.我在这里做什么错了.
And it works fine. What is wrong am I doing here.
推荐答案
创建网格后,您只能在 之后调用jqGrid的setGridWidth
方法.
You can call setGridWidth
method of jqGrid only after the grid is created.
我想您可能需要添加选项autowidth: true
.
I suppose that you need to do is probably adding the option autowidth: true
.
如果要实现自己的自定义大小调整,则可以在jqGridAfterLoadComplete
事件内部进行.该代码可能与以下内容有关:
If you want to implement your own custom resizing then you can do it inside of jqGridAfterLoadComplete
event. The code could be about the following:
var $grid = $("#grid"),
resizeGrid = function () {
var newWidth = $(this).closest(".ui-jqgrid").parent().width();
$(this).jqGrid("setGridWidth", newWidth, true);
};
$(window).on("resize", function() {
resizeGrid.call($grid);
});
$grid.on("jqGridAfterLoadComplete", resizeGrid)
.jqGrid({
// all jqGrid options
});
请参见 https://jsfiddle.net/OlegKi/ejpn9/153/
这篇关于加载时jQuery无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!