简体   繁体   English

在动态填充表thymeleaf spring mvc中使用List.js

[英]Using List.js in dynamic populated table thymeleaf spring mvc

I am having trouble in using List.js in dynamically populated table. 我在动态填充的表中使用List.js遇到麻烦。 My project is in spring mvc with thymeleaf as a templating engine. 我的项目是将百里香叶作为模板引擎的春季mvc。 When I used List.js with the existing or static table, it worked fine but at the same when I use it for the table that is populated dynamically, it doesn't work. 当我将List.js与现有表或静态表一起使用时,它可以正常工作,但是当我将其用于动态填充的表时,它却无法正常工作。

Following is the thing i have done: 以下是我所做的事情:

notification.html notification.html

<div id="box-body pad table-responsive">
    <input class="search" placeholder="Search" />
    <button class="sort" data-sort="name">Sort by ID</button>
    <table class="table table-bordered text-center">
        <thead>
            <tr>
                <th>ID</th>
                <th>DUMP FILE</th>
                <th>LOG</th>
                <th>DATE</th>
                <th>MESSAGE</th>
            </tr>
            </thead>
            <tbody class = "list">
            <tr th:each="searchNotification : ${backupSearchNotification}">
                <td class = "name" th:text="${searchNotification.id}"></td>
                <td class = "dumpfile" th:text="${searchNotification.dumpfile}"></td>
                <td class = "log" th:text="${searchNotification.log}"></td>
                <td class = "endtime" th:text="${searchNotification.endtime}"></td>
                <td class = "status" th:if="${searchNotification.status == 0}"><span class="label label-default" style="width: 50px;"><b>Created</b></span></td>
                <td class = "status" th:if="${searchNotification.status == 1}"><span class="label label-primary"><b>Running</b></span></td>
                <td class = "status" th:if="${searchNotification.status == 2}"><span class="label label-success"><b>Completed with
                                                        Success</b></span></td>
                <td class = "status" th:if="${searchNotification.status == 3}"><span class="label label-info" style="width: 50px;"><b>Completed
                                                        with MDHASH</b></span></td>
                <td class = "status" th:if="${searchNotification.status == 100}"><span class="label label-danger" style="width: 50px;"><b>Stopped
                                                        with Error</b></span></td>
            </tr>
        </tbody>
    </table>
</div>

<div layout:fragment="script">
    <script>
        var options = {
            valueNames: [ 'name' ]
        };
        var userList = new List('box-body pad table-responsive', options);
    </script>
</div>

The values here are coming from the spring controller and it is displaying it right. 此处的值来自弹簧控制器,并且显示正确。 But the search is not working. 但是搜索不起作用。 At the same time if I change the dynamic table to a normal static table as following: 同时,如果我将动态表更改为普通静态表,如下所示:

<div id="box-body pad table-responsive">
    <input class="search" placeholder="Search" />
    <button class="sort" data-sort="name">Sort by name</button>
    <table class="table table-bordered text-center">
        <thead>
            <tr>
                <th>Name</th>
                <th>Year</th>
            </tr>
            </thead>
        <tbody class = "list">
            <tr>  
                <td class = "name">Jonny</td>
                <td class = "born">1991</td>
            </tr>
            <tr>
                <td class = "name">Harry</td>
                <td class = "born">1992</td>
            </tr>
        </tbody>
    </table>
</div>

And the script is the same. 和脚本是相同的。 Then the List.js is working fine for this static table, the list is filtered. 然后,该静态表的List.js工作正常,列表被过滤。

So please can anyone help me with this?? 所以,请问有人可以帮我吗? I want to filter search in dynamically populated table. 我想在动态填充的表中过滤搜索。

Thanks in advance!!! 提前致谢!!!

Cheers!!! 干杯!!!

dynamic table generation HTML 动态表格生成HTML dynamic_table

static table generation HTML 静态表格生成HTML static_table

Two things could be problem here: 这里有两件事可能是问题:

  1. HTML generated and rendered by thymeleaf is different format to your static HTML page thymeleaf生成和呈现的HTML与静态HTML页面的格式不同
  2. Your javascripts are loading before your view is rendered 在呈现视图之前,您的JavaScript正在加载

Solutions: 解决方案:

  1. Compare HTML generated by Thymeleaf against static HTML and check for differences (if you want update your question with this information so we can help) 将Thymeleaf生成的HTML与静态HTML进行比较,并检查差异(如果您想使用此信息更新问题,以便我们提供帮助)
  2. Ensure your javascript is loaded only once the document is ready and not before using say $( document ).ready(); 确保仅在文档准备好后才加载javascript,而不要在使用say $(document).ready()之前加载。

Hey I think i have found the answer of the question: 嘿,我想我已经找到了问题的答案:

When i put the following script in the page then it works: 当我在页面中放置以下脚本时,它将起作用:

<script>
    window.onload = function(){
        var options = {
            valueNames: [ 'name', 'dumpfile' ]
        };
        var userList = new List('box-body pad table-responsive', options);
    };
</script>

I think the js needs to be loaded after the window is loaded for the dynamic populated table. 我认为在为动态填充表加载窗口之后,需要加载js。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM