【问题标题】:how to create a javascript function with parameters in another javascript file to use within another javascript?如何使用另一个 javascript 文件中的参数创建一个 javascript 函数以在另一个 javascript 中使用?
【发布时间】:2011-03-21 05:46:28
【问题描述】:

嘿,美好的一天。我正在创建一个程序,它将数据从服务器加载到 jqgrid 中。我现在要做的是从一个单独的 javascript 文件创建一个函数,然后在我的其他 javascript-jqgrid-load-data 中使用该函数。这是我的 javascript 代码:

    $("#tbl").jqGrid({
        url: '',                            
        datatype: 'local',      
        jsonReader : {              
            root: function(obj) {
            //some codes here

               return root;
            },          
            page: "page",                   
            total: "pageCount",     
            records: "rows",    
            repeatitems:false,  
            id: "0" 
        },

        serializeGridData: function(postData) {
            var jsonParams = {
                .
                .//some codes here
                .

                'sort_fields': postData.sidx
            };

            if (postData.sord == 'desc')
            {
            ..//some codes
            }           
            else
            {
            ...//some codes
            }

            jpar = jsonParams;
            return 'json=' + jsonParams;
        },

        loadError: function(xhr, msg, e) { 
            showMessage('msg error');
        },
        colNames:['ID',...'Type'],      
        colModel:[
        ...//col model
        ],

        rowNum:5,           
        .
        .
        .//some codes here
        loadonce:false,         
        caption: "Main Account Group"
    });

我想把代码分开:

         jsonReader : {             
            root: function(obj) {
            //some codes here

               return root;
            },  
            page: "page",                   
            total: "pageCount",     
            records: "rows",    
            repeatitems:false,  
            id: "0" 
         },

还有这个:

         serializeGridData: function(postData) {
            var jsonParams = {
                .
                .//some codes here
                .

                'sort_fields': postData.sidx
            };

            if (postData.sord == 'desc')
            {
            ..//some codes
            }           
            else
            {
            ...//some codes
            }

            jpar = jsonParams;
            return 'json=' + jsonParams;
        },
        loadError: function(xhr, msg, e) { 
            showMessage('msg error');
        },

【问题讨论】:

    标签: function parameters jqgrid parameter-passing javascript


    【解决方案1】:

    我写了my answer你的下一个问题,以便它回答你的问题。主要思想是您可以使用 global 函数或更好地重新定义 jqGrid 的默认值

    jQuery.extend(jQuery.jgrid.defaults, {/*your changes to the defaults*/});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-16
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 2019-01-16
      相关资源
      最近更新 更多