javascript - 语义 UI 搜索栏 - 你能切断描述,让它只显示前几个词,但仍然搜索所有词吗?

标签 javascript java spring-boot thymeleaf semantic-ui

语义 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/

相关文章:

java - 运行 java -jar 并指定配置路径

spring - JdbcTemplate 中的 Multi-Tenancy

javascript - 按住按键时忽略重复的键盘事件

javascript - 在 ember 中按属性获取记录

java - 如何在 native 查询 JPA 中传递参数

java - 监控Spring boot应用程序: gather service/node availability data for offline reporting

javascript - 我可以传递引用之类的参数吗

javascript - 如何解决由于多个 jQuery 文件导致的多次回发问题?

java - Spring 验证错误的文档

java - 如何在 Java 面板中添加 JRadioButton 的按钮组或仅在面板中添加 JRadioButton?