简体   繁体   中英

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.

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:

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. 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.

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_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. 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

1/12/2012 login user_name fail

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 ($buffer = fgets($handle)) { 
      //if $buffer does not contain keywords
            $log .= $buffer . "<br />\n";
    } 

excuse my pseudo code, I don't write php.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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