Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default values if row is added using inline add button in 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;
    }
  }

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.

like image 685
Andrus Avatar asked Jan 15 '12 19:01

Andrus


1 Answers

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.

like image 148
Oleg Avatar answered Oct 01 '22 00:10

Oleg