0

I'm trying to create a menu which can be used to sort or filter posts from my database, much like how imgur.com sorts their posts. What I'm going for is sorting posts by either newest first or most popular and filtering by category. The sort order I can manage, but I'm not sure how to dynamically change it by which option is selected. Here's some example code.

<html>
<div id="sort-menu">
<span>Sort by</span>
<select>
    <option>Newest first</option>
    <option>Most popular</option>
</select>
<span>in</span>
<select>
    <option>All</option>
    <option>Category1</option>
    <option>Category2</option>
    <option>Category3</option>
</select>
</div>
</html>

1 Answer 1

2

In order to dynamically change the contents of a page, you're going to need to use Javascript and AJAX. With your select box, you need to have an event handler watching for a change in the element's value. On change, you parse that value and make an AJAX call to your server to pull the data you want to display. Upon completion, use that data to populate the content on the page.

Another alternative is you can load all of the data you want, albeit inefficient, and just hide the different content until a user selects the corresponding option in the option box. The problem with this is if you have a lot of content spread among those pages, then there will be a performance issue.

EDIT (with sample code):

<div id='content-one'>
    ...
</div>

<div id='content-two'>
    ...
</div>

<select id='choice'>
    <option value='one'>Content One</option>
    <option value='two'>Content Two</option>
</select>

<script>
    $(function() {
        // When our select changes, we execute a callback
        $('#choice').change(function() {
            var val = $('#choice').val();

            // This implementation can be changed. This is just how I would do it
            $.ajax({
                type: 'GET',
                url: 'path/to/content/' + val; // ie 'path/to/content/one' or 'path/to/content/two'
            }).done(function(data){
                if (val == 'one')
                    // Populate content for one
                else
                    // Populate content for two
            });

            // Hide everything, then show the content that was selected
            $("#content-one, #content-two").hide();
            $("#content-" + val).show();
        });
    });

</script>
Sign up to request clarification or add additional context in comments.

9 Comments

Ok, sounds promising. Can the event handler just be a form with the action being the call? I'm not great with javascript and AJAX though so do you think you could possibly give a quick example of what that may look like? Thanks for your help
sorry, one more thing, I'm getting a syntax error with this line var val = $(#choice).val();
I apologize, the parameter should be a string, like so: $('#choice').val(); My mistake, I'll update the code.
no worries. Now though, I'm getting one here url: 'path/to/content/' + val;
This was just an example. This URL needs to be a server implementation written by you to acquire the data you need for the page.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.