2016-05-01 106 views
0

假設我有一個記錄列表,並且想根據用戶所做的選擇框選擇來對它們進行過濾。我有jsfiddle中的細節。在react.js中過濾記錄

http://jsfiddle.net/reactjs/69z2wepo/

所以如果用戶選擇1-10僅在1〜10這些數字應顯示。 我們如何添加一個事件處理程序來過濾所有記錄?

var numbers = [ 
    {"number":1}, {"number":2}, {"number":3}, {"number":4}, {"number":5}, {"number":6}, {"number":7}, {"number":1}, {"number":1}, {"number":1}, {"number":1}, {"number":1}, {"number":1}, {"number":18}, {"number":11}, {"number":1}, {"number":1}, {"number":1}, {"number":1}, {"number":1}, {"number":21}, {"number":41}, {"number":1}, {"number":1}, {"number":1}, {"number":1}, {"number":1},  {"number":14}, {"number":1}, {"number":1}, {"number":1}, {"number":1}, {"number":1}, {"number":81}, {"number":12}, {"number":1}, {"number":1}, {"number":1}, {"number":1}, {"number":1}, {"number":1}, {"number":111} 
]; 
var DisplayNumbers = React.createClass({  
render: function(){ 

return (
<div> 
    {this.props.allnumbers} 
    </div> 
); 
} 
     }); 
var FilterBox = React.createClass({ 
     getInitialState: function() { 
     return { selected: "1-10" } 
     },  

      render: function() { 
     return (
      React.createElement("select", { value: this.state.selected }, 
      React.createElement("option", { value: 1 }, "1-10"), 
      React.createElement("option", { value: 2 }, "11-20"), 
      React.createElement("option", { value: 3 }, "21-30"), 
      React.createElement("option", { value: 4 }, "31-40") 
     ) 
     ) 
     } 
    }); 


var App = React.createClass({ 
    render: function() {    
     return (
     <div> 
      <FilterBox /> 
         <h1>Requests</h1> 
       <DisplayNumbers allnumbers={this.props.data} /> 
      </div> 
     ); 
    } 
}); 
ReactDOM.render(<App data={numbers}/>, document.getElementById('container')); 
+1

JS小提琴只包含一個hello世界的例子。 – sahil

回答

2

由於@Scarysize建議你必須提供onChange事件處理程序到你的表單元素。同樣在DisplayNumbers組件中,您嘗試呈現js對象的數組,但反應不會呈現它。它必須是一系列法律反應組件才能以這種方式呈現。

在這個小提琴中:https://jsfiddle.net/sehrob/r4c2ycmc/ - 我已經對您的代碼進行了一些更改以使其可行。你可以使用它,如果你想。

P.S. Thinking in React也可以爲你實現你的應用程序非常有用。