语义 UI 的新手。我的应用程序使用 Java、Springboot 和 Thymeleaf。搜索功能真的很方便。它在代码中的设置方式使得它可以搜索标题、描述和 url。这很好,除了当你输入一个词时它会显示整篇文章的内容。有没有办法将显示的内容减少到只有前几个字? 正则表达式、拼接切片、maxLength……都试过了,好像都不行。有什么想法吗?
<script th:inline="javascript">
$(document).ready(function(){
var content = [
<th:block th:each="topic : ${topics}">
<th:block th:each="article : ${topic.articles}" >
{ title: [[${article.title}]], description: [[${article.contentText}]], url: [[${"/article/"+topic.id+"/"+article.id}]], },
</th:block>
</th:block>
];
$(function() {
$('.ui.search').search({
source: content
});
});
});
</script>
最佳答案
通常,在使用 Thymeleaf 时,您可以使用 #strings.abbreviate
来剪切文本。我在我的 html 中使用过它,但从未在脚本中使用过,但也许它可以工作。试试这个。
<th:block th:each="topic : ${topics}">
<th:block th:each="article : ${topic.articles}" >
{ title: [[${article.title}]], description: [[${#strings.abbreviate(article.contentText, 50)}]], url: [[${"/article/"+topic.id+"/"+article.id}]], },
</th:block>
</th:block>
这应该削减您的描述,只留下前 50 个字符。
关于javascript - 语义 UI 搜索栏 - 你能切断描述,让它只显示前几个词,但仍然搜索所有词吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53160167/