简体   繁体   中英

how to display searched json records one by one through next and previous buttons?

I have a working code which searches the json records and displays all the records one by one. I want to display the records based on the search term,

The working code is following:

    <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery PHP Json Response</title>
<style type="text/css">
div
{
text-align:center;
padding:10px;
}

#msg {
width: 500px;
margin: 0px auto;
}
.members {
width: 500px ;
background-color: beige;
}
</style>
</head>
<body>

<input type="text" id="search-json-input" />
<input type="button" id="search-json-submit" value="search" />
<br/>
<br/>

<input type="button" name="next" id="next" value="NEXT" />
<br/>
<input type="button" name="previous" id="previous" value="PREV" />
<br/>
<div id="msg">
    <table id="userdata" border="1">
        <thead>
            <th>Email</th>
            <th>Sex</th>
            <th>Location</th>
            <th>Picture</th>
            <th>audio</th>
            <th>video</th>
        </thead>
        <tbody></tbody>
    </table>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script type="text/javascript">

var users = [];
var idx = 0;
var renderRow = function (idx) {
    if (idx < 0) idx = 0;
    if (idx > (users.length - 1)) idx = (users.length - 1);
    var user = users[idx];
    var tblRow = "<tr>" + "<td>" + user.email + "</td>" + "<td>" + user.sex + "</td>" + "<td>" + user.location + "</td>" + "<td>" + "<img src=" + user.image + ">" + "</td>" + "<td>" + "<audio src=" + user.video + " controls>" + "</td>" + "<td>" + "<video src=" + user.video + " controls>" + "</td>" + "</tr>";
    $('#userdata tbody').html(tblRow);
};
var url = "json.php";
$.getJSON(url, function (data) {
    users = data.members;
    renderRow(idx);
    $('#next').click(function() {
        idx++;
        renderRow(idx);
    });
    $('#previous').click(function() {
        idx--;
        renderRow(idx);
    });
});

</script>
</body>
</html>

The result of json.php can be seen here: http://sco7.com/components/phonegap/json.php

  • Attach the JSON response as a variable to something, so it can be referenced later.
    • Warning if you have >1000 items this approach doesn't work very well, as its too slow to return larger datasets.
  • Create a current index variable (an int).
  • Remove the loop in the response handler (ie the call to each() )
  • Update your document to have a "left button", and "right button" and the 0th item
    • The buttons must not submit anything to a server.
    • The appended item may have the same formatting as what you have at present
  • The created buttons have an onclick handler that:

    • Repeat until valid changed index

      • For left, confirms if the index is valid if decremented, then decrements it.
      • For right, confirms if the index is valid if incremented, then increments it.
      • Does your data validation (the regex) as current.
      • If incorrect data change the index again.
    • Deletes the current displayed item text, and appends the item at the updated index.

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