javascript 如果使用jqgrid中的内联添加按钮添加行,如何设置默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8872863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:50:15  来源:igfitidea点击:

How to set default values if row is added using inline add button in jqgrid

javascriptjqueryjquery-uijqgrid

提问by Andrus

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.

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

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;
    }
  }

Update

更新

I tried code

我试过代码

 $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;
                }
             }
        }
    }
);

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

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

Update 2

更新 2

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

我从回答中推荐的github向jqGrid添加了补丁并尝试

$.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.

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

采纳答案by Oleg

You should use defaultValueproperty of the editoptionsto 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:

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

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.

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

but if you examine the code of new addRowmethod you will see that

但是如果你检查新的addRow方法的代码,你会看到

  1. the default value of the useDefValuesoption is true
  2. the method do use (see here) the defaultValueproperty of the editoptions.
  1. useDefValues选项的默认值为true
  2. 该方法确实使用(参见此处editoptionsdefaultValue属性。

UPDATED: OK! Now I see your problem. You used just wrong properties in editRowParamsand addRowParamsparts of the settings. Correct will be:

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

$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
});

Moreover you can use new $.jgrid.inlineEditfeature to set keys, oneditfuncor 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.inlineEditfeature after publishing the next version of jqGrid. The advantage is that you can easy set options of editRowindependent from where the function will be called (from inlineNav, 'actions'formatter or any other way).

此外,您可以使用新的$ .jgrid.inlineEdit功能设置keysoneditfunc或内联编辑的其它参数。该功能的实现并不完全正确,但您可以从 github(请参阅此处)检查当前版本并在您的jquery.jqGrid.src.js. 无论如何,我建议在发布下一版本的 jqGrid 后使用$.jgrid.inlineEdit功能。优点是您可以轻松设置editRow独立于函数将被调用的位置的选项(从inlineNav'actions'格式化程序或任何其他方式)。

New jqGridInlineEditRowevent feature (see herefor more information) will allow you to implement actions like what you do now inside of onInlineEditevent withoutthe usage of oneditfuncwhich can be set only one time.

jqGridInlineEditRow事件的特征(见这里获得更多信息)可以让你实现你喜欢做什么,现在里面的动作onInlineEdit没有的使用oneditfunc可以只设定一个时间。