data-sly-repeat define max item via cq dialog | Community
Skip to main content
Level 2
January 2, 2019

data-sly-repeat define max item via cq dialog

  • January 2, 2019
  • 3 replies
  • 7483 views

I am trying to use data-sly-repeat and need to define the max item to repeat. I though I could use the following HTL but if i replace the number with properties from the dialog (maxItems - sling:resourceType="granite/ui/components/coral/foundation/form/numberfield"), the component breaks with error Cannot get DefaultSlingScript: Operands are not of the same type: comparison is supported for Number types only.

<sly data-sly-repeat="${reference.items @ begin = 0, end = properties.maxItems}">

Is there a simple way of using only HTL to achieve this? I also tried ${properties.maxItems @ context='number'} but no luck

Thanks in advance.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

3 replies

Ravi_Pampana
Community Advisor
Community Advisor
January 2, 2019

Hi,

You can have Java or JS code to control the size of the list based on the value entered in cq:dialog

Ex:

Add the below JS code in a file

"use strict";
use(function () {
  let n = properties.get("colNum", 0);
  return {
  columns: [...Array(100)] // empty, iterable, array of size n
  };
});

<div
  data-sly-use.config="<JS-file-name>"
  data-sly-list="${config.columns}">
  <div class="6u">
  <h3>Accumsan</h3>
  <ul class="alt">
  <li><a href="#">Nascetur nunc varius</a></li>
  <li><a href="#">Vis faucibus sed tempor</a></li>
  <li><a href="#">Massa amet lobortis vel</a></li>
  <li><a href="#">Nascetur nunc varius</a></li>
  </ul>
  </div>
</div>

Level 2
January 3, 2019

Thanks. I have a Java sling modal that returns the items. I would like to use the HTL only if possible.

arunpatidar
Community Advisor
Community Advisor
January 3, 2019

If the number is replace with the value from the cq dialog, it will return the same error

Cannot get DefaultSlingScript: Operands are not of the same type: comparison is supported for Number types only.

<sly data-sly-test.maxItems="${properties.maxItems @ context='number'}" />

<ul data-sly-list.ref="${reference.items}">

<li data-sly-test="${refList.index <= maxItems}"></li>

</ul>


try to use typehint as Long for number field item.

NumberField — Granite UI 1.0 documentation

Arun Patidar
Gaurav-Behl
Level 10
January 3, 2019

May be its not written to support type conversions and works only within the indices of the collection.

htl-spec/SPECIFICATION.md at master · adobe/htl-spec · GitHub

data-sly-repeat:

  • Iterates over the content of each item in the attribute value and displays the containing element as many times as items in the attribute value, allowing to control the iteration through the following options:
    • begin - iteration begins at the item located at the specified index; first item of the collection has index 0
    • step - iteration will only process every step items of the collection, starting with the first one
    • end - iteration ends at the item located at the specified index (inclusive)

Go with Ravi/Arun's approach.

Feike_Visser1
Adobe Employee
Adobe Employee
January 4, 2019

"Operands are not of the same type: comparison is supported for Number types only."

means not the same type.

Make sure that properties.maxItems is of type of number

Level 2
January 4, 2019

Thanks all for the insightful suggestions.

Will go with Java/JS method since unable to achieve with HTL although the properties.maxItems type was changed to Long.

Feike_Visser1
Adobe Employee
Adobe Employee
January 16, 2019