1

I am working on a Leaflet map that allows a user to open a popup by clicking on a geometry and then generate dynamic content within the popup. However, I find that on closing and then reopening the popup, the dynamic content is lost - I can think of a very lengthy workaround to preserve the content, but I'd like to see if anyone has a suggestion about a more direct way to do this, perhaps using Leaflet's methods... is there any way to preserve a popup's dynamic content when it has been closed and reopened?

Here is a slightly edited version of my code...

map.on('draw:created', function(e){

    //SETS THE BASIC FORMAT, INCLUDING BUTTONS FOR ADDING C
    popUpFields = "<button class='add_content'>Add content</button><div class='content_container'></div><button class='popup_save'>Save</button>";

    $(document).on("click", ".add_content", function(){
        $(this).next(".content_container").append("Hello popup!");
    });

    //ADDS THE LAYER TO THE MAP
    featureGroup.addLayer(e.layer);
    //AUTOMATICALLY OPENS THE POPUP
    e.layer.bindPopup(popUpFields).openPopup();

    e.layer.on('click', function(){
        //IF THE POPUP'S CONTENT HAS BEEN 'SAVED' AS AN ATTRIBUTE OF THE GEOMETRY - IN OTHER WORDS, THE USER HAS EDITED THE POPUP CONTENT
        if(e.layer.hello){
            //THIS IS WHERE A REOPENED POPUP NEEDS TO APPEAR WITH ALL OF THE CONTENT THAT HAD BEEN 'SAVED' - CODE BELOW DOES NOT WORK (OPENS WITH ONLY popUpFields CONTENT, NO ADDED CONTENT, BUT IS ILLUSTRATIVE OF WHAT I'M LOOKING FOR
            e.layer.openPopup();
        }
        else{
            e.layer.bindPopup(popUpFields).openPopup();
        };
    });

    $('.popup_save').click(function(){
        //THE USER SAVES THEIR POPUP CONTENT, WRITING THE ADDED CONTENT TO AN ATTRIBUTE OF THE LAYER     
        e.layer.hello = $(this).prev().html();
    });

});

So in summary, what happens here is that a user adds a geometry, a popup opens, the user clicks the 'Add content' button a few times, appending a few strings to a div in the popup... the user saves those strings as an attribute of the geometry and then closes the popup to do other stuff. Later, they wish to reopen the popup to add more strings, but all of the original added strings have been lost from the view. I would like it so that the added content is not lost when the popup closes. I actually have implemented a bit of a 'hack' solution that rebuilds the popup with the dynamic content every time it is reopened, but I feel that there must be a more elegant solution, where the state of the popup is preserved between closings and reopenings rather than freshly rebuilding it every time. Is there?

3
  • 1
    Do you want the data to be saved only for that session, or do you need the data to persist across users and sessions? Commented Jul 21, 2016 at 5:43
  • Good question - I just want it for the session. If the page is refreshed, the data can be lost - I'm only trying to get it to persist between closings/reopenings of each geometry's popup within the same session.
    – skwidbreth
    Commented Jul 21, 2016 at 14:30
  • How would you this persist permanently? I need to develop this, so that the geojson gets updated by the user. Thanks Commented Jun 20, 2019 at 16:30

1 Answer 1

0

If you want to save the data only for that session, then this can achieved by the following code. I've basically re-arranged the code that you have written, along with the code to show the data in the Popup.

map.on('draw:created', function (e) {

        //ADDS THE LAYER TO THE MAP
        featureGroup.addLayer(e.layer);


        e.layer.on('click', function(){
            //IF THE POPUP'S CONTENT HAS BEEN 'SAVED' AS AN ATTRIBUTE OF THE GEOMETRY - IN OTHER WORDS, THE USER HAS EDITED THE POPUP CONTENT
            if(e.layer.hello){
                //If we have the data, then we will show only the content of the hello variable
                e.layer.bindPopup(e.layer.hello).openPopup();               
            }
            else{
                //we do not have the data; Hence we will show the editing functionality
                //SETS THE BASIC FORMAT, INCLUDING BUTTONS FOR ADDING C
                popUpFields = "<button class='add_content'>Add content</button><div class='content_container'></div><button class='popup_save'>Save</button>";

                //Open the Popup
                e.layer.bindPopup(popUpFields).openPopup();

                //Bind the click event on the Add content Button
                $(document).on("click", ".add_content", function(){
                    $(this).next(".content_container").append("Hello popup!");
                });

                //bind the click event on the save button
                $('.popup_save').click(function(){
                    //THE USER SAVES THEIR POPUP CONTENT, WRITING THE ADDED CONTENT TO AN ATTRIBUTE OF THE LAYER     
                    e.layer.hello = $(this).prev().html();
                });
            };
        });
});
1
  • Sorry I forgot to accept this answer!
    – skwidbreth
    Commented Oct 20, 2017 at 12:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.