简体   繁体   English

使用jQuery添加日期范围选择器并更新内容

[英]Adding a date range picker with jQuery and updating content

I have the following HTML which adds my table headings when you click on the Add Column button. 我有以下HTML,当您单击“ Add Column按钮时,它会添加我的表格标题。 Then when you click Add Row it creates the rows. 然后单击“ Add Row将创建行。

I need to add a date picker and create the button so that the user can pick a date range, then that date range needs to go into the respective travel dates row. 我需要添加日期选择器并创建按钮,以便用户可以选择日期范围,然后该日期范围需要进入相应的旅行日期行。

The user needs to click Add Column , then needs to click Add Date Range and then it should put a date in the cell. 用户需要单击“ Add Column ,然后需要单击“ Add Date Range ,然后它应该在单元格中Add Date Range The Add Date Range needs to have its own column showing in each row. 添加日期范围需要在每行中显示自己的列。 Then when the user picks a range it should show up something like: Fri, 20 Sep - Mon, 7 Oct 2013 然后当用户选择一个范围时,它应该显示如下: Fri, 20 Sep - Mon, 7 Oct 2013

HTML: HTML:

<button id="addcolumn">Add Column</button>
<button id="addrow">Add Row</button>
<button id="adddaterange">Add Date Range</button>

    <table width="100%" border="1" cellpadding="0" cellspacing="0">

    <thead id="theads">
        <tr>
            <th class="th">Travel Dates</th>
            <th class="th">Duration</th>
            <th class="th">Trip Cost</th>
        </tr>
    </thead>

    <tbody id="tbody">

    </tbody>

    </table>

jQuery: jQuery的:

$(document).ready(function () {
    var $cell = $('<td>', {
        'class': 'td',
        'align': 'center',
        'contenteditable': '',
        'text': 'Content'
    });

    var $header = $('<th>', {
        'class': 'th',
        'contenteditable': '',
        'text': 'Heading'
    });

    $('#addcolumn').click(function() {
        $header.clone().appendTo('thead tr');
        $cell.clone().appendTo('tbody tr');
    });

    $('#addrow').click(function(){
        var $row = $('<tr>');

        $('th').each(function() {
            $cell.clone().appendTo($row);
        });

        $row.appendTo('tbody');
    });



});

JSFIDDLE: http://jsfiddle.net/prBZS/35/ JSFIDDLE: http //jsfiddle.net/prBZS/35/

You can use start date and end date in http://www.eyecon.ro/bootstrap-datepicker/ 您可以在http://www.eyecon.ro/bootstrap-datepicker/中使用开始日期和结束日期

Or 要么

can play around in http://jqueryui.com/datepicker/ 可以在http://jqueryui.com/datepicker/玩游戏

for max and min of date range http://jqueryui.com/datepicker/#date-range 最大和最小日期范围http://jqueryui.com/datepicker/#date-range

I made up a fiddle, I illustrate how it works; 我做了一个小提琴,我说明它是如何工作的; I hope I undestand correctly what are you asking. 我希望我没有正确地理解你在问什么。

The project use a (I ca'nt set it hidden...) input field and attach to it a jQuery datepick ; 该项目使用一个(我可以设置它隐藏...)输入字段并附加一个jQuery datepick ; it's not used a standard jQuery UI datepicker because it isn't support the range selection. 它没有使用标准的jQuery UI datepicker,因为它不支持范围选择。

When you add a column it will be added a class canDatePicker to every cell of the column, this class tells that can be "attached" a datepicker to the cell. 当你添加一个列时,它会向列的每个单元格添加一个类canDatePicker ,这个类告诉你可以将一个canDatePicker “附加”到单元格中。 Using a contenteditable for every cell I managed the current focused cell by adding a class focused , so I know the current cell. 对每个单元格使用contenteditable我通过添加一个focused类来管理当前聚焦单元格,所以我知道当前单元格。 When you attach a datepicker to the cell it's added a class hasDatePicker to the cell for the future new clicks on the cell. 当您将一个日期选择器附加到单元格时,它会向该单元格添加一个类hasDatePicker ,以便将来对该单元格进行新的点击。

When you select the range from the datepicker the range is set on the text of the cell. 从日期选择器中选择范围时,将在单元格的文本上设置范围。 If you click again on the cell the datepicker is opened with the cell range previously selected. 如果再次单击单元格,则会打开日期选择器,其中包含先前选定的单元格范围。

Let me know if you need more explaination of the code. 如果您需要更多解释代码,请告诉我。

Code: 码:

$(document).ready(function () {

    $('#datepicker').datepick({
        rangeSelect: true,
        onClose: function () {
            var dates = $('#datepicker').datepick('getDate');
            var value = '';
            for (var i = 0; i < dates.length; i++) {
                value += (i == 0 ? '' : ' - ') + $.datepick.formatDate(dates[i]);
            }
            $('.focused').html(value);
        }
    });

    $(document).on('focus', '.hasDatePicker', function (e) {
        var dates = $(this).text().split(' - ');
        $("#datepicker").datepick('setDate', dates);
        $("#datepicker").datepick('show')
    });

    $(document).on('focus', 'td', function (e) {
        $("td").removeClass('focused');
        $(e.target).addClass('focused');
    });

    var $cell = $('<td>', {
        'class': 'td',
            'align': 'center',
            'contenteditable': '',
            'text': 'Content'
    });

    var $header = $('<th>', {
        'class': 'th',
            'contenteditable': '',
            'text': 'Heading'
    });

    $('#addcolumn').click(function () {
        $header.clone().appendTo('thead tr');
        $cell.clone().appendTo('tbody tr');
        $('table>tbody>tr>td:nth-child(n+4)').addClass('canDatePicker');
    });

    $('#addrow').click(function () {
        var $row = $('<tr>');

        $('th').each(function () {
            $cell.clone().appendTo($row);
        });

        $row.appendTo('tbody');
        $('table>tbody>tr>td:nth-child(n+4)').addClass('canDatePicker');
    });

    $('#adddaterange').click(function () {
        if ($(".focused").length == 0 || !($(".focused").hasClass("canDatePicker"))) return
        $(".focused").addClass("hasDatePicker");
        $("#datepicker").datepick('show')
    });


});

Demo: http://jsfiddle.net/IrvinDominin/WzkBS/ 演示: http//jsfiddle.net/IrvinDominin/WzkBS/

Start the demo, press Add row, Add column, focus the new cell and press Add date range, and try to use the demo. 启动演示,按添加行,添加列,聚焦新单元格并按添加日期范围,然后尝试使用演示。

$(elementId).datepicker({showOn:“button”,buttonImage:“images / icons / calendar.gif”,buttonImageOnly:true,minDate:(minDate)?minDate:“”,);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM