简体   繁体   English

使用JQuery使用下拉列表过滤HTML表值

[英]Filter HTML Table Values with Dropdown Using JQuery

I am new to coding and have had trouble finding the answer to this question. 我是编码新手,很难找到该问题的答案。 I think my lack of industry vocabulary is limiting my ability to properly search for the solution.. I've searched for two days. 我认为我缺乏行业词汇限制了我正确搜索解决方案的能力。我已经搜索了两天。

I have a simple HTML table displaying data from my firebase database. 我有一个简单的HTML表,显示我的Firebase数据库中的数据。 The table looks something like this: 该表如下所示:

Date       Google   Yelp   Bing
-----------------------------------
Mar 2015     1       3      2
Feb 2015     4       0      2
Jan 2015     0       6      1

The above table is representative of where our medical practice receives its patients each month. 上表代表了我们的医学实践每月在哪里接待患者。 We are an office of four doctors. 我们是四个医生的办公室。

QUESTION: I'd like to create a dropdown at the top of the table with a list of each doc's name. 问题:我想在表格顶部创建一个下拉列表,其中列出了每个文档的名称。 When I choose the doctor, the table updates automatically with only the stats for that doctor. 当我选择医生时,表格将仅使用该医生的统计信息自动更新。

How do I solve this? 我该如何解决? How do I filter my data? 如何过滤数据? I'd be super happy if someone could point me in the right direction. 如果有人能指出我正确的方向,我将非常高兴。

LIMITATIONS: I am learning HTML, CSS, JAVASCRIPT, and JQUERY and would like to limit my answer using only those languages. 限制:我正在学习HTML,CSS,JAVASCRIPT和JQUERY,并且想仅使用那些语言来限制我的答案。

FIREBASE DATABASE: Below is a snippet of the firebase export... notice how each patient is entered into the db as a unique object (ie. "-JlItUSK..."). FIREBASE数据库:以下是Firebase导出的代码段...请注意如何将每个患者作为唯一对象输入db(即“ -JlItUSK ...”)。

Then what I do is query the db like this: If the date is march 2015, and the channel is 'Internet', display it in this table cell. 然后,我要做的就是这样查询数据库:如果日期是2015年3月,并且频道是“ Internet”,则在此表格单元格中显示它。

But when I showed this amazing referral tracker to the doctors, their immediate response was: "can I filter by my name?" 但是,当我向医生展示此惊人的转诊跟踪器时,他们的立即反应是:“我可以按我的名字过滤吗?” and "what if I only want to see the female patients - can I filter by gender?" 和“如果我只想看女性患者,该如何按性别过滤?”

So, to reiterate my question: how do I create a dropdown that will re-query the database and show the new data based upon the doctor's filter choice? 因此,重申一下我的问题:如何创建一个下拉列表,该下拉列表将重新查询数据库并根据医生的过滤器选择显示新数据?

{
  "entries" : {
    "-JlItUSK9n-GD70XAyGz" : {
      "age" : "22",
      "channel" : "null",
      "country" : "USA",
      "date" : "2010-02-26",
      "doctor" : "bh",
      "gender" : "female",
      "initials" : "OW",
      "source" : "Insurance",
      "zip" : "90047"
    },
    "-JlIy5378hYrzPpIw0Ll" : {
      "age" : "12",
      "channel" : "Google",
      "country" : "USA",
      "date" : "2014-05-26",
      "doctor" : "lc",
      "gender" : "male",
      "initials" : "FN",
      "source" : "Internet",
      "zip" : "90029"
    },
    "-JlIzkfSxx9XdVzC74x7" : {
      "age" : "12",
      "channel" : "Bing",
      "country" : "USA",
      "date" : "2011-11-26",
      "doctor" : "bh",
      "gender" : "male",
      "initials" : "SI",
      "source" : "Internet",
      "zip" : "90021"
    }
  }
}

Edited, due to new code and data from OP: 由于来自OP的新代码和数据,已编辑:

I've made a small example of how you could filter the data, with requerying the database. 我已经举了一个小例子说明如何通过重新查询数据库来过滤数据。 I only used two of your tables, so you'll have to add the filter mechanic to the rest of your code. 我只使用了两个表,因此您必须将过滤器机制添加到其余代码中。

I used this to filter by doctor (in the change event of a select): 我用它来按doctor过滤(在选择的更改事件中):

fbEntries.orderByChild("doctor").equalTo(myFilterVariable).on("value", function (snapshot) {
    var fbobj = snapshot.val();
    parseMyData(fbobj); //I placed your old code in this new function
});

A few things regarding your code (not bad at all for aa beginner!): 有关代码的几件事(对于初学者来说一点都不差!):

You use a lot of static code and quite messy iterations to manipulate the DOM. 您使用大量静态代码和相当混乱的迭代来操纵DOM。 I followed your structure as much as I could, but also had to create a few functions, move some code and reorder it in some places. 我尽可能地遵循您的结构,但是还必须创建一些函数,移动一些代码并在某些地方重新排序。 I hope you can find your way around. 希望您能找到解决方法。

I also had to fix some html issues. 我还必须修复一些html问题。 All your dynamic html was created in "thead", and I needed to be able to remove (reinitialize) the table rows, so I added tbody and a few other things. 您所有的动态html都是在“ thead”中创建的,我需要能够删除(重新初始化)表行,因此我添加了tbody和其他一些东西。

Id's are supposed to be unique! ID应该是唯一的! You have the table id monthly-by-doc on several tables (there are more examples). 您在多个表上每个monthly-by-doc都有一个表ID(还有更多示例)。 Make sure that id's are unique for the entire page. 确保ID在整个页面上是唯一的。 I fixed this in my example. 我在示例中解决了这个问题。

You can see and test the code here: http://jsfiddle.net/9bqdf121/2/ 您可以在此处查看和测试代码: http : //jsfiddle.net/9bqdf121/2/

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

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