- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试构建日历,并有可能为当天添加注释。我的组件结构如下: App 组件是渲染主要 Build 组件的公共(public)组件。 Build 组件使用 CellBuild 组件来构建日历的日期和一个 DayEventBuilder,它将在单击单元格时显示。
我尝试通过单击更改 CellBuild 组件中生成器的“天”状态,然后更改 DayEventBuilder 的状态以显示实际日期。我在更改 DayEventBuilder 的状态时遇到问题,因为它不是由 Builder 的组件获取状态。
CellBuild 中的“日”状态只有在点击两次后才会改变。 DayEventBuilder 组件不从 Builder 获取任何状态。
应用组件
import 'bootstrap/dist/css/bootstrap.css';
import React from 'react';
import './App.css';
import Builder from './Components/Builder.js';
let helpDate = new Date();
class App extends React.Component {
render() {
return (
<div className="App">
<Builder helpDate={helpDate}/>
</div>
);
}
}
export default App;
构建器组件
import React from 'react';
import 'bootstrap/dist/css/bootstrap.css';
import CellBuild from './CellBuild.js';
import DayEventBuilder from './DayEventBuilder.js';
import $ from 'jquery';
let currentDate= new Date();
let months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
//let month = currentDate.getMonth(); //current month
//let year = currentDate.getFullYear(); //current year
//Builder calendar body
class Builder extends React.Component {
constructor(props) {
super(props);
this.state = {
dataState: this.props.helpDate, //set date to build and display
};
}
//help function to control date parameters in cell which is clicked
clickCell=x=>{
this.setState({day:x});
};
createTable =(data)=>{
let helpDate = new Date(data.getFullYear(), data.getMonth(), data.getDate()); //help date for drawing
let helpOther = new Date(data.getFullYear(), data.getMonth(), data.getDate()); //help date for build previous month days
helpDate.setDate(1);
helpOther.setDate(1);
let table=[]; //create table container
let rows=[]; //create rows container
//outer loop for rows creating (filling rows container)
for(let i=0;i<6;i++){
let cells=[]; //create empty cells container
//inner loop for cells creating in row (filling cells container)
for (let j=0;j<7;j++){
//loop for draw previous month days and padding current 1st days relative days of week
if(i===0&&j<helpDate.getDay()-1){
helpOther.setDate(-helpDate.getDay()+2+j);
cells.push(<CellBuild date={helpOther.getDate()} month={helpOther.getMonth()} isNowDate="numbers otherMonth"/>);
}
//continue drawing calendar
else{
//if current month
if(helpDate.getMonth()===data.getMonth()){
//checking for today
if(helpDate.getDate()===currentDate.getDate()&&helpDate.getMonth()===currentDate.getMonth()&&helpDate.getFullYear()===currentDate.getFullYear()){
cells.push(<CellBuild clickCell={this.clickCell} date={helpDate.getDate()} month={helpDate.getMonth()} isNowDate="numbers nowDate"/>); //join cell to cells container
}
else{
cells.push(<CellBuild clickCell={this.clickCell} date={helpDate.getDate()} month={helpDate.getMonth()} isNowDate="numbers" />); //join cell to cells container
}
}
//next month days
else{
cells.push(<CellBuild clickCell={this.clickCell} date={helpDate.getDate()} month={helpDate.getMonth()} isNowDate="numbers otherMonth" />); //join cell to cells container
}
helpDate.setDate(helpDate.getDate()+1);
}
}
rows.push(<tr>{cells}</tr>); //join filled cells container to rows container (join a row)
}
table.push(<table className="main col-lg-12 col-md-12 col-sm-12 col-xs-12"><thead><tr><th colSpan={2}><button className="decrease" onClick=
{() =>
{
data.setMonth(data.getMonth()-1);
this.setState({dataState:data});
}}>←</button><div className="head">{months[data.getMonth()]}</div><div className="head">{data.getFullYear()}</div><button className="increase" onClick=
{() =>
{
data.setMonth(data.getMonth()+1);
this.setState({dataState:data});
}}>→</button></th></tr></thead><tbody>{rows}</tbody></table>); //join filled rows container to table and make header
return table;
};
render() {
return (
<div className="container">
<div className="calendar">{this.createTable(this.state.dataState)}</div>
<DayEventBuilder day={this.state.day}/>
</div>
)
}
}
export default Builder;
细胞构建
import React from 'react';
import 'bootstrap/dist/css/bootstrap.css';
import $ from 'jquery';
//holidays list
let holidays = [
{id:0,
name:"New year",
date:'01.01'},
{id:1,
name:"Christmas",
date:'01.07'},
{id:2,
name:"Men's day",
date:'02.23'},
{id:3,
name:"Women's day",
date:'03.08'},
{id:4,
name:"Labor day",
date:'05.01'},
{id:5,
name:"Victory Day",
date:'05.09'},
{id:6,
name:"Independence day",
date:'07.03'},
{id:7,
name:"November revolution day",
date:'11.07'}];
class CellBuild extends React.Component {
constructor() {
super();
this.state = {
holidaysState: holidays,
};
}
render(){
//do default cell value if not holiday
let cell=<td tabIndex="0" onClick={() => {
console.log(this.props.date);
this.setState({day:this.props.date}); //set day in state to render actual DayEventBuilderComponent
console.log(this.state.day);
//this.setState({month:this.props.month}); //set day in state to render actual DayEventBuilderComponent
this.props.clickCell(this.state.day);
$(function () {
$('table.main').css('opacity','.5');
$('table.dayEvents').css('display','table');
})
}
}><div tabIndex="0" className={this.props.isNowDate}><p>{this.props.date}</p></div></td>;
//check for holiday day
this.state.holidaysState.map(function(holiday){
//create temporary date object from date parameter of holiday
let tempDate = new Date(holiday.date);
//if current day is holiday change a cell value for this day in calendar
if(tempDate.getMonth()===this.props.month&&tempDate.getDate()===this.props.date ){
cell=<td tabIndex="0" onClick={() => {
$(function () {
$('table.main').css('opacity','.5');
$('table.dayEvents').css('display','table');
});
}
} className="holiday"><div tabIndex="0" className={this.props.isNowDate}><p>{this.props.date}</p></div><p className="holiday">{holiday.name}</p></td>;
}
},this); //give CellBuilder as the context of map-function
return cell;
}
}
export default CellBuild;
DayEventBuilder
import React from 'react';
import 'bootstrap/dist/css/bootstrap.css';
import $ from 'jquery';
let events = [
{id:0,
date:"2019.01.20",
name:"Event 1 for this day",
time1:"15:00",
time2:"19:00"
},
];
class DayEventBuilder extends React.Component {
constructor(props) {
super(props);
this.state = {
day:this.props.day, //take actual day and month of the clicked cell from CellBuild
month:this.props.month,
};
}
render(){
let table=[]; //create table container
let rows=[]; //create rows container
let skip=0; //skip <td> adding if needed
//build table
for (let i=0;i<25;i++){
let cells=[]; //create empty cells container
cells.push(<td>{i+':00'}</td>);
if((i+':00')===events[0].time1){
let rowspan = events[0].time2.substring(0, 2)-events[0].time1.substring(0, 2); //calculate how long will the event be
cells.push(<td rowSpan={rowspan} className="setEvent">{events[0].name}</td>);
skip=rowspan; //set skip counter
}
if(skip<=0){ //if we finished skip <td> adding while event was
cells.push(<td onClick={() => {
$(function () {
$('table.main').css('opacity','.3');
$('table.dayEvents').css('opacity','.5');
$('.form').css('display', 'block');
})
}}/>);
}
--skip;
rows.push(<tr>{cells}</tr>);
}
//create table and create exit button from events list
//console.log('DayEventBuilder state.day ='+this.state.day);
table.push(<table className="dayEvents col-lg-6 col-md-8 col-sm-10 col-xs-10"><thead><tr><th>{this.state.day}</th><th><button onClick={() => {
$(function () {
$('table.main').css('opacity','1');
$('table.dayEvents').css('display','none');
})
}
}>×</button></th></tr></thead><tbody>{rows}</tbody></table>);
return (<div>
<div>{table}</div>
<div className="form col-lg-6 col-md-6 col-sm-8 col-xs-8">
<button onClick={() => {
$(function () {
$('table').css('display', 'table');
$('.form').css('display', 'none');
$('table.dayEvents').css('opacity','1');
$('table.main').css('opacity','.5');
})
}}>←</button>
<form>
<fieldset>
<select className="form-control">
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
<option value="five">Five</option>
</select>
<legend>Add the event</legend>
<p>Description <input name="login"/></p>
<p><input type="submit" value="Add"/></p>
</fieldset>
</form>
</div>
</div>);
}
}
export default DayEventBuilder;
最佳答案
这可能是 CellBuild
单击处理程序中的问题。
此代码无法按预期工作,因为 setState是异步的。
() => {
this.setState({day:this.props.date});
this.props.clickCell(this.state.day);
}
尝试改成这样
() => {
this.setState({day:this.props.date});
this.props.clickCell(this.props.date);
}
此外,将 jquery
与 react
一起使用也是一种不好的做法。
关于javascript - 组件之间状态变化的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54179595/
我有一个 html 格式的表单: 我需要得到 JavaScript在value input 字段执行,但只能通过表单的 submit .原因是页面是一个模板所以我不控制它(不能有
我管理的论坛是托管软件,因此我无法访问源代码,我只能向页面添加 JavaScript 来实现我需要完成的任务。 我正在尝试用超链接替换所有页面上某些文本关键字的第一个实例。我还根据国家/地区代码对这些
我正在使用 JS 打开新页面并将 HTML 代码写入其中,但是当我尝试使用 document.write() 在新页面中编写 JS 时功能不起作用。显然,一旦看到 ,主 JS 就会关闭。用于即将打开的
提问不是为了解决问题,提问是为了更好地理解系统 专家!我知道每当你将 javascript 代码输入 javascript 引擎时,它会立即由 javascript 引擎执行。由于没有看过Engi
我在一个文件夹中有两个 javascript 文件。我想将一个变量的 javascript 文件传递到另一个。我应该使用什么程序? 最佳答案 window.postMessage用于跨文档消息。使
我有一个练习,我需要输入两个输入并检查它们是否都等于一个。 如果是 console.log 正则 console.log false 我试过这样的事情: function isPositive(fir
我正在做一个Web应用程序,计划允许其他网站(客户端)在其页面上嵌入以下javascript: 我的网络应用程序位于 http://example.org 。 我不能假设客户端网站的页面有 JQue
目前我正在使用三个外部 JS 文件。 我喜欢将所有三个 JS 文件合而为一。 尽一切可能。我创建 aio.js 并在 aio.js 中 src="https://code.jquery.com/
我有例如像这样的数组: var myArray = []; var item1 = { start: '08:00', end: '09:30' } var item2 = {
所以我正在制作一个 Chrome 扩展,它使用我制作的一些 TamperMonkey 脚本。我想要一个“主”javascript 文件,您可以在其中包含并执行其他脚本。我很擅长使用以下行将其他 jav
我有 A、B html 和 A、B javascript 文件。 并且,如何将 A JavaScript 中使用的全局变量直接移动到 B JavaScript 中? 示例 JavaScript) va
我需要将以下整个代码放入名为 activate.js 的 JavaScript 中。你能告诉我怎么做吗? var int = new int({ seconds: 30, mark
我已经为我的 .net Web 应用程序创建了母版页 EXAMPLE1.Master。他们的 I 将值存储在 JavaScript 变量中。我想在另一个 JS 文件中检索该变量。 示例1.大师:-
是否有任何库可以用来转换这样的代码: function () { var a = 1; } 像这样的代码: function () { var a = 1; } 在我的浏览器中。因为我在 Gi
我收到语法缺失 ) 错误 $(document).ready(function changeText() { var p = document.getElementById('bidp
我正在制作进度条。它有一个标签。我想调整某个脚本完成的标签。在找到可能的解决方案的一些答案后,我想出了以下脚本。第一个启动并按预期工作。然而,第二个却没有。它出什么问题了?代码如下: HTML:
这里有一个很简单的问题,我简单的头脑无法回答:为什么我在外部库中加载时,下面的匿名和onload函数没有运行?我错过了一些非常非常基本的东西。 Library.js 只有一行:console.log(
我知道 javascript 是一种客户端语言,但如果实际代码中嵌入的 javascript 代码以某种方式与在控制台上运行的代码不同,我会尝试找到答案。让我用一个例子来解释它: 我想创建一个像 Mi
我如何将这个内联 javascript 更改为 Unobtrusive JavaScript? 谢谢! 感谢您的回答,但它不起作用。我的代码是: PHP js文件 document.getElem
我正在寻找将简单的 JavaScript 对象“转储”到动态生成的 JavaScript 源代码中的最优雅的方法。 目的:假设我们有 node.js 服务器生成 HTML。我们在服务器端有一个对象x。
我是一名优秀的程序员,十分优秀!