简体   繁体   English

过滤大表(HTML JS PHP MYSQL)

[英]Filter large table (HTML JS PHP MYSQL)

I have a database with a lot of fields. 我有一个包含很多字段的数据库。 I want the user to be able to filter on multiple fields. 我希望用户能够在多个字段上进行过滤。 I am looking for a Javascript implementation (without jQuery) and possibly a jQuery one as well. 我正在寻找一种Javascript实现(没有jQuery),可能还有一种jQuery。 What I am going for is the function that excel provides when you apply filters to table headers only in html, php, js, and using mySQL as the database. 我要寻找的是excel提供的功能,当您仅在html,php,js和使用mySQL作为数据库的表头中应用过滤器时。

This is my flow so far: 到目前为止,这是我的流程:

  1. PHP builds the dropdowns for the seperate fields, example: PHP为单独的字段构建下拉列表,例如:

    option value=\\"".$row[$filterby]."\\">" .... 选项值= \\“”。$ row [$ filterby]。“ \\”>“...。

  2. User selects an item and "onChange" calls a JS function to build the search string: 用户选择一个项目,然后“ onChange”调用JS函数来构建搜索字符串:

    ?fruit=apple&color=green ?水果=苹果&颜色=绿色

The issue I am having is in order to update my table, I have to call a seperate PHP page in my JS (using window.location = "filtered.php" + searchString;) 我遇到的问题是为了更新表,我必须在JS中调用单独的PHP页面(使用window.location =“ filtered.php” + searchString;)

The code works for one field, but if I want multiple fields to be filtered, the new page has no memory of what I just passed in. 该代码适用于一个字段,但是如果我要过滤多个字段,则新页面不会存储我刚刚传入的内容。

My question is two-fold: 我的问题有两个:

  1. Am I going about this completely wrong? 我要彻底解决这个问题吗? What is the best way to produce what I am going for? 产生我想要的东西的最好方法是什么?
  2. Is this possible without JQuery or AJAX? 没有JQuery或AJAX,这可能吗?

EDIT: 编辑:

Thought I'd add a visual for clarity. 以为我会添加一个视觉清晰的图像。

+----+--------+----------+
| id | Fruit  | Color    | 
+----+--------+----------+
| 1  | apple  | red      | 
| 2  | mango  | yellow   | 
| 3  | banana | yellow   | 
| 4  | apple  | green    | 
+----+--------+----------+

filter.php?fruit=apple&color=green filter.php?fruit = apple&color = green

+----+--------+----------+
| id | Fruit  | Color    | 
+----+--------+----------+
| 4  | apple  | green    | 
+----+--------+----------+

EDIT2: 编辑2:

Would it be better to get all data with php and have the filters just update the html data? 使用php获取所有数据并让过滤器仅更新html数据会更好吗? Or should I try to have the JS query the DB everytime a dropdown is changed? 还是应该在每次下拉列表更改时让JS查询数据库?

I found what I was looking for at: 我找到了想要的东西:

http://www.javascripttoolbox.com/lib/table/examples.php http://www.javascripttoolbox.com/lib/table/examples.php

It has a lot of good examples of pure JS filtering methods, and I'll try to use these for my solution (unless someone has a better one). 它有很多纯JS过滤方法的很好的例子,我将尝试在我的解决方案中使用这些方法(除非有人有更好的方法)。

function price_filter(str)
{       
    var subcategory = new Array();
    $('input[type="radio"]:checked').each(function(){subcategory.push($(this).val());});
    alert (subcategory);
    alert(subcategory.length);
    var price1 = str;
    if(subcategory.length == '0')
    {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {       

            if(xmlhttp.readyState == 4)
            {     
                document.getElementById("pri_fil").innerHTML = xmlhttp.responseText;    
            }
        };

        xmlhttp.open("GET","search.php?price=" + price1,true);
        xmlhttp.send(); 
    }   
    else
    {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if(xmlhttp.readyState == 4)
            {
                document.getElementById("pri_fil").innerHTML = xmlhttp.responseText;    
            }
        };

        xmlhttp.open("GET","search.php?price=" + price1 + "&subcategory=" + subcategory,true);
        xmlhttp.send();         
    }
}

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

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