tonytomov / jqGrid

jQuery grid plugin
www.trirand.com
2.84k stars 1.2k forks source link

Proposal - Get temporary rowData while editing #1054

Open unle70 opened 7 months ago

unle70 commented 7 months ago

Hi Tony,

I would like to propose one new feature, based on a situation I'm facing now. When you define a cusom-type column which has "choices" defined (like: INPUT with DATALIST in my case), sometimes you want to make the list of choices "dynamic". What I mean is, when the field gets focus, the list of choices should be created based on the value of other fields in the row.

The problem is that currently, the "rowData" object which is passed to the custom element creation function, does not reflect any changes which have already been done in the edit form. The values are only the "official" ones, before the planned changes. This way I cannot really see the changes which have not yet been saved.

Getting the field values from the edit form is slightly complicated. You need to consider different types, including custom types. I see that you are doing this in your code (when creating the postdata), but this code is not available for me to call and use. I was wondering if there's any way you could make this service available to users, so that I could call it and get the CURRENT data of the edit form. BTW, I guess the same applies for inline editing.

Thanks, Udi

tonytomov commented 7 months ago

Hello Udi,

Thank you for the recommendation - much appreciate all your efforts regarding jqGrid .

To the request. I do not think that we need to add some additional code for what you need. You can use simple command to get any current value in the form. Maybe you missed some paragraph into the doc. In docs for editGridRow method when you scroll down you will see the following

================================================================================= What is need to know?

When the form is constructed we set the following rules:

This allow us to easily show or hide some table rows depending on conditions using beforeShowForm event

==================================================================================

Having this you can get easy any current value from the form like:

$("#CustomerID").val();

or better if the grid has id grid

$("#CustomerID", "#FrmGrid_grid").val();

In case you have different input types like checkbox, radio and etc you can simply check its type like

var field_type = $("#CustomerID", "#FrmGrid_grid")[0].type

and do appropriate o get the right value

Similar to this there is description for cell edit and inline edit.

Hope this will solve your problem

Best Regards, Tony

tonytomov commented 7 months ago

or another easy current form value get (except maybe for some complex custom elements)

If the jqGrid has id = grid

var formid = "#"+"FrmGrid_grid",
   fields = $(formid).serializeArray(),
   griddata = {};
$.each(fields, function(i, field){
    griddata[field.name] = field.value;
});

or using this thread you can add your own function:

 /*!
 * jQuery serializeObject - v0.2 - 1/20/2010
 * http://benalman.com/projects/jquery-misc-plugins/
 * 
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */

// Whereas .serializeArray() serializes a form into an array, .serializeObject()
// serializes a form into an (arguably more useful) object.

(function($,undefined){
  '$:nomunge'; // Used by YUI compressor.

  $.fn.serializeObject = function(){
    var obj = {};

    $.each( this.serializeArray(), function(i,o){
      var n = o.name,
        v = o.value;

        obj[n] = obj[n] === undefined ? v
          : $.isArray( obj[n] ) ? obj[n].concat( v )
          : [ obj[n], v ];
    });

    return obj;
  };

})(jQuery);

Usage

var current_form_data = $( "#"+"FrmGrid_grid").serializeObject();
unle70 commented 7 months ago

Hi Tony,

Well, if I look at your code for constructing "postdata", indeed this is what I see. Generally speaking, I would need to copy everything you have there, like: Getting simple values (input), getting values from standard type fields (select, buttons, etc.), getting values from custom fields, and eventually unformatting. You have done all of this very well, so I was just hoping I would have a way of using your code. This way, if ever you add more standard types, or any additional functionality, it would automatically be supported and available to me.

But I understand it if you don't see potential for other people using this feature.

Thank you for creating and maintaining jqGrid all these years.