简体   繁体   中英

capturing and updating variable values after ajax request in url

I would really appreciate some help on this. I have a page that shows products in a store using laravel pagination. I have filters on the page based on brands, category, and available products. for filtering the products I am using a checkbox. if a checkbox is checked I use ajax get request and send status via URL to a controller to filter available products.

status = 1 is for available products, and status = 0 is for all products.Url is looks like this:

/Collections/Newest_Items?status=1&page=2

Here is the situation. I want to know if is it possible to change the variable value in URL and regenerate the URL base on the page number and new filters dynamically? Is it a way to get the URL of the page using jquery and change the values and then change the Url with window.history.pushState("", "", URL); ? Here is my ajax:

       $(document).on('click', "#only_available", function () {

                if ($('#only_available').is(':checked')) {
                    var status = 1;
                    url = '/Collections/Newest_Items?status='+status;
                } else {
                    var status = 0;
                    url = '/Collections/Newest_Items';
                }

                window.history.pushState("", "", url);

                $.ajax({
                    url: '/Collections/Newest_Items',
                    type: "GET",
                    data: {status: status},
                    cash: false,
                    success:
                        function (response) {
                            $('#products-load').html(response);
                        }

                });
            });
        });

I do this by writing the URL by myself. In this situation, I must write the URL after every filter applied to the page. this way I cant get the page the user currently in and it goes back to the first page. But what I want to achieve here is, I want to make the Url dynamically with page number the user currently on with all filters applied to it.

You can use window.location.search which will give you something like: status=1&page=2 in your example. Then you will need to parse out those variables to get the page number you're looking for.

Ok I think I understand what you are asking for. So with each unique filter event that you are firing you need to query the current url before pushstate and get the values with something like this.

For instance if someone clicks Brand then you would get the new brand variable as well as the current status and page variables to pass with ajax like this

also just POST it instead of GET

$(document).on('click', ".brand", function () {
    var brand = $(this).attr('id);

    //Example how to use it: 
    var params = parseQueryString();
    var status = params["status"]);
    var page = params["page"]);
    // if you have more variables than this then you would add them here and make sure you pass them along to the ajax data.

    url = '/Collections/Newest_Items?status='+status+'&page='+page+'&brand='+brand;

    window.history.pushState("", "", url);

    $.ajax({
        url: '/Collections/Newest_Items',
        type: "POST",
        data: {status: status, page: page, brand: brand},
        cash: false,
        success:
            function (response) {
                $('#products-load').html(response);
            }
    });
});

var parseQueryString = function() {

    var str = window.location.search;
    var objURL = {};

    str.replace(
        new RegExp( "([^?=&]+)(=([^&]*))?", "g" ),
        function( $0, $1, $2, $3 ){
            objURL[ $1 ] = $3;
        }
    );
    return objURL;
};

tnx to @CesarBielich and @Sokies I finally manage to solve the problem. they give me part of the answer but not all.I made it unique to my question:

what we need here is the path and the parameters that nested in URL. so for getting the path of the route, we must use window.location.pathname and for getting all the parameters must use window.location.search . after that, we must combine the path and params so that the URL comes out of it. then we must add the new parameter like status after that. So that all the parameters can be accessed by the controller. both the old params and the new one. this way laravel pagination knows what url to make, in the href links to other pages.

        $(document).on('click', "#only_available", function () {

                if ($('#only_available').is(':checked')) {
                    var status = 1;
                } else {
                    var status = 0;
                }

                var params = window.location.search;
                var path = window.location.pathname;

                var old_url = path+params;
               var url = old_url+'&status=' + status;

                window.history.pushState("", "", url);

                $.ajax({
                    url: url,
                    type: "GET",
                    cash: false,
                    success:
                        function (response) {
                            $('#products-load').html(response);
                        }

                });
            });
        });

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