Wednesday, February 7, 2018

React Tree using PrimeReact

Long time no post!

Recently I've been struggling to create or locate a decent-looking side menu tree in React.

I had a few requirements:


  • Navigation links, naturally
  • Selected link highlighted by colour
  • Variable row expansion
Turns out most of these are easy enough, once you get the tree expansion bit going.

To do this, I took advantage of the PrimeReact suite of components (https://www.primefaces.org/primereact), specifically the DataTable. This has an expansion capability already.

I realised some simple recursion would work nicely for me. Firstly, a Tree component was needed:

class Tree extends React.Component {
  constructor(props) {
    super(props);
    this.state = {};
    this.rowExpansionTemplate = this.rowExpansionTemplate.bind(this);
  }

  rowExpansionTemplate(subTreeData) {
    return (
      <div style={{paddingLeft: '1em'}}><Tree data={subTreeData.nodes}/></div>
    );
  }

  render() {
    return (
      <DataTable value={this.props.data} expandedRows={this.state.expandedRows}
                 onRowToggle={(e) => this.setState({expandedRows: e.data})}
                 rowExpansionTemplate={this.rowExpansionTemplate}>
        <Column expander={true} style={{width: '2em'}}/>
        <Column field="text"/>
      </DataTable>
    );
  }
}



This sets up a Tree component, displaying the Expander button, and the property called text. When the expander is clicked, the subtree, consisting of the property called nodes, is displayed.

This is called using a simple:

<Tree data={YOURDATAHERE}/>


and the data format is, at minimum:

[
  {
    "text": "fred"
  },
  {
    "text": "group",
    "nodes": [
      {
        "text": "bill"
      },
      {
        "text": "george"
      },
      {
        "text": "simon"
      }
    ]
  }
]


To add things like selection colouring and links can be added simply use a template to render the text column, and add all your requirements in there!

Thursday, April 27, 2017

New Software Alert!


Have you ever needed to find out exactly where a piece of Java software was being used?

I've just released an open-source project to allow someone specify the artefact they're interested in, and provide a Maven repository.

The uility then searches the repo and reports on all artefacts that use the specified artefact, returning a list of uses, along with version information.

It's designed for that situation where you've just released a version with a major fix, or a major improvement, and you need to know which of your other projects need to be updated. It's also great for impact analysis of major change e.g. moving between versions of Junit.

Read all about it, and access the code at :

https://github.com/TrueDub/maven-usage

It should be available in Maven Central too, with the following coordinates:

<groupId>com.castlemon.maven</groupId>
<artifactId>maven-usage</artifactId>
<version>1.0.0.1</version>



Friday, November 6, 2015

First post!!

More to come from me here, mostly around my current interest areas of AngularJS and graph databases, especially Neo4j.

To start with, a link to an excellent tutorial on using Angular's ng-options inside an HTML select.

http://www.undefinednull.com/2014/08/11/a-brief-walk-through-of-the-ng-options-in-angularjs/