2013-02-19 28 views
7

我需要在JQuery UI日期選擇器的某些日期顯示自定義文本,更確切地說,可以爲某些日期呈現特定的價格。 我的想法是使用beforeShowDay在這些日期的標題中附加特定的價格,然後在beforeShow中查看每個td並將標題的文本放入單元格中。例如:向jquery ui datepicker單元添加文本的方法?

var m, d, y, checkDate, specificPrice = '';  
var specificPrices = {"2013-3-8":"300 EUR", "2013-2-26":"263 EUR"} 

$('#my-datepicker').datepicker({ 
    beforeShowDay:  function checkAvailable(date) { 
    m = date.getMonth(); 
    d = date.getDate(); 
    y = date.getFullYear(); 
    checkDate = y + '-' + (m+1) + '-' + d; 
    if(specificPrices[checkDate]){ 
     specificPrice = specificPrices[checkDate]; 
    }else{ 
     specificPrice = ''; 
    } 
    return [true, "", specificPrice]; 
}, 
    beforeShow: function(elem, inst){ 
     $('table.ui-datepicker-calendar tbody td', inst.dpDiv).each(function(){ 
      var calendarPrice = $(this).attr('title'); 
      if(calendarPrice != undefined){ 
       $(this).find('a').append('<span class="calendar-price">' + calendarPrice + '<span>'); 
      } 
     });   
    } 
}); 

,這會使得在第一個日曆價格呈現(月/年改變之前)和僅在線日曆。

我需要價格顯示在非內聯日曆上以及任何月/年的變化。

我嘗試了一些其他變體,並嘗試使用onChangeMonthYear,但迄今沒有成功。

謝謝你的關注,你的想法是受歡迎的。

+0

我已經開發了用於標準和內聯datepickers工作的解決方案,您可能會感興趣,看到我的【答案】(http://stackoverflow.com/a/22822254/2464634)以下。 – 2014-04-03 15:06:51

+0

對不起,自從我問這個問題以來,已經有一年多的時間了。在這一刻,我已經超出了這個問題的範圍。雖然我會盡力找一些空閒時間來審查你的方法,以便接受是一種解決方案。 – yuga 2014-04-12 04:09:36

回答

8

我遇到了同樣的情況,並認爲我會發布我的解決方案的內聯datepicker,但也應該爲popup datepicker工作。

首先,您不能修改由datepicker插件創建的表格單元格的內容。這樣做會在讀取單元格內容以生成所選日期字符串時破壞其核心功能。所以,內容必須使用純CSS添加。

創建日期選擇器

$('#DatePicker').datepicker({ 
    changeMonth: true, 
    changeYear: true, 
    minDate: 0, 
    //The calendar is recreated OnSelect for inline calendar 
    onSelect: function (date, dp) { 
     updateDatePickerCells(); 
    }, 
    onChangeMonthYear: function(month, year, dp) { 
     updateDatePickerCells(); 
    }, 
    beforeShow: function(elem, dp) { //This is for non-inline datepicker 
     updateDatePickerCells(); 
    } 
}); 
updateDatePickerCells(); 

創建插入單元格內容

function updateDatePickerCells(dp) { 
    /* Wait until current callstack is finished so the datepicker 
     is fully rendered before attempting to modify contents */ 
    setTimeout(function() { 
     //Fill this with the data you want to insert (I use and AJAX request). Key is day of month 
     //NOTE* watch out for CSS special characters in the value 
     var cellContents = {1: '20', 15: '60', 28: '100'}; 

     //Select disabled days (span) for proper indexing but apply the rule only to enabled days(a) 
     $('.ui-datepicker td > *').each(function (idx, elem) { 
      var value = cellContents[idx + 1] || 0; 

      /* dynamically create a css rule to add the contents with the :after       
       selector so we don't break the datepicker functionality */ 
      var className = 'datepicker-content-' + value; 

      if(value == 0) 
       addCSSRule('.ui-datepicker td a.' + className + ':after {content: "\\a0";}'); //&nbsp; 
      else 
       addCSSRule('.ui-datepicker td a.' + className + ':after {content: "' + value + '";}'); 

      $(this).addClass(className); 
     }); 
    }, 0); 
} 

添加一個方法來動態創建新的CSS規則,同時跟蹤已經創建者的方法。

var dynamicCSSRules = []; 
function addCSSRule(rule) { 
    if ($.inArray(rule, dynamicCSSRules) == -1) { 
     $('head').append('<style>' + rule + '</style>'); 
     dynamicCSSRules.push(rule); 
    } 
} 

最後,爲新的單元格內容的一些默認的CSS

.ui-datepicker td a:after 
{ 
    content: ""; 
    display: block; 
    text-align: center; 
    color: Blue; 
    font-size: small; 
    font-weight: bold; 
} 

本工程爲基本內容,但如果你想獲得幻想的東西,如「20.95 $」(包含CSS特殊字符) ,您可以使用內容的md5散列來創建className變量。

編輯 JSFiddle從@年代的的jsfiddle修改,以包括獨特的類名的更復雜的內容的MD5哈希值。

+0

根據你的回答,我創建了一個[小提琴](http://jsfiddle.net/yugene/e3uu9),它似乎工作。我接受你的答案,即使我現在沒有可能檢查是否滿足了我最初對此行爲的所有要求。無論如何感謝您分享解決方案:)。 – yuga 2014-04-21 09:04:10

0

根據@ PrestonS的回答和this post,我有一個很好的簡單解決方案,不需要將<style>標籤添加到標題。

我的解決方法更新普雷斯頓的這樣:

修改updateDatePickerCells功能:

function updateDatePickerCells(dp) { 
    /* Wait until current callstack is finished so the datepicker 
     is fully rendered before attempting to modify contents */ 
    setTimeout(function() { 
     //Fill this with the data you want to insert (I use and AJAX request). Key is day of month 
     //NOTE* watch out for CSS special characters in the value 
     var cellContents = {1: '20', 15: '60', 28: '100'}; 

     //Select disabled days (span) for proper indexing but apply the rule only to enabled days(a) 
     $('.ui-datepicker td > *').each(function (idx, elem) { 
      var value = cellContents[idx + 1] || 0; 

      /***** MAGIC! *****/ 

      $(this).attr('data-content', value); 

      // and that's it! (oh, so easy) 
     }); 
    }, 0); 
} 

有了這個解決方案,你不需要addCSSRule功能都沒有。

修改CSS:

/* to target all date cells */ 
.ui-datepicker td > *:after { 
    content: attr(data-content); /***** MAGIC! *****/ 

    /* add your other styles here */ 
} 

/* to target only allowed date cells */ 
.ui-datepicker td > a:after { 
} 

/* to target only disabled date cells */ 
.ui-datepicker td > span:after { 
} 

與日期選擇對象

初始化如果您需要在updateDatePickerCells功能的日期選擇對象,這裏是一個辦法在初始化時得到它。 (上this post基於)

var calendarElem = $('#DatePicker').datepicker({ 
    changeMonth: true, 
    changeYear: true, 
    minDate: 0, 
    //The calendar is recreated OnSelect for inline calendar 
    onSelect: function (date, dp) { 
     updateDatePickerCells(dp); 
    }, 
    onChangeMonthYear: function(month, year, dp) { 
     updateDatePickerCells(dp); 
    }, 
    beforeShow: function(elem, dp) { //This is for non-inline datepicker 
     updateDatePickerCells(dp); 
    } 
}); 

var datepicker = $.datepicker._getInst(calendarElem[0]); 
updateDatePickerCells(datepicker);