cancel
Showing results for 
Search instead for 
Did you mean: 

How to make a table of result?

Ooker
Node

Below is a sample code from @mdfrenchman in the thread Response in the form of table/columns + data instead of records. But when I change the query to MATCH (n) RETURN n.name it returns nothing. Does anyone have a working code that is willing to share?

<html>
  <header>
    <!-- https://github.com/neo4j/neo4j-javascript-driver/blob/4.0/README.md -->
    <script src="https://unpkg.com/neo4j-driver"></script>
  </header>
  <table>
    <thead>
      <tr id="header"></tr>
    </thead>
    <tbody id="body"></tbody>
  </table>
  <script type="text/javascript">
    let header = document.getElementById("header");
    let body = document.getElementById("body");

    example();

    async function example() {
      const driver = neo4j.driver(
        "bolt://localhost:7687",
        neo4j.auth.basic("neo4j", "graph")
      );
      const session = driver.session();
      try {
        const result = await session.run("MATCH (n) RETURN n");

        // Assumes all nodes returned have the same properties as the first one returned.
        Object.keys(result.records[0].get(0).properties).forEach((key) => {
          let colHead = document.createElement("th");
          let headerText = document.createTextNode(key);
          colHead.appendChild(headerText);
          header.appendChild(colHead);
        });

        // There are nicer ways to do this. This is just a hacked together example.
        result.records.forEach((record) => {
          let row = document.createElement("tr");
          Object.values(record.get(0).properties).forEach((val) => {
            let cell = document.createElement("td");
            let text = document.createTextNode(val);
            cell.appendChild(text);
            row.appendChild(cell);
          });
          body.appendChild(row);
        });
      } finally {
        await session.close();
      }
      await driver.close();
    }
  </script>
</html>

1 REPLY 1

You have to change all the processing as you don't have a node with properties anymore.

// create single header
          let colHead = document.createElement("th");
          let headerText = document.createTextNode("n.name");
          colHead.appendChild(headerText);
          header.appendChild(colHead);

// create one cell for each row
          result.records.forEach((record) => {
          let row = document.createElement("tr");
            let cell = document.createElement("td");
            let text = document.createTextNode(record.get("n.name").asString());
            cell.appendChild(text);
            row.appendChild(cell);
          body.appendChild(row);
        });