0

I have a url with an array in it as parameter. For example:

test.dev/orders?filter[]=temp&filter[]=placed

I want to create a js function that can toggle the contents of the array and redirects after the modification.

For example, if i call a function toggleFilter('temp') i want the js to redirect to test.dev/orders?filter[]=placed.

But if i call toggleFilter('processed') i want to have the js redirect to test.dev/orders?filter[]=temp&filter[]=placed&filter[]=processed.

How can i make such function? Thanks in advance!

1
  • please make things clear what you wanna achieve. May be a more elaborate example Commented Mar 16, 2016 at 15:04

2 Answers 2

1

The function needs to get the current query string, break it down, and then based on the parameters, re-assemble it.

document.location.search - gives you the query string easily enough.

String.split - lets you break it down into its parts.

arguments - lets use this instead of defined parameters so you can pass as many (or as few) filters as you like.

From there, its just interpreting the parameters to assemble a new query string...

function toggleFilter() {
    var queryString = document.location.search;
    queryString = queryString.substring(1); // Get rid of the initial '?'

    // Break it into individual parts and remove the 'filter[]=' leaving just the values
    var queryStringSplit = queryString.split('&');
    var values = [];
    for (var qss = 0; qss < queryStringSplit.length; qss++) {
        var value = queryStringSplit[qss];
        value = value.split('=')[1];

        // This will remove any "blank" values (like 'filter[]=&...")
        if (value)
            values.push(value);
    }

    // Add / remove values in arguments
    for (var a = 0; a < arguments.length; a++) {
        var arg = arguments[a];
        var index = values.indexOf(arg);
        if (index == -1)
            values.push(arg);
        else
            values.splice(index, 1);
    }

    // Re-assemble the new query string
    queryString = ''; // Default to blank
    if (values.length)
        queryString = 'filter[]=' + values.join('&filter[]=');

    // Redirect to new location
    location.search = queryString;
}

Having said that, I think that if you want to make this any kind of reusable, I would call it something like toggleQueryString and modify the logic to interpret the first argument as the name of the queryString value to toggle (so you could do things other than "filter[]") and I would not have it set the location, but instead return a query string value that you could do whatever you want with. But this function should do the trick.

Sign up to request clarification or add additional context in comments.

Comments

-1
function toggleFilter(url, key) {
    var result;
    if(url.indexOf('filter[]=' + key) > -1)
        result = url.replace('filter[]=' + key + '&', '').replace('filter[]=' + key, '');
    else
        result = url + (url.indexOf('?') > -1 ? '&' : '?') + 'filter[]=' + key;
    result = result.indexOf('?') == result.length - 1 ? 
        result.substring(0, result.length - 1) : 
        result;
    return result;
    // or if you want to redirect just write window.location.href = result;
}

Comments

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.