分享

javascript-通过选择datepickers创建datatable标头

 印度阿三17 2019-11-20

我试图创建带有动态列的dataTable以便从& to选择日期选择器.

列应在日期和toDate之间包含fromDate

JS代码

$("#from").datepicker();
$("#to").datepicker(); 

var strTopHead='<th rowspan="2">Name</th><th rowspan="2">Code</th><th rowspan="2">code</th>';

$('#getBetween').on('click', function () {

    $('#trTopHead').empty();
     $('#trBottomHead').empty();
    var start = $("#from").datepicker("getDate"),
        end = $("#to").datepicker("getDate"),
        currentDate = new Date(start),
        between = []
    ;
    if(start!=null&&end!=null){
    while (currentDate <= end) {
        between.push(new Date(currentDate).toJSON().substring(0,10));
        currentDate.setDate(currentDate.getDate()   1);
    }

  //  $('#results').html(between.join('<br> '));
    for(var i=0;i<between.length;i  ){
        strTopHead=strTopHead '<th colspan="3">' between[i] '</th>';

    $('#trBottomHead').append('<th>C</th><th>D</th>               <th>E</th>');
        $('table>tbody').append('<tr><td>Tiger Nixon</td>                <td>System Architect</td>                <td>$320,800</td>                <td>Edinburgh</td>                <td>5421</td>               <td>t.nixon@datatables.net</td></tr>');
    }
            $('#trTopHead').append(strTopHead);

        $('#example').dataTable({
        "scrollY":        "200px",
        "scrollCollapse": true
        });
    }
});

这里仅将单个日期显示为列

I tried like this

解决方法:

我已经对您的代码做了一些修改

var date= new Date();//changes
$("#from").datepicker();
$("#to").datepicker();

$("#from").val($.datepicker.formatDate("mm/dd/yy",date));//changes
$("#to").val($.datepicker.formatDate("mm/dd/yy",date));//changes


$('#getBetween').on('click', function () {
    var strTopHead='';
    $('#trTopHead').empty();
     $('#trBottomHead').empty();
   strTopHead=strTopHead  '<th rowspan="2">Name</th><th rowspan="2">Code</th><th rowspan="2">code</th>';
    var start = $("#from").datepicker("getDate"),
        end = $("#to").datepicker("getDate"),
        currentDate = new Date(start),
        between = []
    ;
    if(start!=null&&end!=null){
    while (currentDate <= end) {
        currentDate.setDate(currentDate.getDate()   1);//changes
        between.push(new Date(currentDate).toJSON().substring(0,10));//changes

    }
    var strTopHeadDates='';
  //  $('#results').html(between.join('<br> '));
    for(var i=0;i<between.length;i  ){
        strTopHeadDates=strTopHeadDates '<th colspan="3">' between[i] '</th>';

    $('#trBottomHead').append('<th>C</th><th>D</th>               <th>E</th>');
        $('table>tbody').append('<tr><td>Tiger Nixon</td>                <td>System Architect</td>                <td>$320,800</td>                <td>Edinburgh</td>                <td>5421</td>               <td>t.nixon@datatables.net</td></tr>');
    }
        strTopHead=strTopHead strTopHeadDates;
            $('#trTopHead').append(strTopHead);

     $('#example').dataTable();
        console.log($("#from").val());
    }
});

try this code

来源:https://www./content-1-569001.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约