DataEvents in cloumns.

Jan 18, 2012 at 1:26 PM

I´m not pretty sure how to use this option (DataEvents) within my code. I need to Throw one event once I change a value on Edit mode..

 

Would You have an example? In the code, You recommed to overwrite the attribute DataEvents.

 

Really great job with this library!

Coordinator
Jan 18, 2012 at 7:58 PM

Hi,

DataEvents property allows you to bind client-side javascript events to the element. Internally jqGrid is using jQuery.bind to achieve this:

$('#id').bind(JqGridColumnDataEvent.Type, [JqGridColumnDataEvent.Data], JqGridColumnDataEvent.Function);

Here you have a very simple example of overriding this property:

public class JqGridColumnCustomEditableAttribute : JqGridColumnEditableAttribute
{
    #region Properties
    protected override IList<JqGridColumnDataEvent> DataEvents
    { 
        get
        {
            return new List<JqGridColumnDataEvent>()
            {
                new JqGridColumnDataEvent("change", "function(e) { alert(e.data.Prompt + $(this).val()); }", new { Prompt = "Element value: " }),
                new JqGridColumnDataEvent("keypress", "function(e) { console.log('keypress'); }")
            };
        } 
    }
    #endregion
}

You can use custom attribute like this instead of standard attribute.

Regards,
Tomasz Pęczek

Jan 19, 2012 at 1:11 PM
Edited Jan 19, 2012 at 1:16 PM

Many thanks for this quick response!

 

First of all I tried Your code and What I got is an error "Input string was not in a correct format." on this line 

javaScriptBuilder.AppendFormat("{ type: '{0}', fn: {1} }, ", dataEvent.Type, dataEvent.Function);

I would like to use this jqgrid table with your library in differentes tables and on this way I would have to use so much classes overwriting this dataevent for every single column I want to bind an event. Should it be the right this one? Or could I have another option in which I can define as an attributte.

I´m thinking right now if it´s possible would to create one object where We can define The typical events... anw we can fill IList<JqGridColumnDataEvent> DataEvents thanks to this object....

 public  class jqGridColumnEditAttributeDataEvents
    {
        public string OnChanged { get; set; }
        public string OnKeyPress { get; set; }
        public string OnClick { get; set; }
        ......
    }

   [Display(Name="c")]
   [JqGridColumnFormatter(JqGridColumnPredefinedFormatters.Currency)]
   [jqGridColumnEditAttributeDataEvents(OnChanged="ChangeValue()",OnKeyPress="OnKeyPressFunction()")]
   public override Nullable<decimal> Quantiy { get; set; }




Coordinator
Jan 19, 2012 at 8:01 PM
Edited Jan 19, 2012 at 8:30 PM

Hi,

I have created work item for the issue you have discovered - I'm planning to release fix today or tomorrow (as soon as possible).

There is a possibility of creating attribute which would work like you have described, it would look more less like this:

public class JqGridColumnDataEventsEditableAttribute : JqGridColumnEditableAttribute
{
    #region Properties
    public string OnChange { get; set; }

    public string OnKeyPress { get; set; }

    public string OnClick { get; set; }

    protected override IList<JqGridColumnDataEvent> DataEvents
    {
        get
        {
            List<JqGridColumnDataEvent> dataEvents = new List<JqGridColumnDataEvent>();

            if (!String.IsNullOrWhiteSpace(OnChange))
                dataEvents.Add(new JqGridColumnDataEvent("change", OnChange));

            if (!String.IsNullOrWhiteSpace(OnKeyPress))
                dataEvents.Add(new JqGridColumnDataEvent("keypress", OnKeyPress));

            if (!String.IsNullOrWhiteSpace(OnClick))
                dataEvents.Add(new JqGridColumnDataEvent("click", OnClick));

            return dataEvents;
        }
    }
    #endregion

    #region Constructor
    public JqGridColumnDataEventsEditableAttribute(bool editable)
        : base(editable)
    { }

    public JqGridColumnDataEventsEditableAttribute(bool editable, string dataUrlRouteName)
        : base(editable, dataUrlRouteName)
    { }

    public JqGridColumnDataEventsEditableAttribute(bool editable, string dataUrlAction, string dataUrlController)
        : base(editable, dataUrlAction, dataUrlController)
    { }

    public JqGridColumnDataEventsEditableAttribute(bool editable, string dataUrlAction, string dataUrlController, string dataUrlAreaName)
        : base(editable, dataUrlAction, dataUrlController, dataUrlAreaName)
    { }
    #endregion
}
Jan 20, 2012 at 8:51 AM

That would be very useful.

Many Thanks!!

Coordinator
Jan 20, 2012 at 9:54 PM

The release is available for download. I have tested the code with sample class described above so you can freely use it.

Regards,
Tomasz Pęczek

Feb 21, 2013 at 1:06 PM
I try the code above but when i create the class JqGridColumnDataEventsEditableAttribute
i get this error "Cannot inherit from sealed class JqGridColumnEditableAttribute"
any help?
Coordinator
Feb 21, 2013 at 1:19 PM
Hi

It looks like you are using Lib.Web.Mvc version earlier than 4.4.0. Please verify which version you are using and update if needed (the NuGet package is the best way to always stay up to date).

Regards,
Tomasz Pęczek