2015-10-19 41 views
3

我在我的項目中使用fullcalendar.js,現在一切正常,除了我的模態窗口。我將數據庫中的數據插入fullcalendar.js!這些數據在我的日曆中正確顯示。如果我點擊一個事件模式打開,我希望顯示這些數據。fullcalendar.js - 將循環插入模態

我正在使用for-loop來實現這個功能,但它不能正常工作。看起來for循環不知道它應該在模態窗口內顯示哪些數據,因此所有的條目都顯示出來。這裏是什麼樣子,現在的截圖:

enter image description here

所以,如果我點擊一個事件,一個模式的所有條目打開,但我想實現的是,只有那些條目將顯示其來自我點擊的事件。這是我的代碼:

 <script type="text/javascript"> 
      jQuery(function($) { 

     /* initialize the external events 
     -----------------------------------------------------------------*/ 

     $('#external-events div.external-event').each(function() { 

      // create an Event Object (http://arshaw.com/fullcalendar/docs/event_data/Event_Object/) 
      // it doesn't need to have a start or end 
      var eventObject = { 
       title: $.trim($(this).text()) // use the element's text as the event title 
      }; 

      // store the Event Object in the DOM element so we can get to it later 
      $(this).data('eventObject', eventObject); 

      // make the event draggable using jQuery UI 
      $(this).draggable({ 
       zIndex: 999, 
       revert: true,  // will cause the event to go back to its 
       revertDuration: 0 // original position after the drag 
      }); 
     }); 

     /* initialize the calendar 
     -----------------------------------------------------------------*/ 

<?php 
     print "var date = new Date();\n"; 
     print "var d = date.getDate();\n"; 
     print "var m = date.getMonth()-1;\n"; 
     print "var y = date.getFullYear();\n"; 
     print "var unixTimestamp = Date.now(); // in milliseconds;" 

?> 

     var calendar = $('#calendar').fullCalendar({ 
      //isRTL: true, 
      buttonHtml: { 
       prev: '<i class="ace-icon fa fa-chevron-left"></i>', 
       next: '<i class="ace-icon fa fa-chevron-right"></i>' 
      }, 

      header: { 
       left: 'prev,next today', 
       center: 'title', 
       right: 'month,agendaWeek,agendaDay' 
      }, 
<?php 
      $dates=getPcalInfoOfHour($gl_userid,0,0); 

      print "events: [\n"; 

      for ($x=0;$x<count($dates["id"]);$x++) { 
       print " {\n"; 
       print " title: '".$dates["title"][$x]."',\n"; 
       print " start: new Date(".date("Y",$dates["start"][$x]).", ".(date("n", $dates["start"][$x]) - 1).", ".date("j",$dates["start"][$x]).", ".date("G",$dates["start"][$x]).", ".date("i",$dates["start"][$x]).",0,0),\n"; 
       print " end: new Date(".date("Y",$dates["end"][$x]+1).", ".(date("n", $dates["end"][$x]+1) - 1).", ".date("j",$dates["end"][$x]+1).", ".date("G",$dates["end"][$x]+1).", ".date("i",($dates["end"][$x]+1)).",0,0),\n"; 
       print " allDay: false,\n"; 
       print "durationEditable : false,\n"; 
       print " className: 'label-info',\n"; 
       if ($dates["type"][$x]=="PM") {print "backgroundColor: '#000000',\n";} 
       if ($dates["type"][$x]=="AM") {print "backgroundColor: '#D15B47',\n";} 
       if ($dates["type"][$x]=="SM") {print "backgroundColor: '#FFB752',\n";} 
       if ($dates["type"][$x]=="S") {print "backgroundColor: '#87B87F',\n";} 
       if ($dates["type"][$x]=="SS") {print "backgroundColor: '#4F99C6',\n";} 
       if ($dates["type"][$x]=="MJ") {print "backgroundColor: '#A069C3',\n";} 
       if ($dates["type"][$x]=="PR") {print "backgroundColor: '#5A5A5A'\n";} 
       if ($x<(count($dates["id"])-1)) { 
        print " },\n"; 
       } else { 
        print " }\n"; 
       } 
      } 

      print "]\n"; 
      timeFormat: 'h:mm' 
?> 
      , 
      editable: true, 
      droppable: true, // this allows things to be dropped onto the calendar !!! 
      drop: function(date, allDay) { // this function is called when something is dropped 

       // retrieve the dropped element's stored Event Object 
       var originalEventObject = $(this).data('eventObject'); 
       var $extraEventClass = $(this).attr('data-class'); 

       // we need to copy it, so that multiple events don't have a reference to the same object 
       var copiedEventObject = $.extend({}, originalEventObject); 

       // assign it the date that was reported 
       copiedEventObject.start = date; 
       copiedEventObject.allDay = allDay; 
       if($extraEventClass) copiedEventObject['className'] = [$extraEventClass]; 

       // render the event on the calendar 
       // the last `true` argument determines if the event "sticks" (http://arshaw.com/fullcalendar/docs/event_rendering/renderEvent/) 
       $('#calendar').fullCalendar('renderEvent', copiedEventObject, true); 

       // is the "remove after drop" checkbox checked? 
       if ($('#drop-remove').is(':checked')) { 
        // if so, remove the element from the "Draggable Events" list 
        $(this).remove(); 
       } 

      } 
      , 
      selectable: true, 
      selectHelper: true, 
      select: function(start, end, allDay) { 



       calendar.fullCalendar('unselect'); 
      } 
      , 
      eventClick: function(calEvent, jsEvent, view) { 

       //display a modal 
       var modal = 
       '<div class="modal fade">\ 
        <div class="modal-dialog">\ 
        <div class="modal-content">\ 
        <div class="modal-body">\ 
        <?php for ($x=0;$x<count($dates["id"]);$x++) { ?> 
         <button type="button" class="close" data-dismiss="modal" style="margin-top:-10px;">&times;</button>\ 
         <label> <strong>Topic:</strong> <?php echo $dates["type"][$x] ?></label>\ 
         <label> <strong>Selected Start:</strong> <?php echo $dates["start"][0]?></label>\ 
         <label> <strong>Selected End:</strong> <?php echo $dates["end"][0] ?></label>\ 
         <label> <strong>Title:</strong> <?php echo $dates["title"][0] ?></label>\ 
         <label> <strong>Location:</strong> <?php echo $dates["location"][0] ?></label>\ 
         <label> <strong>Address:</strong> <?php echo $dates["adress"][0] ?></label>\ 
         <label> <strong>Phone:</strong> <?php echo $dates["phone"][0] ?></label>\ 
        <?php } ?> 
        </div>\ 
        <div class="modal-footer">\ 
         <button type="button" class="btn btn-sm" data-dismiss="modal"><i class="ace-icon fa fa-times"></i> Close Window</button>\ 
        </div>\ 
        </div>\ 
       </div>\ 
       </div>'; 

       var modal = $(modal).appendTo('body'); 

       modal.modal('show').on('hidden', function(){ 
        modal.remove(); 
       }); 

       console.log(calEvent.id); 
       console.log(jsEvent); 
       console.log(view); 

       // change the border color just for fun 
       //$(this).css('border-color', 'red'); 

      } 

      }); 

     }) 
     </script> 

有人能告訴我我做錯了什麼嗎?我的代碼應該是什麼樣子才能工作?

回答

0

我和這個完全一樣。我所做的是使用fullcalendar內置的clickHandler JavaScript函數來獲取日曆項目的ID。

然後,我使用AJAX調用我的PHP文件從數據庫中提取相關項目詳細信息,然後將它們附加到模式中。

+0

你能告訴我在我的代碼中fullcalender.js事件選項用下面的代碼我需要什麼更改?我不擅長js函數或JAVA,因此我不知道我的代碼應該是什麼樣子。 –

0

您是在正確的道路上,只是將JSON創建需要的是修復請在

events:function(start, end, callback){ 

      $.ajax({ 
       type: "POST", 
       url: SITEROOT+'/controller/mycalendar/events.json.php', 
       data: {action:'getevents',view:$('#calendar').fullCalendar('getView').name,location:$("#location").val()}, 
       success: function(result){ 
        var events = []; 
        if(result){ 
           $.each(result,function(i, item){ 

           events.push({ 
           id:result[i].id, 
           title:result[i].title, 
           start:result[i].start, 
           end:result[i].end, 
           url:result[i].url, 
           color:result[i].color, 
           textColor:result[i].textColor, 
           allDay:result[i].allDay 
          }); 
         }); 
        } 
        callback(events); 
       } 
      }); 
     } 
+0

我看着fullcalendar.js,但找不到上面的代碼?我究竟需要做什麼@Tejas Patil? –

+0

參考你已經發布for循環的代碼,你有它的相同我發佈了唯一的區別是,我用Ajax調用來獲取事件數據,我建議你嘗試相同的。 –

+0

仍然沒有得到的代碼工作,因爲它應該:(!你可以編輯我的代碼與您的代碼,並重新發布,這樣我可以嘗試如果您的代碼工作,並看看我的代碼應該看起來像什麼 –