Array repeats simultaneously in json

Asked

Viewed 100 times

1

I am trying to make a calendar where I will get the information in a request Rest in Angularjs. What I want to create a calendar where you can record your events. With the json date attribute he enters that event and registers an icon on that day.

My mistake is as you can see in the image it will fetch an array and insert it into all fields and not just the day it was registered in json. My function repeats 42 times the number of the squares in the calendar. Does anyone know where the error in my approach is that I don’t understand.

Imagery

inserir a descrição da imagem aqui

Json

{"data":[
    {"title":"John", 
     "start":"2016-10-22"
    }, {
     "title":"João", 
     "start":"2016-10-25"
    }
]}

controller

$scope.setDayContent = function(date) {

    return $http ({
        method : "GET",
        url : "app/components/home/controller/test_calendar.json"
      }).then(function mySucces(response) {
            console.log(response.data.data[1]);
        }, function myError(response) {
          $scope.valor = response.statusText;
      }); 

html

    <calendar-md flex layout layout-fill
          calendar-direction="direction"
          on-prev-month="prevMonth"
          on-next-month="nextMonth"
          on-day-click="dayClick"
          ng-model='selectedDate'
          week-starts-on="firstDayOfWeek"
          tooltips="tooltips"
          day-format="dayFormat"
          day-content="setDayContent"
          ></calendar-md> 

version 2:

 $scope.loadData = function(){

      return $http ({
        method : "GET",
        url : "app/components/home/controller/test_calendar.json"
      }).then(function mySucces(response) {
           $scope.jsonData = response.data.data;
        }, function myError(response) {
          $scope.valor = response.statusText;
      }); 
    }


    $scope.setDayContent = function(date, content) {


//check if $scope.jsonData contains date parameter
// return title

    }; 
  • Your problem seems to me that every day you are calling an ajax request. If you see on the console you probably have 42 ajax calls, as opposed to just one. I’m not familiar with the angular API to help, but if no one answers, I’ll take a look at the weekend. +1

  • I need to create a logical attribute so that he realizes that that specific date can only show 1 time and not 42 times. I think this is it, correct me if I’m wrong

1 answer

0

I already understand the point that is generating this search loop.

Your setDayContent is called by the component so you can inform the content of the day, but you are not treating the parameters passed by the component and searching for the day it requested, but returning the whole calendar.

Suggestion: Treat your setDayContent to search in the request only the day he asked, which is in the parameters that the component sends you.

Or, load events before loading the component and into your setDayContent you search this array or event object already loaded by the date it requested you via parameter.

And you better go back to the component page you are using and read its documentation better.


UPDATE

So man, I took a look at documentation.

That’s right, your mistake is not to filter the date the component requested. The component te the option to reply with html, or with a precedent.

  • the problem is that there is no documentation for this part. But I have now created a function before setdaycontent with the get request to json and within setdaycontent I will try to fetch only the date

  • I’ll edit my code and tell me if I’m going a good way I don’t understand.

  • Giovane, please make a suggestion as to what the code should look like to solve the problem. @Josecherry please do not change the question to adapt to the answers that come up. Otherwise the question and the answer is meaningless.

  • @Sergio won’t go by code because I’ve never worked with this component, and the solution is simple. His setDayContent have to answer the date the component asks, ready, make it answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.