Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Pobieranie danych z bazy danych, Pobieranie danych z bazy danych
K3n0
post 12.04.2017, 10:18:31
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
viking
post 12.04.2017, 10:37:46
Post #2





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

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


Nie alertuj tylko console.log
JSONObject.eventColored...


--------------------
Go to the top of the page
+Quote Post
K3n0
post 12.04.2017, 10:46:31
Post #3





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

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


Kod
console.log(JSONObject.eventColored);

Wynik:
Array [ Object ]

I pytanie jak moge przypisac to do zmienej zeby pozniej moc wykonac funkcje:
Kod
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');
                    }
                }
            }

jak robie event.id == JSONObject.eventColored wtedy wywala blad:

jQuery.Deferred exception: JSONObject is not defined .eventAfterRender@http://localhost:8000/home:206:1
publiclyTrigger@http://localhost:8000/js/fullcalendar.js:11387:11
FC.View<.publiclyTrigger@http://localhost:8000/js/fullcalendar.js:8480:10
FC.View<.onEventsRender/<@http://localhost:8000/js/fullcalendar.js:9381:4
FC.View<.renderedEventSegEach@http://localhost:8000/js/fullcalendar.js:9471:6
FC.View<.onEventsRender@http://localhost:8000/js/fullcalendar.js:9380:3
FC.View<.executeEventsRender/<@http://localhost:8000/js/fullcalendar.js:9344:4
Promise.resolve/promise.then@http://localhost:8000/js/fullcalendar.js:1887:29
FC.View<.executeEventsRender@http://localhost:8000/js/fullcalendar.js:9337:10
FC.View<.requestEventsRender/<@http://localhost:8000/js/fullcalendar.js:9298:11
addTask/</runFunc@http://localhost:8000/js/fullcalendar.js:1951:21
addTask/<@http://localhost:8000/js/fullcalendar.js:1968:5
Promise@http://localhost:8000/js/fullcalendar.js:1826:3
addTask@http://localhost:8000/js/fullcalendar.js:1946:10
FC.View<.requestEventsRender@http://localhost:8000/js/fullcalendar.js:9297:10
FC.View<.handleEvents@http://localhost:8000/js/fullcalendar.js:9280:3
FC.View<.setEvents@http://localhost:8000/js/fullcalendar.js:9247:3
FC.View<.bindEvents/<@http://localhost:8000/js/fullcalendar.js:9223:5
resolve/</mightThrow@http://localhost:8000/js/jquery.js:3570:21
resolve/</process<@http://localhost:8000/js/jquery.js:3638:12
Go to the top of the page
+Quote Post
viking
post 12.04.2017, 11:05:13
Post #4





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

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


Jak sobie na to Array [ Object ] dalej poklikasz dowiesz się co jest głębiej.


--------------------
Go to the top of the page
+Quote Post
K3n0
post 12.04.2017, 11:09:50
Post #5





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

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


Tak patrzylem to i mam users_id = id usera oraz events_id odpowiedzialne za id eventu ale nie wiem jak przekazac events_id do funkcji odpowiedzialnej za kolory zeby móć porownac czy event.id istnieje w tablicy save_events.
Go to the top of the page
+Quote Post
viking
post 12.04.2017, 11:13:29
Post #6





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

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


JSONObject.eventColored[0].events_id

A jak przekazać nie wiem bo z tych fragmentów kodu nic nie wynika.


--------------------
Go to the top of the page
+Quote Post
K3n0
post 12.04.2017, 11:20:09
Post #7





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
viking
post 12.04.2017, 11:43:02
Post #8





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

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


Chyba oczywiste. Poczytaj o zasięgu zmiennych. To dlaczego nie pobierzesz tych danych tam, gdzie ich potrzebujesz? Najgorszy sposób. Zrób jakąś zmienną zaraz po ready i do niej zapisz te dane.


--------------------
Go to the top of the page
+Quote Post
K3n0
post 12.04.2017, 12:00:31
Post #9





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

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


Zrobiłem tak:
Kod
var color_events =    $.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);      
                    console.log(JSONObject.eventColored[0].events_id);
                    
                },
                                                   
            });


i pozniej daje
if(event.id==color_events)
tą zmienna zeby porownac i niestety ale wszystkie ify sie wykonuja prócz tego jednego
Go to the top of the page
+Quote Post
viking
post 12.04.2017, 12:27:30
Post #10





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

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


Nie wiem co twoim zdaniem zawiera color_events ale jest tam Promise zwrócony przez ajax. Jak już, to przypisz tam wynik z success.


--------------------
Go to the top of the page
+Quote Post
K3n0
post 12.04.2017, 12:39:24
Post #11





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

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


Kod
var color_events;
        $.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);      
                    console.log(JSONObject.eventColored[0].events_id);
                    color_events = JSONObject.eventColored[0].events_id;
                    
                },
                                                 
            });
            console.log(color_events);


Wynik w konsoli:
undefined home:169:10
Array [ Object ] home:162:15
8

Ten post edytował K3n0 12.04.2017, 12:39:48
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 29.03.2024 - 08:33