Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

How to set a time field with a Javascript button?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to set a time field with a Javascript button?

    I have a linked grid with a time field {TO4_Time} with a button next to it in a tabular upgradable grid.

    I would like to figure out how to use the button's JavaScript to set the value of the time field to Now() when pressed.

    I have tried several variations but no luck with the callback or JavaScript. I am also connected to SQL tables.

    I am sure it there is a simple way - any ideas?

    Thanks!

    #2
    Re: How to set a time field with a Javascript button?

    this works
    Code:
    var d = new Date();
    {grid.Object}._setValue('G','TO4_TIME',{Grid.RowNumber},d.toLocaleTimeString());
    your field must be uppercase

    Comment


      #3
      Re: How to set a time field with a Javascript button?

      Lance,
      Your code worked perfectly without modification - so rare, thank you.

      There is always a "however" it seams with coding . . the code will display 12:25:47 AM but it stores the value in my SQL table as 2015-12-21 00:00:00 (that may not be the exact corresponding match gives you the idea).

      I have tried changing the SQL Date/Time type as well as changing the Alpha Display Format to no avail for saving it correctly.

      So it displays it well but doesn't save it well. I ideally would like to display it as military time alone without a date but that is a small point.

      Do you know what combination I need on the Alpha and SQL side for this date field?

      Thanks again!

      Comment


        #4
        Re: How to set a time field with a Javascript button?

        Sorry Michael,

        I got a bit confused and should have read your first post fully, thought you just wanted the time in the field. You actually asked for the javascript to enter the NOW() value. This is even simpler to attain you only need this one line
        Code:
        {grid.Object}._setValue('G','TO4_TIME',{Grid.RowNumber},Date());
        this will save the current date and time in your field, if you only want to show the time in the field you will have to do some formatting

        Comment


          #5
          Re: How to set a time field with a Javascript button?

          Thank you Lance.

          I spent several hours of playing around with different combination of data formats and looking up JavaScript examples online and ended up going with your first example and saving it to SQL as text.

          I am obviously by no means a programmer but this will work. However - I am more curios than anything else to find a way to save it to SQL as a date and in military time.

          Also, what would be very cool is if there was a JavaSript or Ajax callback that would display and save the time automatically!

          Thanks again!

          Comment


            #6
            Re: How to set a time field with a Javascript button?

            Sorry Michael don't do any SQL (although I keep meaning to), but to get the time in military format every time you enter a new record is just a matter of entering something like
            Code:
            e.newValues.TO4_time = time()
            in the onInitialValueCalculate event for your grid. Of course this will, once again, save the data incorrectly in your field at the SQL end, but I am sure with a bit of playing you could get it to work as these are in good old xbasic.

            Comment


              #7
              Re: How to set a time field with a Javascript button?

              For those who are interested and don't fully understand the Alpha use of JavaScript with a button (as in this example and like me), I will explain how I got my solution with the help of Lance's examples (thank you Lance).

              I like to know "why" things work so I typically spend way too much time digging so I hope this will help someone who is at the same place as I.

              I wanted a button that a client can clicked that will insert a military time into a field, or the client can type a military time to be what ever time they want.

              I am using Alpha 10 with an SQL backend. So I added the button, on Field Properties, Javascript, "onClick", I added an Action Script - Inline-Javascript with the following code:

              Code:
              var d = new Date();
              {grid.Object}._setValue('G','DOSE',{Grid.RowNumber},d.getHours()+':'+d.getMinutes());
              So what does this mean?
              new Date() is just a JS date object to hold a date value. It will automatically hold the current date and time as its initial value.

              getHours() and getMinutes() are JS commands to get the Hour and Minute from the new Date varable, and so I just strung them together as they will be stored in a character field in SQL.

              {grid.Object}._setValue(part,name,row,value) is the method used by Alpha to set the value of my field: TO4_DATE

              Using the above method, all I had to do is to insert the "part" of the control ('G' for grid, 'D' for detail view, 'S' for Search part)
              Add my field name to "name", (TO4_DATE)
              Row is using a place holder called {Grid.RowNumber} that will insert the correct row for you
              Value is what I want placed in my field, in this case the value of the string expression.

              That is it - works very nicely. It wasn't exactly what I wanted as I could not get a date time to store in SQL and I am sure there is a way, but I learned a whole lot more about these new features for Alpha 10! I hope someone else will benefit from my ramblings.

              Comment


                #8
                Re: How to set a time field with a Javascript button?

                Lance (or anyone else),

                Your code does work very well with a button press except if the minutes are less than 10, like 08:05, then it displays the military time as 8:5.

                I found a js function to fix this but not sure how to make it work with the alpha script. Here is what I found on a website and tried to replace my working script which gives me a script error:
                Code:
                var hours = new Date();
                var minutes = new Date();
                
                var d= (hours%12==0?"12":hours%12) + ":" + (minutes<10?"0"+minutes:minutes);
                {grid.Object}._setValue('G','TIME',{Grid.RowNumber},d);
                Any ideas on how to work this in?

                Thanks!

                Comment


                  #9
                  Re: How to set a time field with a Javascript button?

                  Michael,

                  You nearly got it right but it is a bit of a combination between both of your last two to get it to work. Here goes:
                  Code:
                  var now = new Date();
                  var hours = now.getHours();
                  var minutes = now.getMinutes();
                  var d= (hours%12==0?'12':hours%12) + ':' + (minutes<10?'0'+minutes:minutes);
                  
                  
                  {grid.Object}._setValue('G','TIME',{Grid.RowNumber},d);

                  Comment


                    #10
                    Re: How to set a time field with a Javascript button?

                    PERFECT!! I love it when these things work - thanks for your help Lance.

                    Comment


                      #11
                      Re: How to set a time field with a Javascript button?

                      I know this post goes back about a year, but I'm wondering if any of you figured out how to get the date formatted correctly when using javascript?

                      I have a need to post a Date in one field and the current time in military style in the other, and I'm stuck on the date part. (The time is working perfectly, thanks to your posts.)

                      After researching on the internet, I found this posted on a Javascript Date Format page:
                      ------------------------------------------------------
                      var now = new Date();

                      now.format("m/dd/yy");
                      // Returns, e.g., 6/09/07
                      -------------------------------------------------------

                      So I tried this:

                      var now = new Date();

                      now.format("m/dd/yy");

                      {grid.Object}._setValue('G',TEST',{Grid.RowNumber},now);

                      But Alpha tells me there is a syntax error, and I'm assuming it has to do with the "m/dd/yy" format, even though that is the way it is shown on the internet.

                      Has anyone mastered this?
                      Sergeant Richard Hartnett
                      Hyattsville City Police Department
                      Maryland

                      Comment


                        #12
                        Re: How to set a time field with a Javascript button?

                        Rich,

                        You have a typo (missing the opening quote on TEST), but you also are using a method that is not standard js (seems to be part of a js library).

                        But you can do it this way:

                        On you button, or whatever event, add an Ajax Callback, with the function name set to something like getCurrentDate. Then define the callback (click the Open Xbasic Function Declarations link at the bottom of the Ajax Callback screen) as:

                        Code:
                        function getCurrentDate as c (e as p)
                            getCurrentDate = "{grid.Object}._setValue('G','TEST',{Grid.RowNumber},'" + dtoc(date()) + "');"
                        end function
                        This should do it.
                        Stephen Pilon
                        Associate Librarian
                        Christendom College

                        Comment


                          #13
                          Re: How to set a time field with a Javascript button?

                          Stephen it works like a champ, and much easier than the other method, but let me ask you one more question -

                          I want to fill two fields at the same time using the same button (date & time). I tried the below xbasic script but it only fills one of the two fields. What am I doing wrong here?

                          function getCurrentDate as c (e as p)
                          getCurrentDate = "{grid.Object}._setValue('G','DATE_BEGIN',{Grid.RowNumber},'" + dtoc(date()) + "');"
                          getCurrentDate = "{grid.Object}._setValue('G','TIME_BEGIN',{Grid.RowNumber},'" + time("0h0m") + "');"
                          end function

                          Thanks
                          Last edited by Rich Hartnett; 12-17-2010, 12:20 AM.
                          Sergeant Richard Hartnett
                          Hyattsville City Police Department
                          Maryland

                          Comment

                          Working...
                          X