question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can I enable edit mode programatically?

See original GitHub issue

Is there way to enable edit mode programatically? Right now I’m looping through the drawControl toolbars looking for one which has “edit” as an option, and saving it off for later use.

It seems like there should be an cleaner way to do this, but I couldn’t find one.

for(var i in drawControl._toolbars){
    if(typeof drawControl._toolbars[i]._modes.edit != 'undefined'){
        editHandler = drawControl._toolbars[i]._modes.edit.handler;
    }   
}   

// Much later… editHandler.enable(); // Later still… editHandler.disable();

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

16reactions
eaignercommented, Jan 20, 2016

The API seems kind of inflexible. Everything relies on the default toolbar. Would be greatly appreciated if everything could be done in code without any default UI.

5reactions
ghostcommented, Feb 23, 2019

I had some problems with the solution from my last comment (regarding “undo’ing” edit changes) so I came up with another one (which also seems a bit nicer):

// disable the original edit toolbar so that there aren't two ways to edit layers
drawControl = new L.Control.Draw({edit: false});

// manually create an edit toolbar (which won't be visible) when the page loads
// use a new feature group to hold the layer that is being edited (and not the map's
// "real" feature group)
editFeatureGroup = L.featureGroup();
editToolbar = new L.EditToolbar({
  featureGroup: editFeatureGroup
})

// get toolbar edit handler (this should probably be done with a loop like in the OP)
editHandler = editToolbar.getModeHandlers()[0].handler

// this is hacky but needed otherwise enabling the edit mod will fail when the code
// attempts to fire map events for edit start etc
editHandler._map = map

To manually enable editing on a certain layer:

editFeatureGroup.addLayer(layer)
editHandler.enable()

And to disable it:

// revert layers (optional)
editHandler.revertLayers()

// disable layer editing
editHandler.disable()
editFeatureGroup.removeLayer(layer)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to programmatically make a List's editMode inactive ...
You can use .environment to bind the List's current edit mode with a @State property, as shown in this awesome article.
Read more >
Programmatically Entering Edit Mode
At run time cell edit mode is usually entered by the user's mouse and keyboard interaction with the grid. However, if you choose,...
Read more >
Put GridView in Edit Mode Programmatically - MSDN - Microsoft
In the DataSource mode, the GridView doesn't auto-databind for you. If an edit, update, cancel, pager, delete, or sort button is clicked, you ......
Read more >
Set component enabled/disabled programmatically in edit ...
I want to set enabled/disabled some lookup components if a checkbox is checked or not. And refresh if checkbox state changes.
Read more >
Custom button does not trigger editMode but EditButton does
It's hard to test your code as you are not showing many parts used in your code. But if I fill such parts...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found