2016-08-15 163 views
0

我想基于选择框的选择值来筛选数据。从下拉列表中筛选数据

<select id="testDD"> 
    <option value="local">Local</option> 
    <option value="international">Internationl</option> 
</select> 
<div id="list"><!-- List Items to be generated from #testDD value --></div> 

我有数据对象这样

var data = [ 
    {type: 'local', name: 'abc1', location: 'xyz1'}, 
    {type: 'local', name: 'abc2', location: 'xyz2'}, 
    {type: 'international', name: 'abc3', location: 'xyz3'}, 
    {type: 'local', name: 'abc4', location: 'xyz4'}, 
    {type: 'local', name: 'abc5', location: 'xyz5'}, 
    {type: 'international', name: 'abc6', location: 'xyz6'}, 
    {type: 'international', name: 'abc7', location: 'xyz7'} 
] 

请告诉我筛选出基于选择的事件数据的理想方式改变了吗?

+0

@ coure2011做我的回答帮助你吗?需要更多信息? –

回答

0

const data = [ 
 
    {type: 'local', name: 'abc1', location: 'xyz1'}, 
 
    {type: 'local', name: 'abc2', location: 'xyz2'}, 
 
    {type: 'international', name: 'abc3', location: 'xyz3'}, 
 
    {type: 'local', name: 'abc4', location: 'xyz4'}, 
 
    {type: 'local', name: 'abc5', location: 'xyz5'}, 
 
    {type: 'international', name: 'abc6', location: 'xyz6'}, 
 
    {type: 'international', name: 'abc7', location: 'xyz7'} 
 
]; 
 

 
const filterStream = Rx.Observable.fromEvent(document.getElementById('testDD'), 'change'); 
 

 

 
filterStream 
 
    .map(evt => evt.target.value) 
 
    .startWith(null) 
 
    .subscribe(filterTypeName => { 
 
    const filteredData = data.filter(d => d.type === filterTypeName || !filterTypeName); 
 
    document.getElementById('list').innerText = JSON.stringify(filteredData); 
 
    });
<script src="https://unpkg.com/@reactivex/[email protected]/dist/global/Rx.umd.js"></script> 
 
    <select id="testDD"> 
 
    <option value="">Select to filter</option> 
 
    <option value="local">Local</option> 
 
    <option value="international">Internationl</option> 
 
    </select> 
 
    
 
    <h2>filtered resuls</h2> 
 
    <div id="list"> 
 
    <!-- List Items to be generated from #testDD value --> 
 
    </div>