如果使用jqgrid中的内联添加按钮添加行,如何设置默认值 [英] How to set default values if row is added using inline add button in jqgrid

查看:92
本文介绍了如果使用jqgrid中的内联添加按钮添加行,如何设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码设置新行的默认值。
如果使用工具栏中的jqGrid内联添加按钮添加行,则不会调用这些方法,并且不会设置
默认值。

Code below sets default values for new row if row is added using form. If row is added using jqGrid inline add button from toolbar, those methods are not called and default values are not set.

如何强制内联添加以执行与下面的代码相同的逻辑?

How to force inline add to perform same logic as code below ?

var lastSelectedRow;
$grid.navGrid("#grid_toppager", { 
del: true,
add: true,
view: true,
edit: true
          }, 
          {},

       { 
       addedrow: 'beforeSelected',
       url: '/Grid/Add?_entity=Desktop',
       beforeInitData: function () {
         // todo: how to call this method from inline add
         var rowid = $grid.jqGrid('getGridParam', 'selrow');
         if (rowid === null) {
           alert( 'Select row before adding');
           return false;
           }
         },

       afterShowForm: function(formID) {
         // todo: how to set default values as this method sets from inline add
         var selRowData, 
            rowid = $grid.jqGrid('getGridParam', 'selrow');
         $('#' + 'Recordtype' + '.FormElement').val('Veerg');
         $('#' + 'Nait2' + '.FormElement')[0].checked = true;
         selRowData = $grid.jqGrid('getRowData', rowid);
         $('#' + 'Baas' + '.FormElement').val(selRowData.Baas);
         $('#' + 'Liigid' + '.FormElement').val(selRowData.Liigid);
       }
       );


$grid.jqGrid('inlineNav', '#grid_toppager', {
    addParams: {
        position: "beforeSelected", 
        rowID: '_empty',
        useDefValues: true,
        addRowParams: {
            keys: true,
            onEdit :  onInlineEdit
        }
    },

    editParams: {
        editRowParams: {
            onEdit : onInlineEdit
            }
    },

   add: true,
   edit: false,
   save: true,
   cancel: true
}); 

function onInlineEdit(rowId) {
  if (rowId && rowId !== lastSelectedRow) {
        cancelEditing($grid);
        lastSelectedRow = rowId;
    }
  }

更新

我试过代码

 $grid.jqGrid('inlineNav', '#grid_toppager', {
    addParams: {
        position: "beforeSelected", 
        rowID: '_empty',
        useDefValues: true,
        addRowParams: {
            keys: true,
            extraparam: { _dokdata: FormData },
            onSuccess : function (jqXHR) { 
alert('addp oncuss');
              jqXHRFromOnSuccess=jqXHR;
              return true;
              },
            afterSave: function (rowID) {
alert('afeesave addp ');
              cancelEditing($grid);
                  afterDetailSaveFunc(rowID,jqXHRFromOnSuccess);
              jqXHRFromOnSuccess=null; 
              },
            onError: errorfunc,
            afterRestore : setFocusToGrid,
            oneditfunc  : function (rowId) {
              var selRowData, selRowId ;
              if (rowId && rowId !== lastSelectedRow) {
                cancelEditing($grid);
                selRowId = $grid.jqGrid('getGridParam', 'selrow');
                if (selRowId ) {
                   selRowData = $grid.jqGrid('getRowData', selRowId ); 
                   $('#' + rowId + '_Reanr' ).val(selRowData.Reanr); 
                  }
                lastSelectedRow = rowId;
                }
             }
        }
    }
);

仅调用oneditfunc func。如何强制onSuccess,afterSave,onError等方法也被调用?

Only oneditfunc func is called. How to force onSuccess, afterSave, onError etc methods to be called also ?

更新2

我在回答中推荐的github上添加了补丁到jqGrid尝试

I added patch to jqGrid from github recommended in answer and tried

$.extend( jQuery.jgrid.inlineEdit, {
  addParams: { 
    position: "beforeSelected", 
    rowID: '<%= EntityBase.NewRowIdPrefix %>',
    useDefValues: true,
    addRowParams: {
      keys: true,
      extraparam: { _dokdata: FormData },
      onSuccess : function (jqXHR) { 
        jqXHRFromOnSuccess=jqXHR;
        return true;
        },
      afterSave: function (rowID) {
              cancelEditing($grid);
              <% if (Model is RowBase ) { %>
                  afterDetailSaveFunc(rowID,jqXHRFromOnSuccess);
                <% } else { %>
                  afterGridSaveFunc(rowID,jqXHRFromOnSuccess);
                <% } %>
              jqXHRFromOnSuccess=null; 
              },
      onError: errorfunc,
      afterRestore : setFocusToGrid,
      oneditfunc : function (rowId) {
        if (rowId && rowId !== lastSelectedRow) {
          cancelEditing($grid);
          lastSelectedRow = rowId;
          }  
        } 
      }
    }
} );

我在这种情况下输入不会终止内联添加。此代码中的所有参数都将被忽略。

I this case enter does not terminate inline add. All parameters from this code are ignored.

推荐答案

您应该使用 defaultValue editoptions 的属性,用于为新添加的行设置默认值。在当前文档中,您可以发现该选项仅在表单编辑模块中有效:

You should use defaultValue property of the editoptions to set default values for the new added row. In the current documentation you can find that the option is valid only in Form Editing module:


该选项可以是字符串或函数。当在添加模式下与editGridRow方法一起使用时,此选项仅在
表单编辑模块中有效。如果定义了
,则只有元素为
为空时才使用此值设置input元素。如果用于选择,则应提供文本而不是密钥。
此外,当使用函数时,函数应返回值。

The option can be string or function. This option is valid only in Form Editing module when used with editGridRow method in add mode. If defined the input element is set with this value if only element is empty. If used in selects the text should be provided and not the key. Also when a function is used the function should return value.

但如果检查新 addRow 方法,您将看到

but if you examine the code of new addRow method you will see that


  1. useDefValues 选项的默认值为 true

  2. 方法确实使用(参见此处 defaultValue 属性> editoptions

  1. the default value of the useDefValues option is true
  2. the method do use (see here) the defaultValue property of the editoptions.

更新:好的!现在我看到了你的问题。您在 editRowParams addRowParams 部分设置中使用了错误的属性。正确的将是:

UPDATED: OK! Now I see your problem. You used just wrong properties in editRowParams and addRowParams parts of the settings. Correct will be:

$grid.jqGrid('inlineNav', topPagerSelector, {
    addParams: {
        position: "beforeSelected", 
        rowID: '_empty',
        useDefValues: true,
        addRowParams: {
            keys: true,
            oneditfunc :  onInlineEdit
        }
    },
    editParams: {
        keys: true,
        oneditfunc: onInlineEdit
    },
   add: true,
   edit: false,
   save: true,
   cancel: true
});

此外你可以使用新的$.jgrid.inlineEdit 功能设置 oneditfunc 或其他内联编辑参数。该功能的实现不完全正确,但您可以从github检查当前版本(参见这里)并在您的 jquery.jqGrid.src.js 版本中进行相同的修改。无论如何,我建议使用 $。jgrid.inlineEdit 功能。优点是您可以轻松设置 editRow 的选项,与调用函数的位置无关(来自 inlineNav 'actions' formatter或任何其他方式。)

Moreover you can use new $.jgrid.inlineEdit feature to set keys, oneditfunc or other parameters of inline editing. The implementation of the feature is not full correct, but you can examine the current version from github (see here) and do the same modification in your version of jquery.jqGrid.src.js. In any way I would recommend to use the $.jgrid.inlineEdit feature after publishing the next version of jqGrid. The advantage is that you can easy set options of editRow independent from where the function will be called (from inlineNav, 'actions' formatter or any other way).

jqGridInlineEditRow 事件功能(参见这里以获取更多信息)将允许您实现类似于您现在在 onInlineEdit 事件内执行的操作没有使用 oneditfunc ,只能设置一次。

New jqGridInlineEditRow event feature (see here for more information) will allow you to implement actions like what you do now inside of onInlineEdit event without the usage of oneditfunc which can be set only one time.

这篇关于如果使用jqgrid中的内联添加按钮添加行,如何设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆