I am trying to add a function named rows to the jqGrid jQuery plugin, but I can’t determine the syntax. Here are my non-working versions.
(function($) {
$.fn.jgrid.rows = function(data) {
// do something
};
});
(function($) {
$.fn.rows = function(data) {
// do something
};
});
$.jqgrid.fn.rows = function(data) {
// do something
};
$.fn.rows = function(data) {
// do something
};
What would be the proper syntax?
Thanks!
It seems the correct answer on your question depends a little from what should do the method
rowswhich you want to implement. I try to guess a little and gives the implementation which correspond to my understanding of your question.First of all jqGrid is jQuery plugin and if you write for example
it can be that
$(myselector)selects more as one DOM element. For examplewill try call jqGrid method ‘setSelection’ on all
<table>elements on the page. Sothiselement in the array of DOM elements (it should be<table>DOM elements) and not only one element.Another general remark. There are jQuery methods which can be chained like
In the case the ‘setGridParam’ do something and return
thisto support chaining. Other methods don’t support chaining and return what the method need to return. For examplegetDataIDsreturns the array of ids and one can’t chaingetDataIDswith another jQuery methods.Now I return back to your question. I would better name the new method
getRowsById. The method will return array with DOM elements which represent<tr>(table row). The method will haverowidas the parameter. Then one can extend jqGrid with the new method in the way:First of all I use in the example the method
$.jgrid.extenddefined here. It does mostly$.extend($.fn.jqGrid,methods);. Then, because the method which we implement can’t be chained, we definetotalRowsvariable which will be returned later as the result of the method. Now we have to enumerate all objects from thethis(like elements of$(myselector)or$('table')in the examples above). We do this with respect ofthis.each(function(){/*do here*/});construct. Then inside of the loop we do followingWith the statement we test whether the current DOM element has
gridproperty. It is not a standard property of thetableelement, but jqGrid extend the DOM elements of thetablewith the property. With the test we could skip for example othertableelements where thejqGridare not applied (which are not a jqGrid). Then I use the fact thatthismust be DOM of thetableelement which hasrowsproperty (see here, and here) and I use itsnamedItemmethod. The native implemented method works better as $(“#”+rowid), but do the same. After all we return the arraytotalRows. It will have no element if the row with the row id not in the grid and 1 if it is exist. If the current jQuery selector select more as one grid and we had an error and included in both grids rows with the same id the returned array will has length greater as 1. So we can use it soAt the end I want to mention that the method
$.jgrid.extendcan be helpful not only if you want to introduce new jqGrid method. Sometime there are already some jqGrid method, but it does not exactly what you need. So you want that the modified method do something at the beginning or something at the end of the original jqGrid method. In the case we can do the followingIn the example we overwrite the original
editCellmethod with will be called by jqGrid itself and do something before of something after the call.