简体   繁体   English

如何自动生成动态moment.js?

[英]How to automatically generate dynamic moment.js?

The idea would be to make one page a monthly table and each line would have week numbers and edit selection.这个想法是将一页作为月表,每一行都有周数和编辑选择。 For this I need moment.js and javascript to generate tables.为此,我需要 moment.js 和 javascript 来生成表格。 Define the exact year and generate one table per month, week numbers, and the first day of the week.定义确切的年份并每月生成一个表、周数和一周的第一天。

I appreciate if you can help me with this.如果你能帮我解决这个问题,我很感激。

Here we go jsfiddle and example.在这里,我们去 jsfiddle 和示例。

Example:例子:

 .table-bordered { border: 1px solid #dee2e6; } .table-bordered thead td, .table-bordered thead th { border-bottom-width: 2px; } .table thead th { vertical-align: bottom; border-bottom: 2px solid #dee2e6; } .table-bordered td, .table-bordered th { border: 1px solid #dee2e6; } table.table thead th { border-top: none; } table.table td, table.table th { padding-top: 1.1rem; padding-bottom: 1rem; } .table td, .table th { padding: .75rem; vertical-align: top; border-top: 1px solid #dee2e6; } table td { font-size: .9rem; font-weight: 300; }
 Generate 2019 (target) year months, week number and first day automatically with moment.js. I'm gonna use php to make tables dynamically.</p> <table id="tablePreview" class="table table-hover table-bordered"> <!--Table head--> <thead> <tr> <th>January</th> <th>Week first day</th> <th>Last Name</th> <th>Username</th> <th>Edit</th> </tr> </thead> <!--Table head--> <!--Table body--> <tbody> <tr> <th scope="row">Week number 1</th> <td>2019-01-01</td> <td>Otto</td> <td>@mdo</td> <td><a href="/edit-link.php">Edit</a></td> </tr> <tr> <th scope="row">Week number 2</th> <td>2019-01-01</td> <td>Thornton</td> <td>-</td> <td><a href="/edit-link.php">Edit</a></td> </tr> <tr> <th scope="row">Week number 3</th> <td>2019-01-01</td> <td>the Bird</td> <td>@twitter</td> <td><a href="/edit-link.php">Edit</a></td> </tr> </tbody> </table> <br><br> <table id="tablePreview" class="table table-hover table-bordered"> <!--Table head--> <thead> <tr> <th>February</th> <th>Week first day</th> <th>Last Name</th> <th>Username</th> <th>Edit</th> </tr> </thead> <!--Table head--> <!--Table body--> <tbody> <tr> <th scope="row">Week number 5</th> <td>2019-02-01</td> <td>Otto</td> <td>@mdo</td> <td><a href="/edit-link.php">Edit</a></td> </tr> <tr> <th scope="row">Week number 6</th> <td>2019-02-01</td> <td>Thornton</td> <td>-</td> <td><a href="/edit-link.php">Edit</a></td> </tr> <tr> <th scope="row">Week number 7</th> <td>2019-02-01</td> <td>the Bird</td> <td>@twitter</td> <td><a href="/edit-link.php">Edit</a></td> </tr> </tbody> </table> <p> etc... </p> </p>

With moment.js this is all I have done..使用 moment.js,这就是我所做的一切。

var months = [];

for( var i = 0; i < 12; i++ ){
months.push( new Date(0,i).toLocaleString({},{month:'long'}) ); 
}

console.log(months);

I've quickly scribbled something together for you.我很快为你写了一些东西。 This should be enough to get you going:这应该足以让你开始:

    var thisYear = 2018;
    var start = new Date("1/1/" + thisYear);
    var defaultStart = moment(start.valueOf());
    var weekNumber = 1;
    this.months = [];
    for (var i = 0; i < 12; i++) {
        var weeks = [];

        var currentMonth = defaultStart.month();

        var monthLimit = i + 1;

        if (defaultStart.month() == 11) {
            monthLimit = 0;
        }

        while (defaultStart.month() != monthLimit) {
            weeks.push(
                {
                    weekNumber: weekNumber,
                    firstDayOfWeek: defaultStart.format("MMM Do YYYY")
                }
            )
            weekNumber++;

            defaultStart.add(7, 'days')
        }
        this.months.push(
            {
                weeks: weeks,
                month: moment().month(currentMonth).format("MMMM")
            });
    }

This will create an array (the months array) for you of 12 objects;这将为您创建一个包含 12 个对象的数组( months数组); this object will contain the name of the month and an array of week objects, which consists of the week number and the first date of the week.这个对象将包含月份的名称和一个周对象数组,它由周数和一周的第一个日期组成。

Take a look at this SlackBlitz example, where the table is displayed using the data generated in the code above using the KnockOutJS library.看看这个SlackBlitz 示例,其中使用KnockOutJS库在上面的代码中生成的数据显示表格。

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

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