JQGrid减少窗口调整大小事件的列数

jqGrid出现问题。我的桌子上有很多列。当我更改窗口或在移动设备中打开Web应用程序时,它应在网格表中仅显示4或5列,而不是许多列,否则应允许在网格内滚动。那么如何在窗口调整大小事件上减少JQGrid中的列数呢?

我已经尝试过以下操作,但resize事件工作正常,但由于网格中的列数更多,并且没有滚动条,因此外观感觉也不佳。

我的HTML,

<table id="list2"></table>

我的jqGrid代码,

 $("#list2").jqGrid({
    url: '/Project/GridData',datatype: "json",mtype: 'POST',colNames: ['edit','view','id','Project_Name','Project_Name2','Project_Nam3','Project_Number','Project_Manager','Business_Unit','Project_Admin_Name','Remarks','Is_active','Created_Date','Modified_Date'],colModel: [
        { name: 'edit',index: 'edit',width: 55 },{ name: 'view',index: 'view',{ name: 'id',index: 'id',width: 55,hidden: true },{ name: 'Project_Name',index: 'Project_Name',width: 140 },{ name: 'Project_Name2',index: 'Project_Name2',{ name: 'Project_Name3',index: 'Project_Name3',{ name: 'Project_Number',index: 'Project_Number',{ name: 'Project_Manager',index: 'Project_Manager',{ name: 'Business_Unit',index: 'Business_Unit',{ name: 'Project_Admin_Name',index: 'Project_Admin_Name',width: 140,align: "right" },{ name: 'Remarks',index: 'Remarks',width: 180,{ name: 'Is_active',index: 'Is_active',width: 100,{ name: 'Created_Date',index: 'Created_Date',width: 150,sortable: false },{ name: 'Modified_Date',index: 'Modified_Date',sortable: false }
    ],rowNum: 10,rowList: [10,20,30,50,100,500,1000],//pager: '#pager2',sortname: 'id',viewrecords: true,sortorder: "desc",loadonce: true,ignoreCase: true,pagepos: 'left',forceFit: true,shrinkToFit: false,// to enable the scroll bar in the responsive mode
    height: 500,width:1600,altRows:true,altclass:'myAltRowClass'

});

我的脚本,

var $grid = $("#ProjectSearchGrid"),newWidth = $grid.closest(".ui-jqgrid").parent().width();
    $grid.jqGrid("setGridWidth",newWidth,true); // change the grid size based on parent div size
    $grid.jqGrid('setColProp','ProjectName',{width:100}); //change the column size for consistent look in the mobile device
maxiuping 回答:JQGrid减少窗口调整大小事件的列数

您可以使用showCol和hideCol方法隐藏/显示条件列。 请注意,这些方法接受array作为参数来一次显示和隐藏更多列。文件可以是found here.

通过示例,您可以执行此操作

var $grid = $("#ProjectSearchGrid"),$grid.jqGrid("hideCol",["Project_Name2","Project_Name3"]); // hide these cols before resizing
newWidth = $grid.closest(".ui-jqgrid").parent().width();
$grid.jqGrid("setGridWidth",newWidth,true);

此外,如果使用Guriddo jqGrid,则可以使用功能 isMobile 在移动设备中加载网格时隐藏一些列。

以示例为例,您可以对Project_name2列执行此操作

colModel: [
    ...
    { name: 'Project_Name2',index: 'Project_Name2',width: 140,hidden: $.jgrid.isMobile() },
,

请始终在您的问题中包含有关您使用(或可以使用)的jqGrid的版本和jqGrid的分支的信息({{ 3}},商业free jqGrid或版本

如果使用免费的jqGrid fork,则可以在相应的列中添加classes: "hidden-xs",labelClasses: "hidden-xs"classes: "hidden-xs hidden-sm",labelClasses: "hidden-xs hidden-sm"之类的属性。有关更多详细信息,请参见Guriddo jqGrid JS中的the demo。如果您不使用Bootstrap CSS,则可以手动添加hidden-**类的定义:

@media (max-width: 767px) {
  .hidden-xs {
    display: none !important;
  }
}
@media (min-width: 768px) and (max-width: 991px) {
  .hidden-sm {
    display: none !important;
  }
}
@media (min-width: 992px) and (max-width: 1199px) {
  .hidden-md {
    display: none !important;
  }
}
@media (min-width: 1200px) {
  .hidden-lg {
    display: none !important;
  }
}

如果您使用的是旧版本的jqGrid,但您确实无法升级到免费的jqGrid或Guriddo,则可以尝试使用

$(window).bind("resize",function () {
    // your resize handler
}).triggerHandler("resize");

并调用hideColshowCol隐藏/显示有关调整大小的列。如果您需要遵循这种方式,建议您仅在确实需要更改时才缓存隐藏/可见列的列表,以调用hideColshowCol。要检测当前分辨率,可以使用window.matchMedia(请参见the old answer)或获取网格的某个外部div的宽度(<table id="list2"></table>的外部div)。

更新:我创建了演示here,该演示使用jqGrid 4.6,并演示了如何使用https://jsfiddle.net/OlegKi/n6g78two/隐藏/显示有关调整网格大小的某些列。如果视口的宽度为767像素或更小,则该演示将隐藏最后一列"ComboDuration"。它使用以下代码:

function hideOrShowColumns (e) {
    if (e.matches) {
        // we have view ports of 767 pixels wide or less
        $grid.jqGrid("hideCol","ComboDuration");
    } else {
        $grid.jqGrid("showCol","ComboDuration");
    }
}
var mql = window.matchMedia('(max-width: 767px)');
hideOrShowColumns(mql);
mql.addListener(function (e) {
    hideOrShowColumns(e);
});

$(window).bind("resize",function () {
    $grid.jqGrid("setGridWidth",$grid.closest(".my-container").width());
}).triggerHandler("resize");
本文链接:https://www.f2er.com/3124615.html

大家都在问