首頁  >  文章  >  web前端  >  react.js 的 批次新增與刪除功能

react.js 的 批次新增與刪除功能

零下一度
零下一度原創
2017-04-17 12:02:162196瀏覽

下面小編就為大家帶來一篇react.js 的 批次新增與刪除功能。小編覺得蠻不錯的,現在就分享給大家,也給大家做個參考。一起跟著小編過來看看吧

最近做的CMS需要用到批量添加圖片的功能:在添加文件的容器盒內,有兩個內容,分別是:添加按鈕與被新增的選擇檔案組件。

結構分析:

被加入的元件,我們稱為:UploadQiNiuFiles(七牛檔案上傳元件),含一個刪除目前元件的刪除按鈕

新增按鈕的事件



#被加入元件存放的容器


做這個效果只需要明白三個方法的用途就OK:

直接綁定要刪除元件的  deleteType(),它是呼叫刪除index數量的方法  removeContent()

##

//删除{qiniu}与{deleteQiNiu}内容,是把页面上的这两个内容一起删除
  deleteType(){
    let index = this.props.index;
    this.props.callbackParent(index);
  }

在新增元件的容器2cf1e8c0f302bbdf5d4025128a4cf4d9< ;/p>中,為新增按鈕所寫的批次新增addContent()  與刪除removeContent()


//批量添加
  addContent(event) {
    if (this.state.number.length >= this.state.maxNum) {
      return;
    }
    console.log("this.state.number:" + this.state.number);
    this.state.number.push(this.state.number[this.state.number.length - 1] + 1);
    let temp = this.state.number;
    this.setState({
      number: temp
    })
  }

  //删除
  removeContent(index) {
    if (this.state.number.length <= 1) {
      return;
    }
    this.state.number.splice(index, 1);
    this.setState({
      number: this.state.number
    })
  }

程式碼分析:

新增元件存放的容器2cf1e8c0f302bbdf5d4025128a4cf4d9

<p className="pBorder">
   {addToBtn} //添加按钮
   {items}   //被添加的组件
</p>


.pBorder {
  position: relative;
  width: 100%;
  height: auto;
  margin-top: 5%;
  border: 1px solid #e3e3e3;
  padding: 30px 10px;
  margin-bottom: 5%;

  -moz-position: relative;
  -moz-width: 100%;
  -moz-height: auto;
  -moz-border: 1px solid #e3e3e3;
  -moz-padding: 30px 10px;
  -moz-margin-bottom: 5%;
  -webkit-position: relative;
  -webkit-width: 100%;
  -webkit-height: auto;
  -webkit-border: 1px solid #e3e3e3;
  -webkit-padding: 30px 10px;
  -webkit-margin-bottom: 5%;
}

被加入的元件:UploadQiNiuFiles   與刪除元件的方法  deleteType()

 

/**
 * Created by wf on 2016/5/16.
 */
import React,{Component} from &#39;react&#39;;
import {render} from &#39;react-dom&#39;;
import ReactBootstrap , {Input,Button,ButtonToolbar} from &#39;react-bootstrap&#39;;
import style from &#39;../../../../css/meeting_data.css&#39;;

//七牛上传公共组件
import QiniuUpload from &#39;qiniu_uploader&#39;;

export default class UploadQiNiuFiles extends Component {
  constructor(props){
    super(props);
  }

  //获取qiniukey
  getQiniuKey(qiniuKey){
    this.props.setQiniuKey(qiniuKey);
  }

  //获取qiniutoken
  getQiniuUptoken() {
    this.props.acquireToken();
  };

  //删除{qiniu}与{deleteQiNiu}内容,是把页面上的这两个内容一起删除,直接绑定要删除的组件
  //这个方法调用的是removeContent(),在下面有介绍
  deleteType(){
    let index = this.props.index;
    this.props.callbackParent(index);
  }

  render(){

    const qiniu = (
      <p className="col-md-8 qiNiuBtn">
        <QiniuUpload containerId="containerId" pickfilesId="pickfilesId" qiniuToken={this.props.meetingState.token} callback={this.getQiniuKey.bind(this)} getQiniuUptoken={this.getQiniuUptoken.bind(this)} />
      </p>
    );

    const deleteQiNiu = (
      <p className="col-md-4">
        <Button bsStyle="danger" className="deleteQiniu" onClick={this.deleteType.bind(this)}>删除</Button>
      </p>

    );

    return(
      <p>
        <p className="uploadBox">
          {qiniu}
          {deleteQiNiu}
        </p>
      </p>
    );
  }
}
 七牛上傳元件,巳作介紹,在製作這個元件時,需要用到action的方法與reducers中的state,請參考這個連結。因為橙色字體中的參數的獲取是需要用到action中的方法

在p為pBorder的容器內操作添加事件

##首先要加載,七牛上傳元件:UploadQiNiuFiles,它的載入路徑為webpack中的方法:


#

/**常用组件路径简写为:
  *
  * 例:config: path.join(__dirname,"./build/config.js")
  * config 变量名
  * path.join(__dirname,"./build/config.js") config的路径
  *
  * 使用方法: import {变量} from &#39;config&#39;
  * //七牛上传公共组件
   import QiniuUpload from &#39;qiniu_uploader&#39;;
  * **/
 resolve: {
  alias: {
   qiniu_uploader: path.join(__dirname,"./public_component/qiniu_upload/QiniuUpload.js"),
   storage: path.join(__dirname,"./utils/Storage.js"),
   config: path.join(__dirname,"./build/config.js")
  }
 }

##

import React,{Component} from &#39;react&#39;;
import {render} from &#39;react-dom&#39;;
import ReactBootstrap , {Input,Button,ButtonToolbar} from &#39;react-bootstrap&#39;;
import { Link } from &#39;react-router&#39;;
//
import UploadQiNiuFiles from &#39;./UploadQiNiuFiles.js&#39;;

批次上傳檔案的元件名稱,我定義為:

UploadFileToFolde   


 


預設參數為:


constructor(props){
    super(props);
    this.state = {number: [1], maxNum: 10} //最大数据为10条,默认显示1条
  }


/*获取上个页面传过来的值 let local = this.props.location;
   如果从(row,query)中跳转过来的页面,从query中传值过来要这么写:let query = local.query;
   如果这个页面是包含在某个大的页面下的,要把query与对应的ID传过去
   */
  componentDidMount(){
    let local = this.props.location;
    let query = local.query;
    this.props.setActivityId(query.activityId);
  }

資料渲染完成之後,需要執行componentDidUpdate(),這是state中所有的資料:

this.props.meetingState.addUploadFolderToFileList; 判斷這裡面的資料是否為空或是undefined。如果這個state有值且新增成功,則下次到這個頁面時清空所有的資料並且點擊儲存按鈕時返回到原來的頁面。 clearInvitation() 的方法是清空所有的業務數據,它的方法寫在action中,data是業務數據,根據實際情況寫:


/* 清空*/

export const CLEAR_INVITATION = &#39;CLEAR_INVITATION&#39;;
 export function clearInvitation(){
  return {
    type: CLEAR_INVITATION,
    data:{
      addInvitationResponse:{},
      Invitations:[],
      deleteInvitationsResponse:{},
      invitationName:&#39;&#39;,
      folderName: &#39;&#39;
    }
  }
}

##### ##
componentDidUpdate(){
    let addFileToFolderList = this.props.meetingState.addUploadFolderToFileList;
    if (typeof(addFileToFolderList) != &#39;undefined&#39;) {
      let status = addFileToFolderList.status;
      if (200 == status) {
        //如果新增成功,则下次添加前清空所有
        this.props.clearInvitation();
        //点击保存按钮,返回原来的页面
        this.props.history.goBack();
      }
    }
  }
#########
//批量添加,直接拿来使用
  addContent(event) {
    if (this.state.number.length >= this.state.maxNum) {
      return;
    }
    console.log("this.state.number:" + this.state.number);
    this.state.number.push(this.state.number[this.state.number.length - 1] + 1);
    let temp = this.state.number;
    this.setState({
      number: temp
    })
  }
#########
//删除,直接拿来使用
  removeContent(index) {
    if (this.state.number.length <= 1) {
      return;
    }
    this.state.number.splice(index, 1);
    this.setState({
      number: this.state.number
    })
  }
### 七牛上傳元件中有個  deleteType() 的刪除方法,它調的就是  removeContent() 方法,缺一不可,需要注意,我把這個deleteType()方法程式碼也放在這裡: #############
//绑定被删除的组件,直接拿来使用
  deleteType(){
    let index = this.props.index;
    this.props.callbackParent(index); //调用removeContent()方法
  }
########
render(){
     //将要添加的组件定义为变量为一个数组 items
    let items = [];
     //从添加的组件数量中遍历,
    for(let i = 0; i < this.state.number.length; i++){
    //给这个items推送组件
      items.push(<UploadQiNiuFiles index={i}
                    callbackParent={this.removeContent.bind(this)}
                    key={this.state.number[i]} {...this.props} />)
    }

    
    const addToBtn = (
      <Button bsStyle="primary" onClick={this.addContent.bind(this)}>添加</Button>
    );return (
      <p>
        <p>
          <p className="pTitle">添加文件</p>
          <p className="pBorder">
            {addToBtn}
            {items}
          </p>
        </p></p>
    );
  }

以上是react.js 的 批次新增與刪除功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn