Eliminate duplicate time slots in Laravel and Vuejs

Currently, I am delving into laravel(5.2) and vuejs as a newcomer to both frameworks. My goal is to utilize vuejs to eliminate redundant time slots.

In my blade file, the code looks like this:

<div class="form-group">
        <label for="form-field-slots">Time Slots&nbsp;&nbsp;<span v-if="loading"><i style="color:#777777" class="fa fa-spinner fa-spin text-center"></i></span></label>
        <select class="form-control " v-model="appointment.selected_time" id="form-field-provider">
            <option value="" selected v-if="available_time_slots.length==0">--No slots are available--</option>
            <option value="" else>Select slot</option>  
            <option v-for="option in available_time_slots" v-bind:value="option">
                {{ option.time | d2d-time-format}}
            </option>
        </select>               
    </div>

The method in my vuejs file, loadAvailableTimeSlots, should function like this:

loadAvailableTimeSlots: function(newDate){
        var self = this;
        self.available_time_slots = [];
        iframe_resize(self.width);
        self.loading = true;
        var date = newDate ? newDate : moment().format('YYYY-MM-DD');
        var service_id = self.appointment.service_id;
        var provider_id = self.appointment.provider_id;

        self.appointment.date = date;
        var url = '/bookappointment/availableslots/' + service_id + '/' + date + (provider_id ? '/'+provider_id : '');

        jQuery.ajax({       
            url: url,
            success: function(data) {
                console.log("data.....with input_data");
                console.log(data.input_data);
                for(var i = 0 ;i<data.input_data.length;i++){
                    data.input_data[i].selected = false;
                }                       
                self.available_time_slots = data.input_data; 
                setTimeout(function(){iframe_resize(self.width);},50);
                self.loading = false;                               
            },
            error: function(){
                self.loading = false; 
            }
        });
    },

When I run console.log(data.input_data);, it provides results like this:

 Object { time="09:00",  providers=[1],  selected=false}
 Object { time="09:00",  providers=[1],  selected=false}
 Object { time="09:30",  providers=[1],  selected=false}
 Object { time="09:30",  providers=[1],  selected=false}
 Object { time="10:00",  providers=[1],  selected=false}
 Object { time="10:00",  providers=[1],  selected=false}

However, I aim for an output resembling this:

 Object { time="09:00",  providers=[1],  selected=false}
 Object { time="09:30",  providers=[1],  selected=false}
 Object { time="10:00",  providers=[1],  selected=false}
 Object { time="10:30",  providers=[1],  selected=false}

I would greatly appreciate any assistance. Many thanks in advance.:)

Answer №1

To eliminate duplicates from your v-for loop, create a filter and apply it as shown below:

For example:

Filter

Vue.filter('dedup', function (data) {
  var filtered = []
  for (var i = 0; i < data.length; i++) {
    var times = filtered.map(function (o) { return o.time })
    var item = data[i]
    if (times.indexOf(item.time) === -1) {
      filtered.push(item)
    }
  }
  return filtered
})

Apply Filter

<div class="form-group">
        <label for="form-field-slots">Time Slots&nbsp;&nbsp;<span v-if="loading"><i style="color:#777777" class="fa fa-spinner fa-spin text-center"></i></span></label>
        <select class="form-control " v-model="appointment.selected_time" id="form-field-provider">
            <option value="" selected v-if="available_time_slots.length==0">--No slots are available--</option>
            <option value="" else>Select slot</option>  
            <option v-for="option in available_time_slots | dedup" v-bind:value="option">
                {{ option.time | d2d-time-format}}
            </option>
        </select>               
    </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

tips for avoiding sanctum from setting its routes

Upon installation of laravel/sanctum, it automatically sets up a "GET" route with the address "sanctum/csrf-cookie". This is achieved through the execution of the defineRoutes() function in the SanctumServiceProvider.php file located at "vendor\larave ...

Adapting the column width to display or hide content with CSS styling

I have a row with 2 columns. The left column contains my main content and the right column is a chatroom. I would like users to be able to minimize and open the chatroom, which I already know how to do. However, when the chatroom is open, I want the left ...

Having trouble with an Unexpected identifier error when using setTimeout and animate together?

Here is my code snippet: setTimeout(function() $('.golden').animate({ opacity: "1", top: "84px" }, 'slow'); }, 1000 ); Ah, it throws an Unexpected identifier error. However, when I simplify the code like this: ...

initiate download upon receiving API response

I'm currently utilizing the ilovepdf REST API with PHP to upload a PDF file, compress it, and save it on my server. However, I encountered an issue where the compressed file is not automatically downloaded to the user's default browser path after ...

establishing the dimensions of table data cells

I'm facing a challenge with setting the width and height for table data that changes dynamically based on different values. The dimensions of the table itself are not definite, so I need to find a solution. Here's the code snippet I'm curren ...

NodeJS not recognizing global variable causing it to return undefined

Can a global variable be defined in a node.js function? I wish to use the variable "ko" (declared in the getNumbers function) in other functions function getNumbers(callback) { result = cio.query("SELECT numbers FROM rooms WHERE durum='1'", ...

What is the best way to decode a large data feed using PHP?

Looking for assistance in reading a large file using PHP and inserting various fields into MySQL. Each record in the file is separated by columns and rows, with the same set of fields in each one. The delimiters for each field and record are as follows: ...

Accessing PHP Array Elements from Another Array within a Class

Within a class, I am working with two associative arrays. My goal is to call elements from one array and use them in another "master" array. I'm wondering if this is possible or if I'm making a mistake; please keep in mind that the arrays provid ...

What is the best way to implement next and previous buttons in a slideshow using code?

Looking to enhance my slideshow by replacing the standard "Next" and "Previous" text buttons with custom JPEG images I've created. Anyone familiar with the steps needed to make this switch in the HTML code? Current HTML code in use: <body sty ...

Are there any solutions to refresh a page by clicking a button in next.js?

I am currently learning how to work with next.js and I'm facing an issue where I need to reload my page to make a new API request. As a beginner, I'm not sure how to achieve this. I've tried a few methods but none of them seem to work. Below ...

Adding JQuery elements to the map pane

I recently decided to upgrade one of my projects to use the Google Maps API v3 instead of v2. In v2, I used the following code to append a div to the map pane: $("#marker_popup").appendTo(map.getPane(G_MAP_FLOAT_SHADOW_PANE)); Is there a straightforward ...

Loop through the coefficients of a polynomial created from a string using JavaScript

Summary: Seeking a method to generate a polynomial from specified coordinates, enabling coefficient iteration and optional point evaluation I am currently developing a basic JavaScript/TypeScript algorithm for KZG commitments, which involves multiplying c ...

Could you please explain the specific distinctions between pipe and map within Angular 7?

After extensive research, I'm still struggling to understand the distinction between pipe and map in Angular 7. Should we always include a pipe in Service.ts file in Angular 7? Appreciate any clarification on this matter. ...

AngularJS: incorporating modules across distinct controllers

Imagine we have two distinct AngularJS controllers housed in separate files that are both referenced in an HTML document. Here's how it looks: //controller1.js "use strict"; var someApp = angular.module('MYAPP'); //var someApp = angular.mod ...

Dealing with currency symbols in Datatables and linking external sources

I'm having trouble linking an external link to the "customer_id" field. The link should look like this: /edit-customer.php?customer_id=$customer_id (which is a link to the original customer id). I am creating a detailed page with most of the informati ...

JavaScript fails to function in an HTML file

I am facing an issue where my JavaScript code works perfectly in JSFiddle, but when I copy it into an HTML file, it doesn't function as expected. Despite searching through other related posts, I have been unable to find a solution for this specific pr ...

Is it achievable to dynamically generate new pages on initial load in NextJS?

Right now, I am utilizing getStaticProps and getStaticPaths to pre-render a specific number of articles before my website goes live. This method works effectively, but if a new article is created and displayed on the front page while the site is still acti ...

Nested JavaScript function expression

In this JavaScript example, the snippet provided is for educational purposes and does not include validation. After defining the variable 'isBetween' within the buildBoundDetector() function, it raises questions about how passing a number through ...

Utilizing VueJS to Establish a Binding Relationship with Props

One of my Vue components is named Avatar.vue, and it is used to create an avatar image based on user-defined props. The parameter imgType determines whether the image should have rounded corners or not. Here is the code: <template> <div> & ...

Utilizing ajax to store information in the database

When my image slider loads the next image, I want to save a record in the database. To achieve this, I am using jQuery and AJAX. I store the necessary information in a hidden field called 'data' so that I can send it to my PHP page. function aj ...