向 jqGrid jQuery 插件添加函数

Adding a function to jqGrid jQuery plugin(向 jqGrid jQuery 插件添加函数)

本文介绍了向 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 插件添加函数