2010-10-28 102 views
0

我正在使用日曆交互模塊。該日曆顯示保留天數。預訂時間間隔爲7天。我已經通過javascript設置了懸停一天的課程,並在這一天的前3天和後3天添加了課程和自動懸停,以便可視化該課程的7天間隔設置。現在我堅持以下問題。需要幫助,通過jQuery進行某種碰撞檢測

如果我徘徊一天,其中一個prev。 3或下3是已經是保留的一部分,我想在7天的時間間隔的另一端添加差異。一個例子:

  1. 我懸停天12
  2. 則間隔看起來像xxx12xxx
  3. i中的光標移動到13
  4. 的間隔看起來像xxx13xxx
  5. 如果我現在將光標移動到14那麼15,16,17也會被標記,但如果16是保留的起點呢?那麼它看起來像xxx14x
  6. 在任何情況下,我終於需要知道左側和右側外端的id,因爲這些是我必須通過窗體發送的值。如何獲得這些?
  7. 如何使差異(16和17)被預置在左端,因此它看起來像xxxxx14x?

我看到它有一個巨大的代碼塊7個開關的唯一方法。但不知何故,我覺得有一個更簡單的方法。

你們可能會告訴我嗎?

非常感謝提前閱讀!

問候

回答

0

當你將鼠標懸停在了一天,你obviuosly需要做的周圍天了一些分析,以完成任務。下面是我將如何處理這個問題(使用僞代碼):

on day hover{ 
    determine the base starting date. 
    start day = current day.value - 3 
    end day = start day + 7 

    There's several scenerios to deal with: 
     all seven days are available. 
     some days between the start day and the current day are reserved, but enough are availabel after the end day to allow for choosing 7 days 
     some days between the current day and the end day are reserved, but enough are available before the start day to allow for selecting 7 days 
     There some days are reserved between both the start day and current day as well as the curent day and end day (in which case, no selection can be made). 

    So, you need to determine which case you're dealing with and adjust the 7 day span accordingly. 

    set a boolean to handle checking logic. 
    hasSevenDayAvailablility = true 

    test the first instance. 
    iterate over each day and see if any are currently reserved. 
    for(start day to end day as day){ 
     if(day is reserved){ 
      hasSevenDayAvailability = false 
     } 
    } 

    At this point, you know whether or not you even need to modify the seven day window. 
    if(!hasSevenDayAvailability){ 
     Check for no availability first (as this is the easiest to check) Since the only reservation options are in blocks of 7, we only have to check the start day and end day. 
     if(start day is reserved && end day is reserved){ 
      a reservation window is unavailable, so do nothing. 
     } else { 
      if(the start day is reserved){ 
       increment the start day until you reach the current day and test for 7 day availability window 
       for(start day to current day as day){ 
        if(day is available && day + 7 is available){ 
         start day = day 
         end day = start day + 7 
         hasSevenDayAvailability = true 
         break 
        } 
       } 
      } else { 
       decrement the end day until you reach the current day and test for 7 day availability window 
       for(end day to current day step -1 as day){ 
        if(day is available && day - 7 is available){ 
         start day = day - 7 
         end day = day 
         hasSevenDayAvailability = true 
         break 
        } 
       } 
      } 
     } 
    } 

    Finally, if there is a seven day availability window, use the calculated start and end days to set classes as needed. 
    if(hasSevenDayAvailability){ 
     for(start day to end day as day){ 
      day.addClass(highlighted) 
     } 
    } 
} 
+0

您應該格式化您的文章,以便只有實際的代碼前面有四個空格。 – treeface 2010-10-28 17:52:03