Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Monday, April 18, 2016

c# mvc autocomplete list

I had this challenge and I had to modify several solutions to get what I wanted. This is what worked for me:

Controller had this function:
[AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Create(string Prefix)
        {
            var agency = (from N in travel_agencies.ToList()
                          where N.travel_agent_name.ToLower().StartsWith(Prefix.ToLower()) || N.travel_agent_id.StartsWith(Prefix)
                          select new { N.travel_agent_name });
            return Json(agency, JsonRequestBehavior.AllowGet);
        }

the view had this:
$(document).ready(function () {
   $('#station_id').autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: "@Url.Action("Create", "ChargeBacks")",
                        type: "POST",
                        dataType: "json",
                        data: { Prefix: request.term },
                        success: function (data) {
                            response($.map(data, function (item) {
                                return { label: item.travel_agent_name, value: item.travel_agent_id };
                            }))
                        }
                    })
                },
                messages: {
                    noResults: "", results: ""

                }
            });
 });

the html element was:

<input type="text" name="station_id" id="station_id" class="form-control" />

Sunday, October 4, 2015

Freelance Developer

It was from this same blog that I got my first freelance project...that was years ago. I had been working with P4A and had posted some code samples from projects that I had worked on and someone who found my blog had asked me for assistance on a project.

This just came to me last night that, since I am at home and I have some amount of free time on my hands that I should probably post some more code samples. So, I'll be doing that as often as I can. I will post code from PHP, JavaScript, MySQL, AngularJS...pretty much anything. I have knowledge of and experience with intel XDK, CodeIgniter, P4A...and I will challenge myself to learn something new. So, I am available for freelance projects.

Thursday, June 26, 2014

Calculating time left in minutes using Javascript

I was creating a mobile app and had a need to be able to show the available time that is left after the user has set the amount of time they have to do something. The code snippet below is from my class:
setEndTime: function(){
        var start_time = new Date();        
        Tracker.start_time = start_time;
        
        var end_time = new Date( start_time.getTime() + ( parseInt(Guests.available_time)*60000 ) );
        Tracker.end_time = end_time;
    },
    
    calculateAvailableTime: function(){
        var current_time = new Date();        
        var difference = current_time - Tracker.start_time;
        difference /= 1000;
        var time_passed = Math.round(difference / 60 );   
        var time_left = parseInt(Guests.available_time) - time_passed;
        Tracker.time_left = time_left;
        var time_value = ( time_left > 1 ) ? " mins" : " min";
        $("#time_left").html(time_left + time_value);
    },
This is how it works. The user sets the time which is Guests.available_time. Upon clicking the button to set the time available, I call the setEndTime function which uses the Javascript date object to capture the time. I set it globally in another class. I then calculate the end time by using the new Javascript object and adding the time available to it. I multiply by 60000 to convert it to timestamp. To get the time that is left, I used a case to capture the page name and then call the calculateAvailableTime function which takes the current time using the Javascript object and then I calculate the difference by subtracting the start time from it. I divide the difference by 1000 to remove the milliseconds then I divide by 60 to get the minutes. I then subtract it from the time that the user had set to get the time that is left.