Displaying multifield using Sightly and JavaScript | Community
Skip to main content
medhik
Level 2
Jun 9, 2017 11:36 AM UTC
Solved

Displaying multifield using Sightly and JavaScript

Hey,

I'm looking at a way using Sightly to read data from a node with properties as followed.

Name: footercol1

Type: String[]

Value: {"key1":"value1","key2","value2"},{"key1":"value3","key2","value4"}.

I'm looking a JavaScript way instead of Java to do that.

Using data-sly-list, so far I'm able to print the whole JSON string so far i.e.

Looking forward to some help here.

Thanks.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Feike_Visser1
Feike_Visser1Accepted solution
Employee
Jun 10, 2017 1:12 PM UTC

First: Your JSON is invalid, make sure to fix that first.

I have made an example on how the deal with this in Javascript.

HTL:

<ul data-sly-use.jsonprocessing="json.js" data-sly-list="${jsonprocessing.keys}">

   <li>${item}</li>

</ul>

Javascript:json.js

   use(function () {

    var jsondata = currentPage.properties.get("jsondata");

    var jsonlength = currentPage.properties.get("jsondata").length;

    var keys = [];

    for (var i=0; i < jsonlength; i++) {

        keys.push(JSON.parse(jsondata[i]).key1);

    }

    return {

        keys : keys

    };

});

Happy programming!

3 replies

Feike_Visser1
Feike_Visser1Accepted solution
Employee
Jun 10, 2017 1:12 PM UTC

First: Your JSON is invalid, make sure to fix that first.

I have made an example on how the deal with this in Javascript.

HTL:

<ul data-sly-use.jsonprocessing="json.js" data-sly-list="${jsonprocessing.keys}">

   <li>${item}</li>

</ul>

Javascript:json.js

   use(function () {

    var jsondata = currentPage.properties.get("jsondata");

    var jsonlength = currentPage.properties.get("jsondata").length;

    var keys = [];

    for (var i=0; i < jsonlength; i++) {

        keys.push(JSON.parse(jsondata[i]).key1);

    }

    return {

        keys : keys

    };

});

Happy programming!

Translate
smacdonald2008
Level 10
Jun 9, 2017 2:34 PM UTC

Why not use Java?

Translate
Feike_Visser1
Employee
Jun 9, 2017 11:48 AM UTC

What is your expected output?

Translate