简体   繁体   English

按钮单击时对Firebase数据进行排序

[英]Sorting Firebase data on button click

Currently within my application, I am displaying a list of books from my Firebase database: 目前,在我的应用程序中,我正在显示Firebase数据库中的书籍列表:

书单

I would like to have an on click event which when the AZ icon (shown in the image) is clicked, the list is sorted in Alphabetical order. 我希望有一个点击事件,当点击AZ图标(如图所示)时,列表按字母顺序排序。

The code snippets show what I have so far: 代码片段显示了我到目前为止所拥有的内容:

 var myFirebase = new Firebase('https://project04-167712.firebaseio.com/object/Book/'); const ulList = document.getElementById('list'); const dbRefObject = firebase.database().ref().child('object'); const dbRefList = dbRefObject.child('Book'); var sort = document.querySelector('#sort'); sort.addEventListener("click", function() { //Where I want the sort event to go //So far I have this: dbRefList.orderByChild('book').on('child_added', snap => { const li = document.createElement('li'); li.innerText = snap.val().book; li.id = snap.key; ulList.appendChild(li); }); //This is taken directly from the child added but changed to orderByChild. //It sorts the list but of course just re prints it below the unsorted list. }); dbRefList.on('child_added', snap => { const li = document.createElement('li'); li.innerText = snap.val().book; li.id = snap.key; ulList.appendChild(li); }); dbRefList.on('child_changed', snap => { const liChanged = document.getElementById(snap.key); liChanged.innerText = snap.val().book; }); dbRefList.on('child_removed', snap => { const liToRemove = document.getElementById(snap.key); liToRemove.remove(); }); 
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> <div id="sortContainer"> <i id="sort" class="material-icons">sort_by_alpha</i> </div> 

I know I should be using orderByChild - I'm just not sure how to implement. 我知道我应该使用orderByChild - 我只是不确定如何实现。 I tweaked the 'child_added' function to orderByChild which does sort the list, but prints it below the original sorted list: 我将'child_added'函数调整为orderByChild,它对列表进行排序,但将其打印在原始排序列表下面:

排序列表

Please could anyone advise what the most efficient way of sorting the list is onClick. 请任何人都可以告知最有效的排序方法是onClick。 At some point as well I want to be able to sort the list in reverse order - ZA - So any help on that would be great as well. 在某些时候,我希望能够以相反的顺序对列表进行排序 - ZA - 所以对此的任何帮助都会很好。

Here is the screen shot of the firebase JSON file: 以下是firebase JSON文件的屏幕截图: JSON

Many thanks, G 非常感谢,G

The problem is with this part of your code: 问题出在这部分代码中:

dbRefList.orderByChild('book').on('child_added', snap => {
    const li = document.createElement('li');
    li.innerText = snap.val().book;
    li.id = snap.key;
    ulList.appendChild(li);
});

You are creating a new li again below the unsorted one. 您正在未分类的下方再次创建新的li Clear the parent list and then append. 清除父列表 ,然后追加。

ulList.innerHTML = '';

Add this inside your sortEventListener function. 在sortEventListener函数中添加它。 It should work. 它应该工作。

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

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