mohsen1 / json-formatter

Angular directive for collapsible JSON in HTML
http://azimi.me/json-formatter/demo/demo.html
Other
372 stars 85 forks source link

Any way to dynamically set open? #34

Open ScottChapman opened 8 years ago

ScottChapman commented 8 years ago

I wanted to implement a mechanism where I would expand/collapse all the JSON Formatters with a button. But I couldn't get it to work.

Is there a way to do that?

mohsen1 commented 8 years ago

No, but it's possible. We need to $watch open to update scope.isOpen when it changes. See here

A pull request is welcome. Make sure you write a test for it.

raygig commented 7 years ago

Seems like an easy fix

Replacing https://github.com/mohsen1/json-formatter/blob/master/src/json-formatter.js#L154

with

scope.$watch('open', function(value) {
        scope.isOpen = !!scope.open;
}, false);

I have this working locally and it works well. For now I am copying the json-formatter package locally until this enhancement is merged.

ric79 commented 7 years ago

Hello, any chance to fix the code as proposed? This will be really useful...

mohsen1 commented 7 years ago

I'll merge and publish a small PR that only does that. #41 is way too much changes.

Please send a PR.

ric79 commented 7 years ago

Now idea about PR. I'm just using your module :) Sorry

ric79 commented 7 years ago

Is it possible to fix it. It Will be rally usefull forum my progetto. Thank you!

mohsen1 commented 7 years ago

@ric79 pull requests are welcome! :)

nicolamontini commented 5 years ago

@raygig I modified the library with your suggestion, but now how can i trigger the watcher? I tried to bind the open attribute of the directive and then changing the data model, but the watcher does not get triggered. I did something like this:

<json-formatter open="jsonDepth" json="jsonObj"></json-formatter> <button ng-click="changeOpen()">CHANGE OPEN</button>

and in my controller:

$scope.jsonObj = my object; $scope.jsonDepth = 10; $scope.changeOpen = function() { $scope.jsonDepth = 1; };

The changeOpen function is triggered, but not the watcher. Can someone help? Many thanks in advance