jqGrid创建单独的功能

时间:2014-08-21 13:07:53

标签: javascript jqgrid

如何将function subGridRowExpanded分隔到另一个函数并转换为subGridRowExpanded :

subGrid: Hierarchy,

subGridOptions : {
   plusicon : "ace-icon fa fa-plus center bigger-110 blue",
   minusicon  : "ace-icon fa fa-minus center bigger-110 blue",
   openicon : "ace-icon fa fa-chevron-right center orange" }, 
subGridRowExpanded: function subGridRowExpanded(subgrid_id, row_id) 
{ 
   //somecode
}

1 个答案:

答案 0 :(得分:1)

我不确定我是否理解你的问题。我想您不希望使用匿名函数作为subGridRowExpanded的值。如果您在JavaScript中有两种标准方法:

1)定义变量并为其指定匿名函数。

var mySubGridRowExpanded = function (subgridDivId, rowId) {
        // here one can use 'this' to access to the grid
        // for example
        //
        // var mainGridPrefix = $(this).jqGrid("getGridParam", "idPrefix"),
        //     pureRowId = $.jgrid.stripPref(mainGridPrefix, rowId);

        // create empty table and div with unique ids which we construct base on
        // id of subgrid div created by jqGrid before calling of mySubGridRowExpanded
        var $subgrid = $("<table id='" + subgridDivId + "_t'></table>" +
                         "<div id='" + subgridDivId + "_p" + "'></div>");

        // append the subgrid to the subgrid div
        $subgrid.appendTo("#" + $.jgrid.jqID(subgridDivId));

        // create subgrid as jqGrid
        $subgrid.jqGrid({
            ...
        });
    };

2)像其他计算机语言一样定义名为function的名称。一个人在Javascript中使用术语function statement来表示案例。上一种方式(var mySubGridRowExpanded = function (subgridDivId, rowId) {...};)一个名称为function expression。您可以使用此函数的名称作为jqGrid的subGridRowExpanded属性的值。

function mySubGridRowExpanded (subgridDivId, rowId) {
    // the same code as above with small disadvantage
    // that one could have warnings by some JavaScript
    // validators that 'this' could be undefined.
}

在这两种情况下,您都可以使用像

这样的jqGrid选项
subGrid: true,
subGridOptions : {
    plusicon : "ace-icon fa fa-plus center bigger-110 blue",
    minusicon  : "ace-icon fa fa-minus center bigger-110 blue",
    openicon : "ace-icon fa fa-chevron-right center orange" }, 
subGridRowExpanded: mySubGridRowExpanded

JavaScript具有我喜欢的语言中的另一种语义功能,因为我可以通过多种方式定义类和对象。所以我个人更喜欢定义mySubGridRowExpanded函数(var mySubGridRowExpanded = function (subgridDivId, rowId) {...};)的第一种方式(函数表达式)