jqGrid:在子网格的自定义验证函数中获取父行ID

时间:2013-06-27 10:22:50

标签: jquery jqgrid jqgrid-php

任何人都可以建议我如何在子网格的自定义验证函数中获取父行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。

1 个答案:

答案 0 :(得分:0)

重要的是您定义用于自定义验证的函数customNumberChk的位置。 JavaScript允许访问外部作用域中定义的任何变量。一个名称的功能为closure(有关详细信息,请参阅the answer)。因此,您可以在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
                    }
                }
            }
        ...
    });
    ...
}