简体   繁体   English

使用多个控件过滤JSON数据数组

[英]Filtering array of JSON data using multiple controls

I've used bootstrap and jquery to make a nice little mobile app that pulls an array of objects from a URL and create a table of them. 我使用了bootstrap和jquery来制作一个不错的小型移动应用程序,该应用程序从URL中提取对象数组并创建它们的表。 Now I have about 5 controls (button groups and dropdowns) that I'd like to use to filter the JSON data. 现在,我有大约5个控件(按钮组和下拉菜单)可用于过滤JSON数据。 My question is should I use ajax? 我的问题是我应该使用ajax吗? I'd like to have the filter occur without a page refresh when any of the controls are changed. 我希望在更改任何控件时都可以在不刷新页面的情况下进行过滤。 It's been many years since I've used ajax (back when I developed in .NET) but I know it's built into jquery. 自从我使用ajax以来已经有很多年了(当我在.NET中开发时),但是我知道它已内置在jquery中。 Here's my jquery function that builds my table of objects: 这是构建我的对象表的jquery函数:

$(document).ready(function () {

    var url = "http://json.url/";
    $.getJSON(url, 
        function ( json ) { 
            var tr;
            for (var i = 0; i < json.length; i++) {

                //Create each table row
                tr = $('<tr data-id="' + json[i].id + '" class="tap">');
                tr.append("<td>" + "<span>" + json[i].part_number + "</span>" + "<br>" + 
                                    json[i].diameter + '\"  &raquo; ' + json[i].phases + ' &raquo; ' + 
                                    json[i].voltage + ' &raquo; ' + json[i].rpm);
                tr.append("</td></tr>");
                $('table').append(tr);
             }
    });
});

Would it be better to keep the original array and store it? 保留原始数组并存储会更好吗? Or maybe call it every time I click one of the filters? 还是每次我单击其中一个过滤器时都调用它? I have 4 bootstrap button groups and and 3 select inputs that I want to use to filter when any or all of them are changed (preferably without a submit button). 我有4个引导程序按钮组和3个选择输入,当它们中的任何一个或全部更改时(最好没有提交按钮),我想使用这些输入进行过滤。 I'm reading up on jquery.ajax now but wanted to get a question out there because I'm sure I'll need a little assistance. 我现在正在阅读jquery.ajax,但想在那里提一个问题,因为我确定我需要一点帮助。

How could AJAX help you? AJAX如何为您提供帮助? Does the JSON url allow you to provide params and it will filter the data for you? JSON网址是否允许您提供参数,并且会为您过滤数据?

If the url you are making the getJSON request does not filter data for you, I think you will have to store the data in an array in javascript, and perform filters on that array based on the values of the options you provide 如果您发出的getJSON请求的网址没有为您过滤数据,我认为您将必须将数据存储在javascript中的数组中,并根据您提供的选项的值对该数组执行过滤器

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

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