简体   繁体   English

如何垂直渲染数组

[英]How to vertically render an array

I am trying to render an Array vertically 我试图垂直渲染一个数组

This is how it is been displayed so far 到目前为止它是如何显示的

在此输入图像描述

But I need it like this 但我需要这样的

Travel

Heart

Earth

Blackjack

and so on... 等等...

here is the code 这是代码

let textareaStyle = {
  width: '100%'
}, people = [
    'Hello',
    'Travel',
    'Heart',
    'Earth',
    'Hills',
    'Blackjack',
    'Casino',
    'Alberto',
    'Marcelo',
    'Jorge'
  ], displayResult;

class Login extends Component {

  constructor() {
    super();
    this.state = {value : '', result: ''};
  } 

  render () {
    return (
         <input type="text" onChange={this._onChange.bind(this)} style={textareaStyle}
                     onKeyUp={this._changeInput.bind(this)} value={this.state.value} />
         <p>{this.state.result}</p>
    );
  }

  _matchPeople = (input) => {
    let reg = new RegExp(input.split('').join('\\w*').replace(/\W/, ""), 'i');
    return people.filter(function(person) {
      if (person.match(reg)) {
        return person;
      }
    });
  }

  _changeInput = (val) => {
    let autoCompleteResult = this._matchPeople(this.state.value);
    this.setState({result: autoCompleteResult.join(', ')});
  }

  _onChange = (event) => {
    this.setState({value: event.target.value});
  }  

}


export default Login;

so here I am displaying the results 所以我在这里展示结果

<p>{this.state.result}</p>

there is where I need to display it vertically, like a dropdown 我需要垂直显示它,就像下拉菜单一样

The function matchPeople returns an array. 函数matchPeople返回一个数组。

Then, you set that array as the innerHTML of #result . 然后,将该数组设置为#resultinnerHTML

However, innerHTML expects a string, so it uses the toString method to convert the array to an string. 但是, innerHTML需要一个字符串,因此它使用toString方法将数组转换为字符串。 That method concatenates the items in the array with a comma separator. 该方法使用逗号分隔符连接数组中的项。

Instead, you can try joining the array with a <br /> element as a separator, which will produce line breaks between the results. 相反,您可以尝试使用<br />元素作为分隔符连接数组,这将在结果之间产生换行符。

result.innerHTML = autoCompleteResult.join('<br />');

Alternatively, since you are only dealing with text, I recommend avoiding innerHTML and using textContent instead, with a newline character as a separator. 或者,由于您只处理文本,我建议避免使用innerHTML并使用textContent ,并使用换行符作为分隔符。

result.textContent = autoCompleteResult.join('\n');

Note newlines are collapsed into a normal space by default. 注意默认情况下,换行符会折叠到普通空间中。 To prevent it, use white-space : 要防止它,请使用white-space

#result { white-space: pre-line; }

filter is not a mutator in respect to each element; filter对于每个元素都不是一个mutator; the map method is appropriate: map方法是合适的:

 var people = ['Steven', 'Sean', 'Stefan', 'Sam', 'Nathan']; function matchPeople(input) { var reg = new RegExp(input.split('').join('\\\\w*').replace(/\\W/, ""), 'i'); return people.map(function(person) { if (person.match(reg)) { return '<br>' + person; }; }); } function changeInput(val) { var autoCompleteResult = matchPeople(val); document.getElementById("result").innerHTML = autoCompleteResult; } 
 <input type="text" onkeyup="changeInput(this.value)"> <div id="result"> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM