Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Want to add javascript function on Date field

  1. #1
    pravin.agham is offline Senior Member
    Join Date
    Sep 2010
    Posts
    64

    Default Want to add javascript function on Date field

    Hi,

    I have 2 field of dates one is From Date and another Is To date.
    I want to add functionality on Form Date with JavaScript for increase the To Date value by 1 month from From Date Field.

    For eg-
    if I select From Date value 14 January then the To Date Field is changed to 15 February automatically.

    I tried the JavaScript function to date field but it wont come in edit view page.

    Please help me out!!!


    Regards,
    Pravin

  2. #2
    pravin.agham is offline Senior Member
    Join Date
    Sep 2010
    Posts
    64

    Default Re: Want to add javascript function on Date field

    no one can help me!!

    How to add JavaScript function to date field through custom code!! I triyed
    PHP Code:
     'displayParams' => 
                array (
                  
    'javascript' => 'onchange="difference();"',
                ), 

    but not working.


    Regards,
    Pravin.

  3. #3
    swilliams is offline Sugar Community Member
    Join Date
    Apr 2007
    Posts
    21

    Default Re: Want to add javascript function on Date field

    I was trying to execute some javascript when a date was picked and found this post when looking for an answer. So, here's what I did to get it to work.

    I duplicated the Datetime Sugarfield so I could make a change and it would be upgrade safe.

    Copy the contents of include/SugarFields/Datetime to a new folder.
    Example include/SugarFields/Datetimecustom

    Rename SugarFieldDatetime.php to SugarFieldDatetimecustom.php

    Edit it and change the class name from SugarFieldDatetime to SugarFieldDatetimecustom and the last line:

    return $this->fetch('include/SugarFields/Fields/Datetime/EditView.tpl');

    to

    return $this->fetch('include/SugarFields/Fields/Datetimecustom/EditView.tpl');

    If you'll look at jssource/src_files/jscalendar/calendar-setup_3.js, you will see the available config parameters. The one I was interested in was onUpdate.

    So, edit EditView.tpl and add the onUpdate parameter to the calendar setup and give it the function of your choosing.

    Example:

    <script type="text/javascript" language="javascript">
    Calendar.setup ({ldelim}
    inputField : "{{sugarvar key='name'}}",
    daFormat : "{$CALENDAR_FORMAT}",
    button : "{{sugarvar key='name'}}_trigger",
    singleClick : true,
    dateStr : "{$date_value}",
    step : 1,
    onUpdate : calUpdate
    {rdelim}
    );
    </script>

    Now you will need to define a javascript function called calUpdate and include it in the view.

    And lastly, you will need to edit the editviewdefs.php and add a type def to point to the newly defined SugarField.

    Example

    array (
    'name' => 'my_date',
    'label' => 'LBL_MY_DATE',
    'type' => 'Datetimecustom',
    ),

    You will then need to do do a rebuild/repair to update the cache and then when a date is selected using the datepicker, it will call your javascript callback.

    Hope this helps.

    Steve

  4. #4
    pravin.agham is offline Senior Member
    Join Date
    Sep 2010
    Posts
    64

    Default Re: Want to add javascript function on Date field

    Yes swilliams, I tried this and its works.

    Thanks for replying.


    Regards,
    -Pravin

  5. #5
    mrpele is offline Member
    Join Date
    Nov 2010
    Posts
    14

    Default Re: Want to add javascript function on Date field

    Quote Originally Posted by swilliams View Post
    I was trying to execute some javascript when a date was picked and found this post when looking for an answer. So, here's what I did to get it to work.

    I duplicated the Datetime Sugarfield so I could make a change and it would be upgrade safe.

    Copy the contents of include/SugarFields/Datetime to a new folder.
    Example include/SugarFields/Datetimecustom

    Rename SugarFieldDatetime.php to SugarFieldDatetimecustom.php

    Edit it and change the class name from SugarFieldDatetime to SugarFieldDatetimecustom and the last line:

    return $this->fetch('include/SugarFields/Fields/Datetime/EditView.tpl');

    to

    return $this->fetch('include/SugarFields/Fields/Datetimecustom/EditView.tpl');

    If you'll look at jssource/src_files/jscalendar/calendar-setup_3.js, you will see the available config parameters. The one I was interested in was onUpdate.

    So, edit EditView.tpl and add the onUpdate parameter to the calendar setup and give it the function of your choosing.

    Example:

    <script type="text/javascript" language="javascript">
    Calendar.setup ({ldelim}
    inputField : "{{sugarvar key='name'}}",
    daFormat : "{$CALENDAR_FORMAT}",
    button : "{{sugarvar key='name'}}_trigger",
    singleClick : true,
    dateStr : "{$date_value}",
    step : 1,
    onUpdate : calUpdate
    {rdelim}
    );
    </script>

    Now you will need to define a javascript function called calUpdate and include it in the view.

    And lastly, you will need to edit the editviewdefs.php and add a type def to point to the newly defined SugarField.

    Example

    array (
    'name' => 'my_date',
    'label' => 'LBL_MY_DATE',
    'type' => 'Datetimecustom',
    ),

    You will then need to do do a rebuild/repair to update the cache and then when a date is selected using the datepicker, it will call your javascript callback.

    Hope this helps.

    Steve


    Thanks Steve for this superb information.
    Respect!

  6. #6
    kgrise is offline Junior Member
    Join Date
    Jun 2011
    Posts
    7

    Default Re: Want to add javascript function on Date field

    I ran into this same issue and was able to fix it with this solution for editview, but what about quickcreate? I tried applying the same type to the quickcreatedefs.php and have the EditView.tpl file, etc in place. I get an error saying that it can't find my javascript function, but it is defined. Any thoughts?

  7. #7
    crewen is offline Junior Member
    Join Date
    Jun 2011
    Posts
    1

    Default Re: Want to add javascript function on Date field

    i have the same problem with quickcreate. any help?
    Last edited by crewen; 2011-06-26 at 01:39 PM.

  8. #8
    rafael.q.g@hotmail.com's Avatar
    rafael.q.g@hotmail.com is offline Sugar Community Member
    Join Date
    Jun 2011
    Location
    Florianópolis - Brazil
    Posts
    782

    Default Re: Want to add javascript function on Date field

    For date fields sugar overwrite the onchage function that was set on onchange event.
    For this cases you have to assign the function to the event after it is loaded on screen, like:

    document.getElementById("field_id").onchange=funct ionName;
    Rafael Queiroz Gonçalves
    Advanced OMG UML Certified Professional
    Sun Certified Enterprise Architect for the Java Platform
    Sun Certified Programmer for the Java 2 Platform
    IBM Certified Advanced Application Developer - Lotus Notes and Domino
    IBM Certified Application Developer - IBM WebSphere Portlet Factory
    Computer Science Mastering / UFSC - PPGCC

  9. #9
    madineclark2001 is offline Junior Member
    Join Date
    Jun 2011
    Posts
    1

    Default Re: Want to add javascript function on Date field

    I have the same problem and thank you for sharing

  10. #10
    kgrise is offline Junior Member
    Join Date
    Jun 2011
    Posts
    7

    Default Re: Want to add javascript function on Date field

    Thank you Rafael for responding, but your answer doesn't fully apply. The change event (when placed inside $(document).ready) only works when you type into the date textbox and tab out. That will trigger the update I need for the other textbox, but the change event doesn't work when I choose a date from the calendar. It's basically ignored. I implemented what was suggested above and it works great for editview, but not for quickcreate. My question is really why the onUpdate inside my EditView.tpl can't find my javascript function?

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Adding Javascript function to EditViewDef
    By Shargat in forum Developer Help
    Replies: 1
    Last Post: 2011-01-17, 07:44 AM
  2. Need Help: Calling Save Function from Javascript
    By nagarajkonaguthi@gmail.com in forum Developer Help
    Replies: 3
    Last Post: 2010-03-17, 12:16 PM
  3. Replies: 1
    Last Post: 2008-06-04, 04:23 PM
  4. Problem of check_form function on date field
    By sorrentinov in forum Developer Help
    Replies: 1
    Last Post: 2007-11-27, 09:07 AM
  5. Replies: 0
    Last Post: 2006-07-07, 02:13 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •