如何在rxjs中显示来自对象数组的搜索结果?

huangapple go评论67阅读模式
英文:

How to display the search result from array of object in rxjs?

问题

我正在使用rxjs库,并使用简单的JavaScript来实现我的代码。我使用ajax来进行游戏的API请求,然后将所有数据映射到表格中。表格包括游戏的名称、发布日期和流派。现在我只需根据输入搜索显示行;基本上,用户将通过名称搜索游戏,只有该游戏的数据将显示在该行。如何使用Rxjs实现这样的功能?

我已经添加了代码片段。

注意:获取数据需要4-5秒。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/7.8.0/rxjs.umd.min.js"
        integrity="sha512-v0/YVjBcbjLN6scjmmJN+h86koeB7JhY4/2YeyA5l+rTdtKLv0VbDBNJ32rxJpsaW1QGMd1Z16lsLOSGI38Rbg=="
        crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</head>
<body>
    <h1>Games</h1>
    <input type="text" name="" id="search_game" placeholder="search games">
    <p id="test"></p>
    <table cellpadding="10">
        <thead>
            <tr>
                <th style="text-align:left;" id="game_name">Name</th>
                <th style="text-align:left;">Released</th>
                <th style="text-align:left;">Genre</th>
            </tr>
        </thead>
        <tbody id="table-content">
        </tbody>
    </table>
</body>
<script>
const { of, fromEvent, ajax: { ajax }, operators: { startWith, switchMap, map, debounce }, combineLatest } = rxjs;

const searchGame = document.getElementById('search_game');

const search = fromEvent(searchGame, 'input');
search.subscribe(e => console.log(e.target.value));

const games = ajax.getJSON('https://api.sampleapis.com/switch/games');
games.subscribe(gamesList => {
    let tableContent = '';
    gamesList.forEach(game => {
        tableContent += '<tr>';
        tableContent += '<td>' + game.name + '</td>';
        tableContent += '<td>' + (!game.releaseDates.Japan + ' ' + '(Japan)' ? game.releaseDates.Europe + ' ' + '(Europe)' : game.releaseDates.Australia + ' ' + '(Australia)') + '</td>';
        tableContent += '<td>' + game.genre + '</td>';
    });
    document.getElementById('table-content').innerHTML = tableContent;
    console.log(gamesList);
});
var x = document.getElementById('game_name').value;
</script>
</html>

这段代码是使用Rxjs库进行游戏数据搜索和呈现的示例。当用户在输入框中输入游戏名称时,它会根据输入实时更新表格内容,只显示与搜索匹配的游戏数据。

英文:

So I am using rxjs library and implementing my code using simple javascript. So I used ajax to make an API request for games and then mapped all the data inside the table, the table has the name of the game, released date, and genre. Now I have to only show the row based on the input search; basically, the user will search the game by name and only the data of that respective game will be shown in that particular row. How can we achieve such functionality using Rxjs?

I have added the snippet of the code.<br>

Note: It takes 4-5 sec to fetch data.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;title&gt;Document&lt;/title&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/rxjs/7.8.0/rxjs.umd.min.js&quot;
integrity=&quot;sha512-v0/YVjBcbjLN6scjmmJN+h86koeB7JhY4/2YeyA5l+rTdtKLv0VbDBNJ32rxJpsaW1QGMd1Z16lsLOSGI38Rbg==&quot;
crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Games&lt;/h1&gt;
&lt;input type=&quot;text&quot; name=&quot;&quot; id=&quot;search_game&quot; placeholder=&quot;search games&quot;&gt;
&lt;p id=&quot;test&quot;&gt;&lt;/p&gt;
&lt;table cellpadding=&quot;10&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style=&quot;text-align:left;&quot;id=&quot;game_name&quot;&gt;Name&lt;/th&gt;
&lt;th style=&quot;text-align:left;&quot;&gt;Released&lt;/th&gt;
&lt;th style=&quot;text-align:left;&quot;&gt;Genre&lt;/th&gt;
&lt;/tr&gt;
&lt;tbody id=&quot;table-content&quot;&gt;
&lt;/tbody&gt;
&lt;/thead&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;script&gt;
const { of, fromEvent, ajax: { ajax }, operators: { startWith, switchMap, map, debounce }, combineLatest } = rxjs;
const searchGame = document.getElementById(&#39;search_game&#39;);
const search = fromEvent(searchGame, &#39;input&#39;);
search.subscribe(e=&gt;console.log(e.target.value));
const games = ajax.getJSON(&#39;https://api.sampleapis.com/switch/games&#39;);
games.subscribe(gamesList =&gt; {
let tableContent = &#39;&#39;;
gamesList.forEach(game =&gt; {
tableContent += `&lt;tr&gt;`;
tableContent += `&lt;td&gt;` + game.name + `&lt;/td&gt;`
tableContent += `&lt;td&gt;` + (!game.releaseDates.Japan + &#39; &#39; + &#39;(Japan)&#39; ? game.releaseDates.Europe + &#39; &#39; + `(Europe)` : game.releaseDates.Australia + &#39; &#39; + `(Australia)`) + `&lt;/td&gt;`
tableContent += `&lt;td&gt;` + game.genre + `&lt;/td&gt;`
})
document.getElementById(&#39;table-content&#39;).innerHTML = tableContent
console.log(gamesList);
})
var x = document.getElementById(&#39;game_name&#39;).value;
&lt;/script&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 1

你需要将搜索映射到它的值并且使其以某种方式开始这样你就不需要先按键

const search = fromEvent(searchGame, 'input').pipe(
  map(e => e.target.value),
  startWith('')
);

然后你可以在这两个可观察对象上使用 combine latest 来过滤只包含搜索字符串的游戏

const { of, fromEvent, ajax: { ajax }, operators: { startWith, switchMap, map, debounce }, combineLatest } = rxjs;

const searchGame = document.getElementById('search_game');

const search = fromEvent(searchGame, 'input').pipe(
  map(e => e.target.value),
  startWith('')
);

const games = ajax.getJSON('https://api.sampleapis.com/switch/games');

combineLatest([games, search]).pipe(
  map(([games, search]) => games.filter(game => game.name.indexOf(search) !== -1))
)
.subscribe(gamesList => {
    let tableContent = '';
    gamesList.forEach(game => {
        tableContent += '<tr>';
        tableContent += '<td>' + game.name + '</td>'
        tableContent += '<td>' + (!game.releaseDates.Japan + ' ' + '(Japan)' ? game.releaseDates.Europe + ' ' + '(Europe)' : game.releaseDates.Australia + ' ' + '(Australia)') + '</td>'
        tableContent += '<td>' + game.genre + '</td>'
    })
    document.getElementById('table-content').innerHTML = tableContent
});
英文:

You need to map the search to it's value and make it start with something so you don't need to press a key first.

const search = fromEvent(searchGame, &#39;input&#39;).pipe(
map(e =&gt; e.target.value),
startWith(&#39;&#39;)
);

then you can use combine latest on the two observables to filter the games to just the ones where the name contains the search string.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const { of, fromEvent, ajax: { ajax }, operators: { startWith, switchMap, map, debounce }, combineLatest } = rxjs;
const searchGame = document.getElementById(&#39;search_game&#39;);
const search = fromEvent(searchGame, &#39;input&#39;).pipe(
map(e =&gt; e.target.value),
startWith(&#39;&#39;) // Otherwise you wont get any results until you press a key
);
const games = ajax.getJSON(&#39;https://api.sampleapis.com/switch/games&#39;);
combineLatest([games, search]).pipe(
map(([games, search]) =&gt; games.filter(game =&gt; game.name.indexOf(search) !== -1))
)
.subscribe(gamesList =&gt; {
let tableContent = &#39;&#39;;
gamesList.forEach(game =&gt; {
tableContent += `&lt;tr&gt;`;
tableContent += `&lt;td&gt;` + game.name + `&lt;/td&gt;`
tableContent += `&lt;td&gt;` + (!game.releaseDates.Japan + &#39; &#39; + &#39;(Japan)&#39; ? game.releaseDates.Europe + &#39; &#39; + `(Europe)` : game.releaseDates.Australia + &#39; &#39; + `(Australia)`) + `&lt;/td&gt;`
tableContent += `&lt;td&gt;` + game.genre + `&lt;/td&gt;`
})
document.getElementById(&#39;table-content&#39;).innerHTML = tableContent
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/rxjs/7.8.0/rxjs.umd.min.js&quot;
integrity=&quot;sha512-v0/YVjBcbjLN6scjmmJN+h86koeB7JhY4/2YeyA5l+rTdtKLv0VbDBNJ32rxJpsaW1QGMd1Z16lsLOSGI38Rbg==&quot;
crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
&lt;h1&gt;Games&lt;/h1&gt;
&lt;input type=&quot;text&quot; name=&quot;&quot; id=&quot;search_game&quot; placeholder=&quot;search games&quot;&gt;
&lt;p id=&quot;test&quot;&gt;&lt;/p&gt;
&lt;table cellpadding=&quot;10&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style=&quot;text-align:left;&quot;id=&quot;game_name&quot;&gt;Name&lt;/th&gt;
&lt;th style=&quot;text-align:left;&quot;&gt;Released&lt;/th&gt;
&lt;th style=&quot;text-align:left;&quot;&gt;Genre&lt;/th&gt;
&lt;/tr&gt;
&lt;tbody id=&quot;table-content&quot;&gt;
&lt;/tbody&gt;
&lt;/thead&gt;
&lt;/table&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月19日 15:41:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定