Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Pobieranie danych z bazy danych, Pobieranie danych z bazy danych
K3n0
post
Post #1





Grupa: Zarejestrowani
Postów: 46
Pomógł: 0
Dołączył: 3.05.2016

Ostrzeżenie: (0%)
-----


Witam, Tak jak w tytule mam problem z pobieraniem danych z tablicy. Musze pobrać wszystkie elementy z tablicy save_events. W controllerze zrobilem zapytanie wyglada mniej wiecej w ten sposob:
  1. $eventColored = DB::table('save_events')->get(['users_id','events_id']);
  2. return response()->json(['eventColored'=>$eventColored])->$eventColored = array('eventColored' => $eventColored );


I jak czytałem za pomoca ajaxa moge pobrac dane i przypisać je do zmiennej, dzieki ktorej bede mogl dopasowac kolor eventu. I tutaj jest pytanie jak przypisać dane do zmiennej, wydaje sie błache ale szczerze nie mam pojecia.

Kod
$.ajax({    
type: "GET",
url: "{!!route('color')!!}",            
dataType: "json",
data:{},                
success: function(data)
     {
        var JSONString=data;
        var JSONObject = JSON.parse(JSON.stringify(data));
            console.log(JSONObject);      
         alert(JSONObject);
       },                                               
});

Ogolnie pobiera mi dane i jak chce sobie sprawdzic allertem wyswietla prawidlowa ilosc tzn jak mam 2 elementy w tablicy to wyswietla mi 2 elementy w allercie ale niestety pokazuje "Object object". Kombinuje na różne sposoby lecz nic nie wychodzi.

Wynik w konsoli:
Object { eventColored: Array[1] }

Ten post edytował K3n0 12.04.2017, 10:34:05
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
K3n0
post
Post #2





Grupa: Zarejestrowani
Postów: 46
Pomógł: 0
Dołączył: 3.05.2016

Ostrzeżenie: (0%)
-----


Cały skrypt
Kod
<script src="{{ asset('js/fullcalendar') }}/fullcalendar.js"></script>
<script src="{{ asset('js/bootstrap.js') }}"></script>
<script type="text/javascript">

    $(document).ready(function() {    
        var base_url = '{{ url('/') }}';        
            $.ajax(
            {    
                type: "GET",
                url: "{!!route('color')!!}",            
                dataType: "json",
                data:{},                
                success: function(data)
                {
                    var JSONString=data;
                    var JSONObject = JSON.parse(JSON.stringify(data));
                    console.log(JSONObject.eventColored);              
                },                                          
            });
            $('#bootstrapModalFullCalendar').fullCalendar({
            weekends: true,
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek'
            },
             eventClick: function(event, jsEvent, view) {
              $('#modalTitle').html(event.title);
              $('#modalBody').html(event.name);
              $('#eventUrl').attr('href','/home/zapis/'+event.id);
              $("#startTime").html(moment(event.start).format('HH:mm '));
              if (event.end) $("#endTime").html(moment(event.end).format('HH:mm '));
              else $("#endTime").html('');                        
              $('#fullCalModal').modal();
              return false;
            },
            eventLimit: true,
            FirstDay: 1,
            contentheight : 650,
            editable : true,
            allDay : false,
            aspectRatio : 2,
            slotLabelFormat : 'HH:mm:ss',
            timeFormat : 'HH:mm',
            displayEventEnd : true,
            events: {
                url: base_url + '/api',
                error: function() {
                    alert("cannot load json");
                }
            },
             //eventColor: 'white',
/// Do funkcji eventAfterRender chce przekazac kolumne events_id z tablicy save_event.
            eventAfterRender: function (event, element, view) {
            if(event.id){
                element.css('background-color', '#cccccc');                                            
            }
                else
                 {
                    if (event.title == "Wydzial 1")
                    {
                    element.css('background-color', '#378006');
                    }
                    else if(event.title == "Wydzial 2")
                    {
                    element.css('background-color', '#ff0000');    
                    }
                    else if(event.title == "Wydzial 3")
                    {
                    element.css('background-color', '#73e600');    
                    }
                    else{
                    element.css('background-color', '#0066ff');
                    }
                }
            }
        });

    });

</script>


Zrobilem
Kod
console.log(JSONObject.eventColored[0].events_id);

Wynik w konsoli 8 i jest spoko pokazuje id i wlasnie teraz te id chcialbym przeslac do funkcji eventAfterRender, zeby porownac

I jak robie:
Kod
eventAfterRender: function (event, element, view) {
            if(event.id==JSONObject.eventColored[0].events_id){
                element.css('background-color', '#cccccc');                                            
            }

Wyskakuje blad:
jQuery.Deferred exception: JSONObject is not defined .eventAfterRender@http://localhost:8000/home:206:1

Ten post edytował K3n0 12.04.2017, 11:22:08
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 29.12.2025 - 14:24