简体   繁体   English

如何使用关键字过滤JSON数组对象?

[英]How to use keywords to filter a JSON array object?

I am trying to find a way to take an array of keywords that are user createda and filter a JSON array, and have the results appended to the screen. 我正在尝试寻找一种方法来获取用户创建的关键字数组并过滤JSON数组,并将结果附加到屏幕上。

Below is the actual code I am using. 以下是我正在使用的实际代码。

var keywords= [];
var interval = "";
var pointer = '';
var scroll = document.getElementById("tail_print");

$("#filter_button").click(
function(){
    var id = $("#filter_box").val(); 
    if(id == "--Text--" || id == ""){
        alert("Please enter text before searching.");
    }else{
        keywords.push(id);
        $("#keywords-row").append("<td><img src=\"images/delete.png\" class=\"delete_filter\" /> " + id + "</td>");
    }
}
);

$(".delete_filter").click(
function(){
   ($(this)).remove(); 
}
);

function startTail(){
clearInterval(interval);
interval = setInterval(
function(){
    $.getJSON("ajax.php?function=tail&pointer=" + pointer + "&nocache=" + new Date(),
        function(data){
            pointer = data.pointer;
            $("#tail_print").append(data.log);
            scroll.scrollTop = scroll.scrollHeight;
        });
}, 1000);
}

PHP code that pulls the tailing info: 提取尾随信息的PHP代码:

function tail(){
    $file = "/path/to/the/log/filelog.log";
    $handle = fopen($file, "r");
    clearstatcache();       

    if ($_REQUEST['pointer'] == '') {
        fseek($handle, -1024, SEEK_END);
    } else {
        fseek($handle, $_REQUEST['pointer']);
    }

    while ($buffer = fgets($handle)) { 
        $log .= $buffer . "<br />\n";
    } 

    $output = array("pointer" => ftell($handle), "log" => $log);
    fclose($handle);

    echo json_encode($output);
}

The whole purpose of this is to allow the user to filter log results. 这样做的全部目的是允许用户过滤日志结果。 So the user performs an action that starts startTail() and $.getJSON() retrieves a JSON object that is built by a PHP function and prints the results. 因此,用户执行启动startTail()的操作,然后$.getJSON()检索由PHP函数构建的JSON对象,并打印结果。 Works flawlessly. 完美地工作。 Now I want to give the user the option to filter the incoming tailing items. 现在,我想为用户提供过滤传入尾矿的选项。 The user clicks a filter button and jQuery takes the filter text and adds it to the keywords array then the data.log from the JSON object is filtered using the keywords array and then appended to the screen. 用户单击一个过滤器按钮,jQuery将过滤器文本添加到keywords数组中,然后使用keywords数组过滤来自JSON对象的data.log ,然后将其附加到屏幕上。

I also have a delete filter function that isn't working. 我还有一个删除过滤器功能不起作用。 Maybe someone can help me with that. 也许有人可以帮助我。

The delete event handler should be deleting the parent td element delete事件处理程序应删除父td元素

$(".delete_filter").click(
  function(){
    $(this).parent().remove(); 
  }
);

You might also want to remove the keyword from the keywords array when you do that. 这样做时,您可能还想从关键字数组中删除关键字。

As for filtering the logs, pass along the keywords array to the server where it can be filtered. 至于过滤日志,请将关键字数组传递到可以对其进行过滤的服务器。 That would save on the datasize returned by the server. 这样可以节省服务器返回的数据大小。 If you need to do client side, provide a sample response to indicate to the structure of the data returned. 如果需要在客户端进行操作,请提供示例响应以指示返回的数据的结构。

It seems like this tool is constantly getting more results. 似乎此工具不断获得更多结果。 You would probably want to filter the results out on server-side to reduce bandwidth and client-side load. 您可能希望在服务器端过滤掉结果,以减少带宽和客户端负载。 You may also want to filter the previous results, which you could do in javascript. 您可能还需要过滤以前的结果,可以使用javascript进行过滤。 Without a little more info about the data, there is little we can recommend. 如果没有有关数据的更多信息,我们几乎没有建议。

I'm not sure what the data looks like in the log, but I'll make up an example: 我不确定日志中的数据是什么样的,但我将组成一个示例:

1/12/2012 login user_name success 2012年1月12日成功登录user_name

1/12/2012 login user_name fail 1/12/2012登录user_name失败

Your keyword might be success (you don't want to see the ones that succeed). 您的关键字可能是成功的(您不想看到成功的关键字)。 So in your while loop, check if the entry has the keywords (success) and only add it if it does not. 因此,在您的while循环中,检查条目是否包含关键字(成功),如果没有则仅添加关键字。

   while ($buffer = fgets($handle)) { 
      //if $buffer does not contain keywords
            $log .= $buffer . "<br />\n";
    } 

excuse my pseudo code, I don't write php. 请问我的伪代码,我不写php。

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

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