I’m quite the beginner in jQuery and I’m trying to get a simple columnChooser to work for jqGrid.
I’m using jqGrid’s navigation bar to insert an “Add/remove columns” button, and on the click event of this button I display the column chooser. Having loaded the multiselect plugin before, it uses it to display the columns with checkboxes.
Here is my code:
$("#myGrid")
.jqGrid({
...
toppager: true,
pager: jQuery('#myPager'),
...
})
.jqGrid('navGrid', "#myPager", { //add the navigator (defaults to the bottom of the grid)
edit: false, add: false, del: false, search: false, refresh: false, //remove all default buttons
cloneToTop: true //clone it, so a new one is created on top of the grid (name of the clone is <id of grid>_toppager)
})
.jqGrid('navButtonAdd', "#myGrid_toppager", { //add a custom button to the cloned navigator
caption: "show/hide columns",
onClickButton: function () {
var colChooser = $("#colchooser_myGrid");
if (colChooser.length == 0) {
$("#myGrid").jqGrid('columnChooser', {
width: 260,
height: 220,
classname: "column-chooser",
msel_opts: {
autoOpen: true,
header: false,
height: "auto",
classes: "column-chooser" },
dlog_opts: { modal: true, resizable: false }
});
}
else {
// ??
}
}
});
And my CSS:
.column-chooser .ui-multiselect-checkboxes {
overflow-y: hidden;
}
I am stuck with three things:
- the buttons (OK and Cancel) are not visible. I don’t find them anywhere on the inner html code. When I remove the options, they appear, but the multiselect does not resize to fit the columnChooser dialog.
- how do I get the multiselect to be “unclosable”? I tried adding
beforeclose: function () { return false; }in the msel_opts object, and it works, but then the multiselect values stay visible always, even when closing the dialog. - the dialog only displays once, then refuse tho whow up again. It seems it’s because it has been created, but it seems the jqGrid calls destroy on both the dialog and the multiselect, so I cannot show them again.
I am using jquery 1.4.4, jquery-ui 1.8.18, jqgrid 4.3.1 and multiselect 1.12, all tested under Firefox 11.
Here’s the code I ended up writing to add a column chooser to a grid:
So in this code context.grid is the id of the grid, and this code gets called after the creation of the grid (i.e. after a line looking like
$(context.grid).jqGrid({ /* insert colmodel, pager name, etc */ }))Here is what is being done against all of my issues:
return falsein beforeclose, but remove theselectfrom the page with the close element in dlog_opts. Somehow theselectstays in the page (as child to the root node).I do this by setting the same class on every created object (the multiselect, the chooser, the dialog). When I’m done using it, I remove everything from my html using this class.
The root problem is that jqGrid doesn’t create a proper html hierarchy for the elements, probably because of the way the multiselect plugin works (it hides the
selectelement and creates anullist next to it containing the checkboxes). In the end, I find myself with 3 divs, one containing the dialog, one containing theuland one containing theselect, all direct children of thebodyelement. When closing, jqGrid leaves theselectelement, and that breaks it the second time I open the chooser.