reactjs - 在反应中保存反应可拖动对象的位置
问题描述
我正在使用 react-draggable 拖放元素。拖动后如何保存元素位置。
<div className={classes.section}>
<h5>Create Invoice</h5>
{isEditing ? <Button variant="contained" color="primary">Edit</Button> : <Button variant="contained" color="primary">Update</Button>} </label>
<div id="target" onDrop={this.drop} onDragOver={this.allowDrop}>
<Paper className={classes.paper} >
<form>
<Draggable axis="both" bounds="div" disabled={isEditing}>
<CustomInput select variant="outlined" label="To" helperText="Please select your company"/></Draggable>
<Draggable axis="both" bounds="div" disabled={isEditing}><CustomInput name="invoicenumber" label="Invoice Number"/></Draggable>
<Draggable axis="both" bounds="div" disabled={isEditing}><CustomInput id="Date" label="Issue Date" defaultValue="2019-01-01" type="Date" variant="outlined" InputLabelProps={{shrink:true}}/></Draggable>
<TextField id="Date" defaultValue="2019-01-01" type="Date" variant="outlined" InputLabelProps={{shrink:true}}/>
<h5>Item Id</h5> */}
<Draggable axis="both" bounds='div' disabled={isEditing}><CustomInput type="textarea" variant="outlined" label="Description"/></Draggable>
<Draggable axis="both" bounds='Div' disabled={isEditing}>
<CustomInput type="file" variant="outlined" helperText="Attachments" style={{alignContent:"right"}}/>
</Draggable>
</form>
</Paper>
</div>
解决方案
这是获取值的函数。获得值后,您将根据需要将其设置为数据库或 localStorage。
const eventLogger = (e, data) => {
localStorage.setItem('defaultPosition', { valueX: data.x, valueY: data.y });
};
该函数将在 Dragable 中由 onStop 事件调用。
<Draggable defaultPosition={{ x: 0, y: 0 }} onStop={eventLogger}>
<h2 style={{ cursor: 'grab' }}>
dragable
</h2>
</Draggable>
推荐阅读
- mysql - 如何在不使用 phpmyadmin 的情况下在 XAMPP 中备份 Mysql 数据库
- javascript - 使用 javascript 将动态字符串转换为 JSON
- angular - 使用 ionic4-5 项目进行推送通知的最佳方法是什么?
- node.js - Google Api,Node.js - invalid_grant 格式错误的身份验证代码
- php - 未找到 PHP PDO 驱动程序
- webpack - 无法使用 --bundle 设置全局自定义“cssFile”。(没有 --bundle 也可以)
- html5-canvas - 如何从 websocket onmessageevent 以更快的速度绘制图像画布
- javascript - 需要帮助了解为什么我在计算虚数时得到非数字输出
- html - 在 td 内部,如何将“文本溢出:省略号”应用于某些元素,但不是全部?
- mysql - 在 PostgreSQL 中使用 SQL 平衡值