Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Are you planning to create an Angular 2 component with similar functionality? #62

Open
ghost opened this issue Oct 17, 2016 · 6 comments

Comments

@ghost
Copy link

ghost commented Oct 17, 2016

No description provided.

@mohsen1
Copy link
Owner

mohsen1 commented Oct 17, 2016

For Angular 2 create a simple component in your codebase that uses JSON Formatter JS.

@mhalttu
Copy link

mhalttu commented Mar 28, 2017

Could somebody post an example of such a component here?

@jwhitmarsh
Copy link

@mhalttu loosely something like:

import { Component, OnChanges, Input, ElementRef, ViewChild, Renderer } from '@angular/core';
import { isObject } from 'rxjs/util/isObject';
import { isArray } from 'rxjs/util/isArray';
import JSONFormatter from 'json-formatter-js';

@Component({
  selector: 'json-viewer',
  template: `<div #jsonViewer></div>`,
  styleUrls: ['./json-viewer.component.sass']
})
export class JsonViewerComponent implements OnChanges {
  @ViewChild(`jsonViewer`) input: ElementRef;

  @Input() json: Array<any> | Object | any;

  constructor() { }

  ngOnChanges() {
    // Do nothing without data
    if (!isObject(this.json) && !isArray(this.json)) {
      return;
    }

    const formatter = new JSONFormatter(this.json);
    this.input.nativeElement.appendChild(formatter.render());
  }
}

@alexzuza
Copy link

alexzuza commented Aug 3, 2017

Here is an example for angular2 https://plnkr.co/edit/6Cbs6pIPqGlXM0hmXF9n?p=preview. If Mohsen Azimi doesn't mind i can make it as library

@IAMtheIAM
Copy link

@alexzuza Very AWESOME! Thank you. While I was able to get it to work in Angular 2 using basic document.body.appendChild() this is not the proper way for an angular app. Using you plunker, I copied the code and it works PERFECTLY.

Maybe you can release this as an NPM package for easy consumption. Very good work to @mohsen1 for creating the original code as well!!! It was hard to find a library that could take plain JSON and convert it into a tree view with expand/collapse - they all wanted custom name properties which means they don't work with plain JSON.

Thanks guys.

@IAMtheIAM
Copy link

@jwhitmarsh Great minimal example - except I had to use ngAfterViewInit{} lifecycle hook to get it to render instead of ngOnChanges - what is ngOngChanges going to do the way you suggested it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants