22、HTL Sightly (一)data-sly-list data-sly-repeat

2024-03-06 10:10
文章标签 list data 22 repeat htl sightly sly

本文主要是介绍22、HTL Sightly (一)data-sly-list data-sly-repeat,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 22、HTL Sightly (一)data-sly-list & data-sly-repeat
    • data-sly-list
      • 显示列表中的一部分数据
    • data-sly-repeat
    • data-sly-repeat与data-sly-list的区别

22、HTL Sightly (一)data-sly-list & data-sly-repeat

在HTL模板语言中,有大量的API可供使用,在这一章中主要来讲解如何在HTL页面中使用data-sly-list和data-sly-repeat来展示数据列表。

data-sly-list

使用data-sly-list来展现一个数据列表,创建HTLSightly.java接口和HTLSightlyImpl.java实现类,与之前章节中讲到的SlingModel创建方式一致

package com.adobe.aem.guides.wknd.core.models;import com.adobe.aem.guides.wknd.core.models.dto.Book;import java.util.List;public interface HTLSightly {List<Book> getBooks();
}
package com.adobe.aem.guides.wknd.core.models.impl;import com.adobe.aem.guides.wknd.core.models.HTLSightly;
import com.adobe.aem.guides.wknd.core.models.dto.Book;
import com.adobe.cq.export.json.ExporterConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;import java.util.ArrayList;
import java.util.List;@Slf4j
@Model(adaptables = {SlingHttpServletRequest.class, Resource.class},adapters = {HTLSightly.class},resourceType = {HTLSightlyImpl.RESOURCE_TYPE},defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class HTLSightlyImpl implements HTLSightly {public final static String RESOURCE_TYPE = "wknd-guides/components/htlsightly";@Overridepublic List<Book> getBooks() {List<Book> books = new ArrayList<>();books.add(Book.builder().name("AEM").price(11.11).build());books.add(Book.builder().name("Java").price(22.22).build());books.add(Book.builder().name("JavaScript").price(33.33).build());return books;}
}

创建实体类Book.java

package com.adobe.aem.guides.wknd.core.models.dto;import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Accessors(chain = true)
public class Book {private String name;private double price;}

对话框中的ID没有实际作用,在后期可以通过手动维护ID类达到在页面上获取为一组件元素的功能

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:granite="http://www.adobe.com/jcr/granite/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"jcr:primaryType="nt:unstructured"jcr:title="list and repeat"sling:resourceType="cq/gui/components/authoring/dialog"><contentjcr:primaryType="nt:unstructured"sling:resourceType="granite/ui/components/coral/foundation/container"><items jcr:primaryType="nt:unstructured"><tabsjcr:primaryType="nt:unstructured"sling:resourceType="granite/ui/components/coral/foundation/tabs"maximized="{Boolean}false"><items jcr:primaryType="nt:unstructured"><propertiesjcr:primaryType="nt:unstructured"jcr:title="Properties"sling:resourceType="granite/ui/components/coral/foundation/container"margin="{Boolean}true"><items jcr:primaryType="nt:unstructured"><columnsjcr:primaryType="nt:unstructured"sling:resourceType="granite/ui/components/coral/foundation/fixedcolumns"margin="{Boolean}true"><items jcr:primaryType="nt:unstructured"><columnjcr:primaryType="nt:unstructured"sling:resourceType="granite/ui/components/coral/foundation/container"><items jcr:primaryType="nt:unstructured"><idjcr:primaryType="nt:unstructured"sling:resourceType="granite/ui/components/coral/foundation/form/textfield"fieldLabel="ID"name="./id"/></items></column></items></columns></items></properties></items></tabs></items></content>
</jcr:root>

组件配置信息

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0"xmlns:jcr="http://www.jcp.org/jcr/1.0"jcr:primaryType="cq:Component"jcr:title="list and repeat"jcr:description="list and repeat"componentGroup="Steven Group" />

组件页面信息

  • data-sly-list.book:data-sly-list为遍历集合数据的API,book为集合中的单个元素名称,可自定义
  • bookList.index:当使用data-sly-list来遍历集合数据时,默认会创建一个*List,*为元素名称,index可以获取到下标值
  • bookList.count:可以获取到下标+1
  • bookList.first:是否为第一个数据
  • bookList.middle:是否为中间的数据
  • bookList.last:是否为最后一个数据
  • bookList.odd:是否为奇数个数据
<div class="cq-placeholder cmp-title" data-emptytext="${component.title}:Click to configure" data-sly-unwrap="${!wcmmode.edit}"></div><sly data-sly-use.model="com.adobe.aem.guides.wknd.core.models.HTLSightly"><div data-sly-list.book="${model.books}"><h1>下标:${bookList.index}</h1><h1>序号:${bookList.count}</h1><h2>书名:${book.name}</h2><h2>价格:${book.price}</h2></div>
</sly>

创建页面新增list repeat组件,效果如下

在这里插入图片描述

显示列表中的一部分数据

如果只想显示列表中的一部分该如何做呢?可以使用@符号增加操作变量,修改代码如下,下标从0开始,到1结束,就是显示前两个内容

<div data-sly-list.book="${model.books @ begin = 0, end = 1}">

刷新页面,只看到了前两条数据

请添加图片描述

如果只想显示偶数下标的数据,可以添加step参数,代码如下

<div data-sly-list.book="${model.books @ begin = 0, step=2, end = 2}">

请添加图片描述

data-sly-repeat

data-sly-repeat与data-sly-list使用方式一样,修改如下代码

<div class="cq-placeholder cmp-title" data-emptytext="${component.title}:Click to configure" data-sly-unwrap="${!wcmmode.edit}"></div><sly data-sly-use.model="com.adobe.aem.guides.wknd.core.models.HTLSightly"><h1>==== data-sly-list =====</h1><div  class="list" data-sly-list.book="${model.books @ begin = 0, step=2, end = 2}"><h1>下标:${bookList.index}</h1><h1>序号:${bookList.count}</h1><h2>书名:${book.name}</h2><h2>价格:${book.price}</h2></div><h1>==== data-sly-repeat =====</h1><div class="repeat" data-sly-repeat.book="${model.books}"><h1>下标:${bookList.index}, 序号:${bookList.count}, 第一个:${bookList.first}, 最后一个:${bookList.last}</h1><h2>书名:${book.name}</h2><h2>价格:${book.price}</h2></div>
</sly>

查看页面显示效果

请添加图片描述

data-sly-repeat与data-sly-list的区别

data-sly-repeat与data-sly-list显示效果是一样的,那么它们有什么区别?

查看页面源码会发现,两者在页面的渲染方式不同。

  • data-sly-list:会把所有的元素和属性都放在class为list的div元素中
  • data-sly-repeat:会把class为repeat的div元素复制一份,再把元素内容渲染在div中

请添加图片描述

这篇关于22、HTL Sightly (一)data-sly-list data-sly-repeat的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/779707

相关文章

C#之List集合去重复对象的实现方法

《C#之List集合去重复对象的实现方法》:本文主要介绍C#之List集合去重复对象的实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C# List集合去重复对象方法1、测试数据2、测试数据3、知识点补充总结C# List集合去重复对象方法1、测试数据

Python中合并列表(list)的六种方法小结

《Python中合并列表(list)的六种方法小结》本文主要介绍了Python中合并列表(list)的六种方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋... 目录一、直接用 + 合并列表二、用 extend() js方法三、用 zip() 函数交叉合并四、用

Java List排序实例代码详解

《JavaList排序实例代码详解》:本文主要介绍JavaList排序的相关资料,Java排序方法包括自然排序、自定义排序、Lambda简化及多条件排序,实现灵活且代码简洁,文中通过代码介绍的... 目录一、自然排序二、自定义排序规则三、使用 Lambda 表达式简化 Comparator四、多条件排序五、

Java使用Stream流的Lambda语法进行List转Map的操作方式

《Java使用Stream流的Lambda语法进行List转Map的操作方式》:本文主要介绍Java使用Stream流的Lambda语法进行List转Map的操作方式,具有很好的参考价值,希望对大... 目录背景Stream流的Lambda语法应用实例1、定义要操作的UserDto2、ListChina编程转成M

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

java streamfilter list 过滤的实现

《javastreamfilterlist过滤的实现》JavaStreamAPI中的filter方法是过滤List集合中元素的一个强大工具,可以轻松地根据自定义条件筛选出符合要求的元素,本文就来... 目录1. 创建一个示例List2. 使用Stream的filter方法进行过滤3. 自定义过滤条件1. 定

如何通过Golang的container/list实现LRU缓存算法

《如何通过Golang的container/list实现LRU缓存算法》文章介绍了Go语言中container/list包实现的双向链表,并探讨了如何使用链表实现LRU缓存,LRU缓存通过维护一个双向... 目录力扣:146. LRU 缓存主要结构 List 和 Element常用方法1. 初始化链表2.

HTML5 data-*自定义数据属性的示例代码

《HTML5data-*自定义数据属性的示例代码》HTML5的自定义数据属性(data-*)提供了一种标准化的方法在HTML元素上存储额外信息,可以通过JavaScript访问、修改和在CSS中使用... 目录引言基本概念使用自定义数据属性1. 在 html 中定义2. 通过 JavaScript 访问3.

python中列表list切分的实现

《python中列表list切分的实现》列表是Python中最常用的数据结构之一,经常需要对列表进行切分操作,本文主要介绍了python中列表list切分的实现,文中通过示例代码介绍的非常详细,对大家... 目录一、列表切片的基本用法1.1 基本切片操作1.2 切片的负索引1.3 切片的省略二、列表切分的高

java两个List的交集,并集方式

《java两个List的交集,并集方式》文章主要介绍了Java中两个List的交集和并集的处理方法,推荐使用Apache的CollectionUtils工具类,因为它简单且不会改变原有集合,同时,文章... 目录Java两个List的交集,并集方法一方法二方法三总结java两个List的交集,并集方法一