jqGrid GridUnload/ GridDestroy
To be able to create jqGrid on the page you have to insert an empty <table>
element on the place of the page where you want see the grid. The simplest example of the table element is <table id="mygrid"></table>
.
The empty <table>
element itself will be not seen on the page till you call $('#mygrid').jqGrid({...})
and the grid elements like column headers will be created.
The method GridDestroy
works like jQuery.remove. It deletes all elements which belong to the grid inclusve the <table>
element.
The method GridUnload
on the other hand delete all, but the empty <table>
element stay on the page. So you are able to create new grid on the same place. The method GridUnload
is very usefull if you need create on one place different grids depend on different conditions. Look at the old answer with the demo. The demo shows how two different grids can by dynamically created on the same place. If you would be just replace GridUnload
in the code to GridDestroy
the demo will be not work: after destroying of the first grid no other grids will be created on the same place.
Consecutive GridUnload createGrid not working
You don't included the code of createGrid
so I can only guess. One possible reason is that you use grid
variable inside. If you use GridUnload
the old <table>
element will be deleted and another one will be created on the same place. So you should reset the value of grid
after call of GridUnload
:
var gridId = grid[0].id; // or grid.attr('id');
grid.jqGrid('GridUnload');
grid = $('#' + $.jgrid.jqID(gridId)); // or just $('#' + gridId);
createGrid();
The method $.jgrid.jqID
you have to use only if the id
of the grid can hold some meta-character.
How to Destroy jqgrid
If I understand you correctly you should use GridUnload
instead of GridDestroy
(see the answer for more details). The only what you will need to do is to resave the value of jQuery variables if any used before. For example if you have table with id="list" and you saved $("#list")
before calling of GridUnload
in a variable (like var myGrid=$("#list");
) then you should reassign the value of $("#list")
to the same variable (execute myGrid=$("#list");
one more time after the call of GridUnload
).
Why is free-jqgrid not resetting row ids after GridUnload?
I see that your incorrectly understand the meaning of rowids and the requirements for input data. I'm wonder especially about the suggestion of Tony to reset $.jgrid.guid
. I find the suggestion very bad because it looks like a solution, but it can produce more seriously problems as the problems, which are solved!
First of all, it's important to understand what is rowid. Rowid is the values of id
attribute of the grid rows: the id
values of <tr>
elements of the grid's body (see here). The rowid are used as a parameter of almost all callbacks and events of jqGrid. Because of that jqGrid must assign id attribute to every row and the input data of the grid have to contain rowid information! The existence of rowids is critical for working jqGrid. Only because of that jqGrid generates unique rowids in case of wrong input data. Alternative for generation of ids would be preventing creating the grid at all.
jqGrid tolerates the error with specifying the rowids in input data only for some simple grids, where one need just display some simple data and one never need to use rowids. If you use ids then you should fix the problem only in one way: you have to fix your input data. You can for example include id
property with unique value in every element of input data. You should take in consideration that no id duplicates exist not only in the grid but on the whole HTML page. It's strictly recommended to use idPrefix
option with unique value for every grid always if your HTML page have more as one grids (inclusive grid with subgrids).
Resetting of $.jgrid.guid
is very dangerous and it can break functionality of jqGrid because of creating elements with duplicate id values. The property $.jgrid.guid
is global. It's common over all grids and subgrids on the page. Simple subgrids scenario will produces id duplicates after you manually reset $.jgrid.guid
. It will be used inside of $.jgrid.randId()
method used in many places of jqGrid code. For example, the method addRow
used by inlineNav
uses the method.
I repeat my suggestion more clear once more. If you uses rowids or if the values of rowids are important for you in some way then you have to include id
property to every item of input data. If some other property has already an unique value then you can add key: true
in the corresponding column of colModel
(one can use key: true
in only one column). Alternatively, you can add prmNames: { id: "myId" }
if myId
property of input data contains unique rowid instead of default id
property.
jqGrid GridUnload - unable to reference table element after unload
I knew it was something stupid that I was missing.
I discovered that my issue was being caused by my $(this).empty() and $(this).dialog('destroy') calls on my dialog. These two calls, primarily the empty() call were blowing away my grid table. Thus, when I tried to initialize the jqGrid it was not show anything since the element was gone (at least thats my guess). Changing the call to dialog('close') and leaving my dialog intact solved the problem.
close: function (e) {
$(this).dialog('close');
if (isLogon) {
LogonFormJS.LogoffAjax();
}
}
jqgrid delete all rows inside grid
It's depend on what you exactly mean under "deleting of all rows". The method GridUnload could be very helpful in many cases, but it delete more as only grid contain.
Another method used intern in jqGrid is:
var trf = $("#list tbody:first tr:first")[0];
$("#list tbody:first").empty().append(trf);
Probably it is what you need. It delete all grid rows excepting of the first one. You can overwrite the code also as the following
var myGrid = $("#list"); // the variable you probably have already somewhere
var gridBody = myGrid.children("tbody");
var firstRow = gridBody.children("tr.jqgfirstrow");
gridBody.empty().append(firstRow);
Related Topics
Downloading Canvas Element to an Image
!Function(){ }() VS (Function(){ })()
$.Each() VS For() Loop - and Performance
What Is Node_Env and How to Use It in Express
Solve Cross Origin Resource Sharing with Flask
Does a JavaScript If Statement with Multiple Conditions Test All of Them
How to Get All Checked Checkboxes
Group by Array and Add Field and Sub Array in Main Array
React Js: Uncaught (In Promise) Syntaxerror: Unexpected Token < in JSON at Position 0
Jquery Clone Form Fields and Increment Id
JavaScript to Check When the Browser Window Is Closed
How to Get the First Element of an Array
Clearing Localstorage in JavaScript