当前位置: 首页 > news >正文

做网站时间软件开发公司哪家强

做网站时间,软件开发公司哪家强,wordpress图创,用discuz怎样做网站react 事件处理在使用React渲染RESTful服务后#xff0c;我们创建了简单的UI#xff0c;用于渲染从RESTful服务获取的员工列表。 作为本文的一部分#xff0c;我们将扩展同一应用程序以支持添加和删除员工操作。 我们将通过添加/删除员工操作来更新react-app后端api#x… react 事件处理 在使用React渲染RESTful服务后我们创建了简单的UI用于渲染从RESTful服务获取的员工列表。 作为本文的一部分我们将扩展同一应用程序以支持添加和删除员工操作。 我们将通过添加/删除员工操作来更新react-app后端api并修改现有的get employee方法以返回员工列表方法如下 步骤1.定义由PostMapping“ / employee / add”标记的addEmployee方法该方法将在类级别的员工列表中添加员工 PostMapping(/employee/add) public ListEmployee addEmployee(final RequestBody Employee employee) {System.out.println(Adding employee with name : employee.getName());if(employee.getName() ! null employee.getName().length() 0)employeeList.add(new Employee(employeeList.size(), employee.getName(), IT));return employeeList; } 步骤2.定义由PostMapping“ / employee / delete”标记的deleteEmployee方法 该方法将从与雇员姓名匹配的类级别雇员列表中删除雇员如下所示 PostMapping(/employee/delete) public ListEmployee deleteEmployee(final RequestBody Employee employee) {System.out.println(Deleting employee with name : employee.getName());final OptionalEmployee optional employeeList.stream().filter(e - e.getName().equalsIgnoreCase(employee.getName())).findAny();if(optional.isPresent()){employeeList.remove(optional.get());}return employeeList; } 最终 ReactAppApplication.java应该看起来像 SpringBootApplication RestController public class ReactAppApplication {final ListEmployee employeeList new ArrayList();public static void main(String[] args) {SpringApplication.run(ReactAppApplication.class, args);}GetMapping(/employee/get)public ListEmployee get() {return employeeList;}PostMapping(/employee/add)public ListEmployee add(final RequestBody Employee employee) {System.out.println(Adding employee with name : employee.getName());if(employee.getName() ! null employee.getName().length() 0)employeeList.add(new Employee(employeeList.size(), employee.getName(), IT));return employeeList;}PostMapping(/employee/delete)public ListEmployee delete(final RequestBody Employee employee) {System.out.println(Deleting employee with name : employee.getName());final OptionalEmployee optional employeeList.stream().filter(e - e.getName().equalsIgnoreCase(employee.getName())).findAny();if(optional.isPresent()){employeeList.remove(optional.get());}return employeeList;} } 步骤3在ReactApp组件中定义addEmployee方法/处理程序该方法以员工名称作为POST调用的负载作为我们刚刚在控制器中定义的addEmployee方法的有效负载如下所示 /Users/ArpitAggarwal/react-app/app/components/react-app.jsx addEmployee(employeeName){let _this this;this.Axios.post(/add, {name: employeeName}).then(function (response) {console.log(response);_this.setState({employees: response.data});}).catch(function (error) {console.log(error);}); } 步骤4在ReactApp组件的构造函数中绑定addEmployee处理程序 constructor(props) {super(props);this.state {employees: []};this.addEmployee this.addEmployee.bind(this);this.Axios axios.create({baseURL: /employee,headers: {content-type: application/json, creds:user}}); } 步骤5渲染子组件– AddEmployee作为ReactApp组件渲染方法的一部分将addEmployee处理程序作为react 道具传递以建立父子通信 render() {return (divAddEmployee addEmployee{this.addEmployee}/EmployeeList employees{this.state.employees}//div) } 步骤6在组件目录中创建add-employee组件如下所示 cd react-app/app/components/ touch add-employee.jsx 并复制以下内容 react-app / app / components / add-employee.jsx import React, { Component, PropTypes } from reactexport default class AddEmployee extends React.Component {render(){return (divinput type text ref input /button onClick {(e) this.handleClick(e)}Add Employee/button/div)}handleClick(e) {const node this.refs.inputconst text node.value.trim()console.log(text);this.props.addEmployee(text)node.value } } 如上所定义的handleClicke中函数调用上添加员工按钮点击这将进一步调用使用的道具 ReactApp定义addEmployee处理程序。 完成所有这些操作后我们的react-app可以执行添加员工操作。 接下来我们将进一步扩展该功能以支持删除员工的操作。 步骤7定义deleteEmployee处理程序并以与addEmployee处理程序相同的方式绑定到ReactApp中 /Users/ArpitAggarwal/react-app/app/components/react-app.jsx constructor(props) {super(props);this.state {employees: []};this.addEmployee this.addEmployee.bind(this);this.deleteEmployee this.deleteEmployee.bind(this);this.Axios axios.create({baseURL: /employee,headers: {content-type: application/json, creds:user}}); }deleteEmployee(employeeName){let _this this;this.Axios.post(/delete, {name: employeeName}).then(function (response) {_this.setState({employees: response.data});console.log(response);}).catch(function (error) {console.log(error);}); } 步骤8将 deleteEmployee处理函数传递给EmployeeList组件该组件将进一步将其传递给它的子容器 render() {return (divAddEmployee addEmployee{this.addEmployee}/EmployeeList employees{this.state.employees} deleteEmployee{this.deleteEmployee}//div)} 最终 ReactApp组件应如下所示 use strict; const React require(react); var axios require(axios);import EmployeeList from ./employee-list.jsx import AddEmployee from ./add-employee.jsxexport default class ReactApp extends React.Component {constructor(props) {super(props);this.state {employees: []};this.addEmployee this.addEmployee.bind(this);this.deleteEmployee this.deleteEmployee.bind(this);this.Axios axios.create({baseURL: /employee,headers: {content-type: application/json, creds:user}});}componentDidMount() {let _this this;this.Axios.get(/get).then(function (response) {console.log(response);_this.setState({employees: response.data});}).catch(function (error) {console.log(error);});}addEmployee(employeeName){let _this this;this.Axios.post(/add, {name: employeeName}).then(function (response) {console.log(response);_this.setState({employees: response.data});}).catch(function (error) {console.log(error);});}deleteEmployee(employeeName){let _this this;this.Axios.post(/delete, {name: employeeName}).then(function (response) {_this.setState({employees: response.data});console.log(response);}).catch(function (error) {console.log(error);});}render() {return (divAddEmployee addEmployee{this.addEmployee}/EmployeeList employees{this.state.employees} deleteEmployee{this.deleteEmployee}//div)} } 步骤9更新EmployeeList组件以将deleteEmployee处理程序传递给它的子组件– Employee方法是将其与render方法中的更改一起导入以具有Delete列 const React require(react); import Employee from ./employee.jsxexport default class EmployeeList extends React.Component{render() {var employees this.props.employees.map((employee, i) Employee key{i} employee{employee} deleteEmployee{() this.props.deleteEmployee(employee.name)}/);return (tabletbodytrthID/ththName/ththDepartment/ththDelete/th/tr{employees}/tbody/table)} } 步骤10更新Employee组件以进行渲染– DeleteEmployee组件通过deleteEmployee处理程序 const React require(react); import DeleteEmployee from ./delete-employee.jsxexport default class Employee extends React.Component{render() {return (trtd{this.props.employee.id}/tdtd{this.props.employee.name}/tdtd{this.props.employee.department}/tdtdDeleteEmployee deleteEmployee{this.props.deleteEmployee}//td/tr)} } 步骤11在组件目录内创建delete-employee组件 cd react-app/app/components/ touch delete-employee.jsx 并复制以下内容 react-app / app / components / delete-employee.jsx import React, { Component, PropTypes } from reactexport default class DeleteEmployee extends React.Component {render(){return (button onClick {(employeeName) this.handleDelete(employeeName)}Delete/button)}handleDelete(employeeName) {this.props.deleteEmployee(employeeName);} } 上面定义的handleDeleteemployeeName函数在Delete按钮单击时调用这将进一步使用props调用ReactApp中定义的deleteEmployee处理程序。 一切就绪后目录结构应如下所示 现在重新运行该应用程序并访问http// localhost8080 一旦您添加了几名员工它应如下图所示。 完整的源代码托管在github上 。 翻译自: https://www.javacodegeeks.com/2017/05/handling-events-react.htmlreact 事件处理
http://www.sadfv.cn/news/299480/

相关文章:

  • 网站进入百度沙盒电子商务网站开发目标
  • 做百度手机网站优化快临河网站建设
  • 手机免费建设网站制作天津品牌网站设计
  • 2017优秀网站设计欣赏自己创建网站怎么做电商
  • 企业网站展示论文深圳入户申请网站官网
  • 网站建设的优点和不足侵入别人的网站怎么做
  • 广州在线网站制作什么浏览器可以看任何网站
  • 河北省老区建设促进会网站销售网站建设推广
  • 安徽省建设工程信息网官网是什么网站wordpress口腔
  • asp mysql做网站如何在电脑登录wordpress
  • 网站建设制作设计开发福建从化区住房和建设局网站
  • 建设一个招聘网站大概多少费用灵璧县建设局网站
  • 营销型网站开发流程包括设计官网页面需要多少钱
  • 建设一个电商网站安徽公司招聘网站
  • 快速搭建网站前端重庆市网站推广
  • 400网站建设价格微信开发者工具官网平台入口
  • zhihe网站建设 淘宝专业机械设计公司
  • 漂亮的手机网站模板WordPress万级数据优化
  • 南通网站建设seo佛山网站建设玲念建站
  • 网站变更备案给公司做网站 图片倾权
  • 网站建设前的功能单页html模板
  • 营销型网站是啥意思尊云服务器
  • asp.net做学校网站首页天津放心站内优化seo
  • 国外网站国内做二维码wordpress 网站改名
  • 做自己的网站流量怎么网站设计公司哪家专业
  • 网站推广公司 wordpress四川建设人才官方网站
  • 做网站的结论桔子seo网
  • 做网站美工赚钱吗怎么查看wordpress版本
  • 国外的室内设计网站作文生成器
  • 微信开放平台与个人网站怎么四川建设网app