0

谁能建议我如何在子网格的自定义验证功能中获取父行 ID?

这是我的代码

 subGrid: true,
                subGridRowExpanded: function (subgrid_id, row_id)
                {

                    var subgrid_table_id;
                    subgrid_table_id = subgrid_id+"_t";
                    var subgrid_pager_id = subgrid_id+"_p";
                    jQuery("#"+subgrid_id).html("<table id="+subgrid_table_id+"  class=scroll></table><div  id="+subgrid_pager_id+" ></div>");
                    jQuery("#"+subgrid_table_id).jqGrid(
                    {
                        url:"form_subgrid_ajax_296.php?id="+row_id+"&child_form_id=296",
                        editurl:"../ajax/common_subgrid_edit_296.php?form_id="+sub_grid_id+"&parent_id="+row_id,
                        datatype: "json",

                        colNames: ['ID','PESO'],
                        colModel: [{name:'id',index:'id',editable:false,hidden:true},
                            {name:'peso',
                    index:'peso',
                    formoptions:{elmsuffix:''},editrules:{required:true,custom:true, custom_func:customNumberChk},editable:true,
                                hidden:false,
                                search:true,
                                editoptions: {size:80, maxlength: 1000}
                                }],
                        sortorder: "asc",
                        height: 300,
                        loadonce: false,
                        width: 500,
                        rowNum:10, 
                        rowList:[10,20,50],
                        sortname: 'id',
                        sortorder: "asc",
                        cellEdit: false,
                        cellsubmit: "clientarray",
                        caption:"Child",
                        pager: subgrid_pager_id
                    });
                    jQuery("#"+subgrid_table_id).jqGrid
                    (
                    "navGrid",
                    "#"+subgrid_pager_id,
                    {
                        edit:true,
                        add:true,
                        del:true,
                        search:false
                    },
                    {
                        width:780,
                        recreateForm:true
                    },
                    {
                        width:780,
                        recreateForm:true
                    }
                  );
                }

现在看名称:'peso',index:'peso', .我使用了自定义函数customNumberChk。

这是我的自定义功能

function customNumberChk(value,colname)
{

}

我想要这个函数中的rowid。

4

1 回答 1

0

重要的是您定义customNumberChk用于自定义验证的函数的位置。JavaScript 允许访问在外部范围中定义的任何变量。将函数命名为闭包(有关更多信息,请参阅答案)。因此,您可以例如在匿名回调函数customNumberChk内部定义:subGridRowExpanded

subGridRowExpanded: function (subgrid_id, row_id) {
    var subgrid_table_id;
    function customNumberChk (value, colname) {
        // one can use here ANY variable from the outer scope
        // inclusive subgrid_id, row_id, subgrid_table_id and so on
    }
    ...
    jQuery("#"+subgrid_table_id).jqGrid({
        ...
        colModel: [...
            {name:'peso',
             ...
             editrules:{required:true,custom:true, custom_func: customNumberChk}
            }
        ...
    });
    ...
}

以同样的方式,您根本不能定义customNumberChk函数并使用custom_func定义为匿名函数:

subGridRowExpanded: function (subgrid_id, row_id) {
    var subgrid_table_id;
    ...
    jQuery("#"+subgrid_table_id).jqGrid({
        ...
        colModel: [...
            {
                name:'peso',
                ...
                editrules: {
                    required: true,
                    custom: true,
                    custom_func: function (value, colname) {
                        // one can use here ANY variable from the outer scope
                        // inclusive subgrid_id, row_id, subgrid_table_id
                        // and so on
                    }
                }
            }
        ...
    });
    ...
}
于 2013-06-27T15:17:58.260 回答