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

update MMM-JsonTable.js #93

Closed
wants to merge 10 commits into from
28 changes: 27 additions & 1 deletion MMM-JsonTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Module.register("MMM-JsonTable", {
defaults: {
url: "",
arrayName: null,
// Note: The arrayName string is case-sensitive! Ensure that the path provided matches the exact case of the keys in the JSON data.
noDataText:
"Json data is not of type array! Maybe the config arrayName is not used and should be, or is configured wrong.",
keepColumns: [],
Expand Down Expand Up @@ -58,9 +59,34 @@ Module.register("MMM-JsonTable", {
const table = document.createElement("table");
const tbody = document.createElement("tbody");

function resolveDataPath(data, path) {
// If path is a string and doesn't contain '.'
if (typeof path === "string" && !path.includes(".")) {
return data[path];
}
// If path is a string with '.' indicating a deeper path,
let pathArray;
if (typeof path === "string" && path.includes(".")) {
pathArray = path.split(".");
}

// If path is array, reduce it to resolve nested data
if (Array.isArray(pathArray)) {
return pathArray.reduce(
(obj, key) => (obj && obj[key] !== undefined ? obj[key] : null),
data
);
}

// Fallback (unexpected format)
// console.error("Unexpected path format:", path);
return null;
}

let items = [];

if (this.config.arrayName) {
items = this.jsonData[this.config.arrayName];
items = resolveDataPath(this.jsonData, this.config.arrayName);
} else {
items = this.jsonData;
}
Expand Down
87 changes: 86 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# MMM-JsonTable

All credit to @timdows her.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Credits are good and would be appropriate for a fork. But here I don't think it should be listed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry never ment readme for PR

just added the functionality to dive in to a second array

A module for the [MagicMirror²](https://github.com/MichMich/MagicMirror) project which creates a table filled with a list gathered from a json request.

All the variables of the objects in the array are represented by a table column.
Expand Down Expand Up @@ -237,13 +240,95 @@
position: 'top_right',
header: 'HouseDB Kwh Statistics',
config: {
url: 'https://xyz/abc/get.json',

url: 'https://xyz/abc/get.json',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What for is this change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readme should not be pulled

arrayName: 'deviceKwhUsages',
descriptiveRow: '<tr><td>Name</td><td>Today</td><td>ThisWeek</td><td>LastWeek</td><td>ThisMonth</td><td>LastMonth</td></tr>'
}
}
```

Getting the data under "Forbrok"

```

Check failure on line 253 in README.md

View workflow job for this annotation

GitHub Actions / build (18.x)

Fenced code blocks should have a language specified [Context: ```]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add json after the backticks.

{
"status": "success",
"url": "/soppel",
"method": "GET",
"data": [
{
"Forbrok": [
{
"deviceName": "Power consumption",
"currentValue": "Now: 3009 watts",
"currentTarget": " ",
"lastUpdate": "Limit: 5000 watt"
},
{
"deviceName": "Varmepumpe",
"currentValue": "Now:25.8°C,",
"currentTarget": "Target:21°C,",
"lastUpdate": "Home override: true"
},
{
"deviceName": "Hotwater",
"currentValue": "Now:44.38°C,",
"currentTarget": "Last heat complete",
"lastUpdate": "66.9 : 05:46 @ 19-12-2022 "
},
{
"deviceName": "Last update",
"currentValue": " ",
"currentTarget": " ",
"lastUpdate": "23:27 19-12-2022"
}
]
},
{
"mren": [
{
"avfallstype": "Restavfall",
"dato": "30-12-2022"
},
{
"avfallstype": "Matavfall",
"dato": "30-12-2022"
},
{
"avfallstype": "Papiravfall",
"dato": "30-12-2022"
},
{
"avfallstype": "Plastemballasje",
"dato": "30-12-2022"
},
{
"avfallstype": "Glass- og metallemballasje",
"dato": "30-12-2022"
}
]
}
]
}
```

Configuration:

```javascript
{
module: 'MMM-JsonTable',
position: 'top_left',
header: 'Homey Local API',
config: {
url: 'https://xyz/abc/get.json',
arrayName: 'Data',
arrayName2: 'Forbrok',


}
}
```

## Developer hints

Please use `npm run test` before doing a PR.
Loading