2016-11-17 41 views
1

每個人。我有一個React應用程序,它應該根據不同的標準過濾天文館表演。的標準之一是應該有多個值:反應 - 從數組內部填充下拉對象和過濾器數據

var shows = [ 
    { id: 1, name: 'Black Holes', showType: 'Full Dome', age: 'All Ages', grade: ['Late Elementary', 'High School'], cover:'http://www.starsatnight.org/sciencetheater/assets/File/200x300_blackholes.png'}, 
    { id: 2, name: 'Astronaut', showType: 'Full Dome', age: 'All Ages', grade: ['Early Elementary,',' Late Elementary', 'High School'], cover:'http://www.starsatnight.org/sciencetheater/assets/File/200x300_astronaut.png'}, 
    { id: 3, name: 'Laser Holidays', showType: 'Laser', age: '18+', grade: ['Late Elementary', 'High School', 'College'], cover:'http://www.starsatnight.org/sciencetheater/assets/File/200x300_laserholidays.png'}, 
    { id: 4, name: 'The Gruffalo', showType: 'Flat Screen', age: 'All Ages', grade: ['Pre-K', 'Kinder'], cover:'http://www.starsatnight.org/sciencetheater/assets/File/200x300_gruffalo.png'}, 
    { id: 5, name: 'Laser iPOP', showType: 'Laser', age: 'All Ages', grade: ['Late Elementary', 'High School', 'College'], cover:'http://www.starsatnight.org/sciencetheater/assets/File/200x300_ipop.png'} 
]; 

的「顯示」對象的「等級」屬性可以有多個值,以及我決定把它們在陣列中。

我需要兩件事: 1 - 我需要用所有可能的值填充「等級」下拉列表,而不需要重複的值; 2 - 我需要根據用戶在該下拉列表中選擇的內容來篩選節目,類似於「顯示類型」和「年齡」下拉菜單。任何想法如何做到這一點?謝謝。

var shows = [ 
 
    { id: 1, name: 'Black Holes', showType: 'Full Dome', age: 'All Ages', grade: ['Late Elementary', 'High School'], cover:'http://www.starsatnight.org/sciencetheater/assets/File/200x300_blackholes.png'}, 
 
    { id: 2, name: 'Astronaut', showType: 'Full Dome', age: 'All Ages', grade: ['Early Elementary,',' Late Elementary', 'High School'], cover:'http://www.starsatnight.org/sciencetheater/assets/File/200x300_astronaut.png'}, 
 
    { id: 3, name: 'Laser Holidays', showType: 'Laser', age: '18+', grade: ['Late Elementary', 'High School', 'College'], cover:'http://www.starsatnight.org/sciencetheater/assets/File/200x300_laserholidays.png'}, 
 
    { id: 4, name: 'The Gruffalo', showType: 'Flat Screen', age: 'All Ages', grade: ['Pre-K', 'Kinder'], cover:'http://www.starsatnight.org/sciencetheater/assets/File/200x300_gruffalo.png'}, 
 
    { id: 5, name: 'Laser iPOP', showType: 'Laser', age: 'All Ages', grade: ['Late Elementary', 'High School', 'College'], cover:'http://www.starsatnight.org/sciencetheater/assets/File/200x300_ipop.png'} 
 
]; 
 

 
// FilterForm React Class 
 
var FilterForm = React.createClass({ 
 
    getInitialState: function() { 
 
     return { 
 
      data: this.props.data, 
 
      showType: '', 
 
      age: '', 
 
      grade: '', 
 
     } 
 
    }, 
 
    filterItems: function(val, type){ 
 
     switch (type) { 
 
      case 'showType': 
 
       this.setState({showType: val}); 
 
       break; 
 
      case 'age': 
 
       this.setState({age: val}); 
 
       break; 
 
      case 'grade': 
 
       this.setState({grade: val}); 
 
       break; 
 
      default: 
 
       break; 
 
     } 
 
    }, 
 
    render: function() { 
 
     var filteredItems = this.props.data; 
 
     var state = this.state; 
 
     ["showType", "age", "grade"].forEach(function(filterBy) { 
 
      var filterValue = state[filterBy]; 
 
      if (filterValue){ 
 
       filteredItems = filteredItems.filter(function(item){ 
 
        return item[filterBy] === filterValue; 
 
       }); 
 
      } 
 
     }); 
 
     var showTypeArray = this.props.data.map(function(item) {return item.showType}); 
 
     var ageArray = this.props.data.map(function(item) {return item.age}); 
 
     // This array gets once instance of all grade options since one show can be good for several grades 
 
     var gradeArray = this.props.data.map(function(item){ 
 
      return item.grade; 
 
     }); 
 
     showTypeArray.unshift(""); 
 
     ageArray.unshift(""); 
 
     gradeArray.unshift(""); 
 
     return(
 
       <div className="container"> 
 
        <FilterOptions 
 
         data={this.state.data} 
 
         showTypeOptions={showTypeArray} 
 
         ageOptions={ageArray} 
 
         gradeOptions={gradeArray} 
 
         changeOption={this.filterItems} /> 
 
        <div className="filter-form"> 
 
         <FilterItems data={filteredItems} /> 
 
        </div> 
 
       </div> 
 
     ) 
 
    } 
 
}); 
 

 
// FilterOptions React Class 
 
var FilterOptions = React.createClass({ 
 
    changeOption: function(type, e) { 
 
     var val = e.target.value; 
 
     this.props.changeOption(val, type); 
 
    }, 
 
    render: function(){ 
 
     return (
 
       <div className="filter-options"> 
 
        <div className="filter-option"> 
 
         <label>Show Type</label> 
 
         <select id="showType" value={this.props.showType} onChange={this.changeOption.bind(this, 'showType')}> 
 
          {this.props.showTypeOptions.map(function(option) { 
 
           return (<option key={option} value={option}>{option}</option>) 
 
          })} 
 
         </select> 
 
         <label>Age</label> 
 
         <select id="age" value={this.props.age} onChange={this.changeOption.bind(this, 'age')}> 
 
          {this.props.ageOptions.map(function(option) { 
 
           return (<option key={option} value={option}>{option}</option>) 
 
          })} 
 
         </select> 
 
         <label>Grade</label> 
 
         <select id="grade" value={this.props.grade} onChange={this.changeOption.bind(this, 'grade')}> 
 
          {this.props.gradeOptions.map(function(option) { 
 
           return (<option key={option} value={option}>{option}</option>) 
 
          })} 
 
         </select> 
 
        </div> 
 
       </div> 
 
     ); 
 
    } 
 
}); 
 

 
// FilterItems React Class 
 
var FilterItems = React.createClass({ 
 
    render: function(){ 
 
     return(
 
       <div className="filter-items"> 
 
        <br /> 
 
        {this.props.data.map(function(item){ 
 
         return(
 
           <div className="filter-item">{item.id} - {item.name} - {item.showType} - {item.age}</div> 
 
         ); 
 
        })} 
 
       </div> 
 
     ) 
 
    } 
 
}); 
 

 

 

 
ReactDOM.render(
 
     <FilterForm data={shows}/>, 
 
     document.getElementById('show-catalog') 
 
);
<script src="https://unpkg.com/[email protected]/dist/react.js"></script> 
 
<script src="https://unpkg.com/[email protected]/dist/react-dom.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script> 
 
<div id="show-catalog"></div>

+2

如果您一次提出一個問題,您就更有可能獲得幫助,並且包括您嘗試過的以及遇到的具體問題。 –

+0

謝謝,喬丹。我擁有的代碼就是我所嘗試過的。下次我會記住它。 –

回答

0

1)您可以使用設置陣列得到一組唯一值的。例如,

var gradeArray = this.props.data.map(function(item){ 
    return item.grade; 
}); 
var uniqueGrades = Array.from(new Set(gradeArray)); 

2)什麼是不關於您當前的代碼?