Adding a function to jqGrid jQuery plugin(向 jqGrid jQuery 插件添加函数)
问题描述
我正在尝试向 jqGrid jQuery 插件添加一个名为 rows
的函数,但我无法确定语法.这是我的非工作版本.
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
};
正确的语法是什么?
谢谢!
推荐答案
您的问题的正确答案似乎取决于您要实现的方法 rows
应该做什么.我试着猜测一下,并给出了与我对你的问题的理解相对应的实现.
It seems the correct answer on your question depends a little from what should do the method rows
which you want to implement. I try to guess a little and gives the implementation which correspond to my understanding of your question.
首先jqGrid是jQuery插件,如果你写例子
First of all jqGrid is jQuery plugin and if you write for example
$(myselector).jqGrid('setSelection',rowid);
可能是 $(myselector)
选择 more 作为一个 DOM 元素.例如
it can be that $(myselector)
selects more as one DOM element. For example
$('table').jqGrid('setSelection',rowid);
将尝试在页面上的所有 <table>
元素上调用 jqGrid 方法setSelection".所以 this
元素在 DOM 元素数组中(应该是 <table>
DOM 元素)而不仅仅是一个元素.
will try call jqGrid method 'setSelection' on all <table>
elements on the page. So this
element in the array of DOM elements (it should be <table>
DOM elements) and not only one element.
另一个一般性评论.有一些 jQuery 方法可以像这样链接起来
Another general remark. There are jQuery methods which can be chained like
$("#list").jqGrid('setGridParam',{datatype:'json'}).trigger('reloadGrid');
如果 'setGridParam' 做某事并返回 this
以支持链接.其他方法不支持链接并返回方法需要返回的内容.例如 getDataIDs
返回 ids 数组,并且不能将 getDataIDs
与另一个 jQuery 方法链接.
In the case the 'setGridParam' do something and return this
to support chaining. Other methods don't support chaining and return what the method need to return. For example getDataIDs
returns the array of ids and one can't chain getDataIDs
with another jQuery methods.
现在我回到你的问题.我最好将新方法命名为 getRowsById
.该方法将返回带有 DOM 元素的数组,这些元素代表 <tr>
(表格行).该方法将 rowid
作为参数.然后可以顺便用新方法扩展jqGrid:
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 have rowid
as the parameter. Then one can extend jqGrid with the new method in the way:
$.jgrid.extend({
getRowsById: function (rowid){
var totalRows = [];
// enum all elements of the jQuery object
this.each(function(){
if (!this.grid) { return; }
// this is the DOM of the table
// we
var tr = this.rows.namedItem(rowid);
if (tr !== null) { // or if (tr !== null)
totalRows.push(tr);
}
});
return totalRows;
}
});
首先我在示例中使用 $.jgrid.extend
定义的方法 这里.它主要是$.extend($.fn.jqGrid,methods);
.然后,由于我们实现的方法不能被链接,我们定义了 totalRows
变量,该变量将在稍后作为方法的结果返回.现在我们必须枚举 this
中的所有对象(如上面示例中的 $(myselector)
或 $('table')
的元素).我们针对 this.each(function(){/*do here*
本文标题为:向 jqGrid jQuery 插件添加函数


- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06