Detailed explanation of how to develop calendar components using react
本篇文章给大家带来的内容是关于php中如何得到小程序传来的json数组数据(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
准备工作
提前需要准备好react脚手架开发环境,由于react已经不支持在页面内部通过jsx.transform来转义,我们就自己大了个简易的开发环境
创建一个文件夹,命名为react-canlendar
cd ./react-canlendar
运行
npm init
一路enter我们得到一个package.json的文件
安装几个我们需要的脚手架依赖包
npm install awesome-typescript-loader typescript webpack webpack-cli -D
安装几个我们需要的类库
npm install @types/react react react-dom --save
基础类库安装完毕,开始构建webpack配置
新建一个目录config,config下面新增一个文件,名字叫做webpack.js
var path = require('path') module.exports = { entry: { main: path.resolve(__dirname, '../src/index.tsx') }, output: { filename: '[name].js' }, resolve: { extensions: [".ts", ".tsx", ".js", ".json"] }, module: { rules: [ {test: /\.tsx?$/, use: ['awesome-typescript-loader']} ] } }
还需要创建一个index.html文件,这是我们的入口文件
nbsp;html> <meta> <title>Document</title> <p></p> <script></script>
以上环境只是一个极简单的环境,真实环境要比这个复杂的多。
好了,言归正传,我们还是聚焦到日历组件的开发中来吧
创建一个src文件夹,内部创建一个index.tsx文件。
这个入口文件很简单就是一个挂载
import * as React from 'react' import * as ReactDOM from 'react-dom' ReactDOM.render(( <p> test </p> ), document.getElementById('root'))
ok,打开页面可以看到页面正常显示了test字样。
我们需要创建Calendar组件了。
创建一个components文件夹,内部创建一个Calendar.tsx文件。
import * as React from 'react' export default class Calendar extends React.Component { render() { return (<p> 日历 </p>) } }
在index.tsx中把Calendar.tsx引入,并使用起来。于是index.tsx变成这个样子。
import * as React from 'react' import * as ReactDOM from 'react-dom' import Calendar from './components/Calendar' ReactDOM.render(( <p> <calendar></calendar> </p> ), document.getElementById('root'))
可以看到页面显示了日历字样。
要显示日历,首先需要显示日历这个大框以及内部的一个个小框。实现这种布局最简单的布局就是table了
所以我们首先创建的是这种日历table小框框,以及表头的星期排列。
import * as React from 'react' const WEEK_NAMES = ['日', '一', '二', '三', '四', '五', '六'] const LINES = [1,2,3,4,5,6] export default class Calendar extends React.Component { render() { return (<p> </p>
{week} | }) }
{index} | }) }
可以看到我们使用了一个星期数组作为表头,我们按照惯例是从周日开始的。你也可以从其他星期开始,不过会对下面的日期显示有影响,因为每个月的第一天是周几决定第一天显示在第几个格子里。
那为什么行数要6行呢?因为我们是按照最大行数来确定表格的行数的,如果一个月有31天,而这个月的第一天刚好是周六。就肯定会显示6行了。
为了显示好看,我直接写好了样式放置在index.html中了,这个不重要,不讲解。
nbsp;html> <meta> <title>Document</title> <style> * { margin: 0; padding: 0; } .table { border-collapse:collapse; border-spacing:0; } .table td{ border: 1px solid #ddd; padding: 10px; } .table caption .caption-header{ border-top: 1px solid #ddd; border-right: 1px solid #ddd; border-left: 1px solid #ddd; padding: 10px; display: flex; justify-content: space-between; } .table caption .caption-header .arrow { cursor: pointer; font-family: "宋体"; transition: all 0.3s; } .table caption .caption-header .arrow:hover { opacity:0.7; } </style> <p></p> <script></script>
下面就要开始显示日期了,首先要把当前月份的日期显示出来,我们先在组件的state中定义当前组件的状态
state = { month: 0, year: 0, currentDate: new Date() }
我们定义一个方法获取当前年月,为什么不需要获取日,因为日历都是按月显示的。获取日现在看来对我们没有意义,于是新增一个方法,设置当前组件的年月
setCurrentYearMonth(date) { var month = Calendar.getMonth(date) var year = Calendar.getFullYear(date) this.setState({ month, year }) } static getMonth(date: Date): number{ return date.getMonth() } static getFullYear(date: Date): number{ return date.getFullYear() }
创建两个静态方法获取年月,为什么是静态方法,因为与组件的实例无关,最好放到静态方法上去。
要想绘制一个月还需要知道一个月的天数吧,才好绘制吧
所以我们创建一个数组来表示月份的天数
const MONTH_DAYS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] // 暂定2月份28天吧
组件上创建一个函数,根据月份获取天数,也是静态的
static getCurrentMonthDays(month: number): number { return MONTH_DAYS[month] }
下面还有一个重要的事情,就是获取当前月份第一天是周几,这样子就可以决定把第一天绘制在哪里了。首先要根据年月的第一天获得date,根据这个date获取周几。
static getDateByYearMonth(year: number, month: number, day: number=1): Date { var date = new Date() date.setFullYear(year) date.setMonth(month, day) return date }
这里获得每个月的第一天是周几了。
static getWeeksByFirstDay(year: number, month: number): number { var date = Calendar.getDateByYearMonth(year, month) return date.getDay() }
好了,开始在框子插入日期数字了。因为每个日期都是不一样的,这个二维数组可以先计算好,或者通过函数直接插入到jsx中间。
static getDayText(line: number, weekIndex: number, weekDay: number, monthDays: number): any { var number = line * 7 + weekIndex - weekDay + 1 if ( number monthDays ) { return <span> </span> } return number }
看一下这个函数需要几个参数哈,第一个行数,第二个列数(周几),本月第一天是周几,本月天数。line * 7 + weekIndex
表示当前格子本来是几,减去本月第一天星期数字。为什么+1,因为索引是从0开始的,而天数则是从1开始。那么本月最大天数的
则过滤掉,返回一个空span,只是为了撑开td。其他则直接返回数字。
import * as React from 'react' const WEEK_NAMES = ['日', '一', '二', '三', '四', '五', '六'] const LINES = [1,2,3,4,5,6] const MONTH_DAYS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] export default class Calendar extends React.Component { state = { month: 0, year: 0, currentDate: new Date() } componentWillMount() { this.setCurrentYearMonth(this.state.currentDate) } setCurrentYearMonth(date) { var month = Calendar.getMonth(date) var year = Calendar.getFullYear(date) this.setState({ month, year }) } static getMonth(date: Date): number{ return date.getMonth() } static getFullYear(date: Date): number{ return date.getFullYear() } static getCurrentMonthDays(month: number): number { return MONTH_DAYS[month] } static getWeeksByFirstDay(year: number, month: number): number { var date = Calendar.getDateByYearMonth(year, month) return date.getDay() } static getDayText(line: number, weekIndex: number, weekDay: number, monthDays: number): any { var number = line * 7 + weekIndex - weekDay + 1 if ( number monthDays ) { return <span> </span> } return number } static formatNumber(num: number): string { var _num = num + 1 return _num {this.state.month}
{week} | }) }
{Calendar.getDayText(key, index, weekDay, monthDays)} | }) }
可以看到最终的代码多了一些东西,因为我加了月份的切换。
还记的上文我们把二月份天数写死的18天嘛?要不你们自己改改,判断一下闰年。
相关推荐:
The above is the detailed content of Detailed explanation of how to develop calendar components using react. For more information, please follow other related articles on the PHP Chinese website!

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
