能帮我吗? 我想显示产品中的前3个对象,但不是必须的。 我尝试使用百里香叶序列,但它不起作用。也许有人可以提示我该怎么做。
HTML:
beforeEach(() => {
return Promise.all([
mongo.db('mydb').collection('myCollection1').deleteMany({}),
mongo.db('mydb').collection('myCollection2').deleteMany({})
]) // close the promise.all here
.then(() => collections.engines().insertOne(exampleObject)) // close `then` here
.then((value) => {
foo = value.ops[0];
return Promise.resolve();
});
});
控制器:
<th:block th:each="product:${products}">
<a th:class="production_Page" th:href="@{'product/'+${product.id}}"> <p
th:text="${product.productName}"/></a>
<a th:class="production_Page"
th:href="@{'productDelete/'+${product.id}}">Delete</a>
<a th:class="production_Page"
th:href="@{'productEdit/'+${product.id}}">Edit</a>
<img th:class="productImage" th:src="${product.pathImage}"/>
<br/>
</th:block>
如果有人可以提示我这个问题,那就太好了。
谢谢。
答案 0 :(得分:3)
由于products
是Product
的列表,因此您必须遍历该列表。在百里香上,您可以使用th:each
属性进行迭代。因此,对于您的情况,您可以使用以下内容。试试看。
<th:each="product,iterStat: ${products}" th:if="${iterStat.index} <3">
我不确定,但是根据您的问题,您只需要前三个对象。为此,您可以使用在th:each
中定义的状态变量。您可以找到here来获得更多详细信息。
答案 1 :(得分:0)
这是使用#{numbers}
上下文对象进行操作的方式。
<th:block th:each="i: ${#numbers.sequence(0, 2)}" th:with="product=${products[i]}">
<a th:class="production_Page" th:href="@{'product/'+${product.id}}">
<p th:text="${product.productName}"/>
</a>
<a th:class="production_Page" th:href="@{'productDelete/'+${product.id}}">Delete</a>
<a th:class="production_Page" th:href="@{'productEdit/'+${product.id}}">Edit</a>
<img th:class="productImage" th:src="${product.pathImage}"/>
<br/>
</th:block>