6

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

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 ?

Update 2

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.

Andrus
  • 26,339
  • 60
  • 204
  • 378

1 Answers1

5

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:

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.

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

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

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

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

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.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thank you very much. Code in question retrieves default values from current row and does not allow to add row if current row is not set. How to implement this using defaultValue? – Andrus Jan 15 '12 at 20:45
  • @Andrus: The be exactly the code do different things. The lines like `$('#' + 'Recordtype' + '.FormElement').val('Veerg');` and `$('#' + 'Nait2' + '.FormElement')[0].checked = true;` are pure setting of default values. Because of other settings I don't understand why you not do the settings inside of `onEdit` handler (in `onInlineEdit` function in your case)? – Oleg Jan 15 '12 at 21:28
  • I need to get values from current row. I do'nt know how to get current row values in onInlineEdit. Probably current is already added row, no way to get previous row values. also if there is no current row, inline add shoudl be blocked. onInlineedit does not allow to canced add operation, new row is already added. – Andrus Jan 16 '12 at 20:29
  • 1
    @Andrus: I don't sure that I understand correct your problem. The id of the current row is the parameter of onInlineEdit. The rule for the ids of the controls (textbox, chechbox, select and so on) in the editing rows are `rowid + '_' + colName` like `123_Recordtype`. You can set the values in the controls in the same way like you set values in `afterShowForm`. I don't understand why you need manually set values on input fields (like `$('#' + 'Baas' + '.FormElement').val(selRowData.Baas)`) because inline editing already do this. – Oleg Jan 16 '12 at 21:27
  • Thank you. Using code in question onInlineEdit method is **not called** if inline add button is pressed in toplevel toolbar. I don't understand how I can use this method if it is not called. `$('#' + 'Baas' + '.FormElement').val(selRowData.Baas))` sets new row field value from previous row. Add shows empty Baas element value otherwize. This line pre-fills added row value with the value from the row where Add command was invoked. – Andrus Jan 19 '12 at 17:10
  • 1
    @Andrus: OK! I see now that you use wrong parameters. I appended my answer with **UPDATED** part. – Oleg Jan 19 '12 at 18:03
  • Thank you. How to force other functions to be called also ? I updated question – Andrus Jan 20 '12 at 17:47
  • I tried also jQuery.jgrid.inlineEdit as shown in update2 in question. Al lparameters specified are ignored using this. – Andrus Jan 21 '12 at 11:09
  • 1
    @Andrus: Yo use just wrong names of parameters. Where you get the names: `onSuccess`, `afterSave`, `onError`, `afterRestore`? In the documentation of [editRow](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:inline_editing#editrow) you find `editparameters` with properties `keys`, `oneditfunc`, `successfunc`, `url`, `extraparam`, `aftersavefunc`, `errorfunc`, `afterrestorefunc`, `restoreAfterError`, `mtype`. You should just use the names. – Oleg Jan 21 '12 at 11:38
  • Thank you very much. This was the issue. I copied them from add. – Andrus Jan 21 '12 at 15:39