Element-React Transfer 穿梭框

2020-10-16 10:59 更新

基礎(chǔ)用法

Transfer 的數(shù)據(jù)通過 data 屬性傳入。數(shù)據(jù)需要是一個(gè)對(duì)象數(shù)組,每個(gè)對(duì)象有以下屬性:key 為數(shù)據(jù)的唯一性標(biāo)識(shí),label 為顯示文本,disabled 表示該項(xiàng)數(shù)據(jù)是否禁止轉(zhuǎn)移。目標(biāo)列表中的數(shù)據(jù)項(xiàng)會(huì)同步到綁定至 value 的變量,值為數(shù)據(jù)項(xiàng)的 key 所組成的數(shù)組。當(dāng)然,如果希望在初始狀態(tài)時(shí)目標(biāo)列表不為空,可以像本例一樣為 value 綁定的變量賦予一個(gè)初始值。

constructor(props) {
  super(props);
  this.state = {
    value: [1, 4]
  }
  this._handleChange = this.handleChange.bind(this);
}


get data() {
  const data = [];
  for (let i = 1; i <= 15; i++) {
    data.push({
      key: i,
      label: `備選項(xiàng) ${ i }`,
      disabled: i % 4 === 0
    });
  }
  return data;
}


handleChange(value) {
  this.setState({ value })
}


render() {
  const { value } = this.state;
  return <Transfer value={value} data={this.data} onChange={this._handleChange}></Transfer>
}

可搜索

在數(shù)據(jù)很多的情況下,可以對(duì)數(shù)據(jù)進(jìn)行搜索和過濾。

設(shè)置 filterabletrue 即可開啟搜索模式。默認(rèn)情況下,若數(shù)據(jù)項(xiàng)的 label 屬性包含搜索關(guān)鍵字,則會(huì)在搜索結(jié)果中顯示。你也可以使用 filterMethod 定義自己的搜索邏輯。filterMethod 接收一個(gè)方法,當(dāng)搜索關(guān)鍵字變化時(shí),會(huì)將當(dāng)前的關(guān)鍵字和每個(gè)數(shù)據(jù)項(xiàng)傳給該方法。若方法返回 true,則會(huì)在搜索結(jié)果中顯示對(duì)應(yīng)的數(shù)據(jù)項(xiàng)。

constructor(props) {
  super(props);
  this.state = {
    value: []
  }


  this._handleChange = this.handleChange.bind(this);
  this._filterMethod = this.filterMethod.bind(this);
}


get data() {
  const data = [];
  const cities = ['上海', '北京', '廣州', '深圳', '南京', '西安', '成都'];
  const pinyin = ['shanghai', 'beijing', 'guangzhou', 'shenzhen', 'nanjing', 'xian', 'chengdu'];
  cities.forEach((city, index) => {
    data.push({
      label: city,
      key: index,
      pinyin: pinyin[index]
    });
  });
  return data;
}


filterMethod(query, item) {
  return item.pinyin.indexOf(query) > -1;
}


handleChange(value) {
  this.setState({ value })
}


render() {
  const { value } = this.state;
  return (
    <Transfer
      filterable
      filterMethod={this._filterMethod}
      filterPlaceholder="請(qǐng)輸入城市拼音"
      value={value}
      onChange={this._handleChange}
      data={this.data}>
    </Transfer>
  )
}

可自定義

可以對(duì)列表標(biāo)題文案、按鈕文案、數(shù)據(jù)項(xiàng)的渲染函數(shù)、列表底部的勾選狀態(tài)文案、列表底部的內(nèi)容區(qū)等進(jìn)行自定義。

可以使用 titles、buttonTexts、renderContentfooterFormat 屬性分別對(duì)列表標(biāo)題文案、按鈕文案、數(shù)據(jù)項(xiàng)的渲染函數(shù)和列表底部的勾選狀態(tài)文案進(jìn)行自定義。對(duì)于列表底部的內(nèi)容區(qū),提供了兩個(gè)屬性:leftFooterrightFooter。此外,如果希望某些數(shù)據(jù)項(xiàng)在初始化時(shí)就被勾選,可以使用 leftDefaultCheckedrightDefaultChecked 屬性。最后,本例還展示了 onChange 事件的用法。

constructor(props) {
  super(props);
  this.state = {
    value: [1]
  }


  this._handleChange = this.handleChange.bind(this);
  this._filterMethod = this.filterMethod.bind(this);
  this._renderFunc = this.renderFunc.bind(this);
}


get data() {
  const data = [];
  for (let i = 1; i <= 15; i++) {
    data.push({
      key: i,
      label: `備選項(xiàng) ${ i }`,
      disabled: i % 4 === 0
    });
  }
  return data;
}


filterMethod(query, item) {
  return item.label.indexOf(query) > -1;
}


handleChange(value) {
  this.setState({ value })
}


renderFunc(option) {
  return <span>{ option.key } - { option.label }</span>;
}


get style() {
  return {
    marginLeft: '20px',
    padding: '6px 5px'
  }
}


render() {
  const { value } = this.state;


  return (
    <Transfer
      value={value}
      filterable
      leftDefaultChecked={[2, 3]}
      rightDefaultChecked={[1]}
      renderContent={this.renderFunc}
      titles={['Source', 'Target']}
      buttonTexts={['到左邊', '到右邊']}
      footerFormat={{
        noChecked: '${total}',
        hasChecked: '${checked}/${total}'
      }}
      onChange={this._handleChange}
      data={this.data}
      leftFooter={
        <Button style={this.style} size="small">操作</Button>
      }
      rightFooter={
        <Button style={this.style} size="small">操作</Button>
      }
    >
    </Transfer>
  )
}

數(shù)據(jù)項(xiàng)屬性別名

默認(rèn)情況下,Transfer 僅能識(shí)別數(shù)據(jù)項(xiàng)中的 key、labeldisabled 字段。如果你的數(shù)據(jù)的字段名不同,可以使用 propsAlias 屬性為它們?cè)O(shè)置別名。

本例中的數(shù)據(jù)源沒有 keylabel 字段,在功能上與它們相同的字段名為 valuedesc。因此可以使用props 屬性為 keylabel 設(shè)置別名。

constructor(props) {
  super(props);
  this.state = {
    value: []
  }


  this._handleChange = this.handleChange.bind(this);
}


get data() {
  const data = [];
  for (let i = 1; i <= 15; i++) {
    data.push({
      value: i,
      desc: `備選項(xiàng) ${ i }`,
      disabled: i % 4 === 0
    });
  }
  return data;
}


handleChange(value, direction, movedKeys) {
  console.log(value, direction, movedKeys);
  this.setState({ value })
}


render() {
  const { value } = this.state;
  return (
    <Transfer
      value={value}
      propsAlias={{
        key: 'value',
        label: 'desc'
      }}
      data={this.data}
      onChange={this._handleChange}
      >
    </Transfer>
  )
}

Attributes

參數(shù) 說明 類型 可選值 默認(rèn)值
data Transfer 的數(shù)據(jù)源 array[{ key, label, disabled }] [ ]
filterable 是否可搜索 boolean false
filterPlaceholder 搜索框占位符 string 請(qǐng)輸入搜索內(nèi)容
filterMethod 自定義搜索方法 function
titles 自定義列表標(biāo)題 array ['列表 1', '列表 2']
buttonTexts 自定義按鈕文案 array [ ]
renderContent 自定義數(shù)據(jù)項(xiàng)渲染函數(shù) function(h, option)
footerFormat 列表底部勾選狀態(tài)文案 object{noChecked, hasChecked} { noChecked: '共 ${total} 項(xiàng)', hasChecked: '已選 ${checked}/${total} 項(xiàng)' }
propsAlias 數(shù)據(jù)源的字段別名 object{key, label, disabled}
leftDefaultChecked 初始狀態(tài)下左側(cè)列表的已勾選項(xiàng)的 key 數(shù)組 array [ ]
rightDefaultChecked 初始狀態(tài)下右側(cè)列表的已勾選項(xiàng)的 key 數(shù)組 array [ ]
leftFooter 左側(cè)列表底部的內(nèi)容 ReactElement -
rightFooter 右側(cè)列表底部的內(nèi)容 ReactElement -

Events

事件名稱 說明 回調(diào)參數(shù)
onChange 右側(cè)列表元素變化時(shí)觸發(fā) 當(dāng)前值、數(shù)據(jù)移動(dòng)的方向('left' / 'right')、發(fā)生移動(dòng)的數(shù)據(jù) key 數(shù)組
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)