简体   繁体   English

提取数据后如何加载表

[英]how to load a table after the data is fetched

I am currently developing a web application and I am fetching a huge amount of data through ajax request triggering it by onload event on body.My table requires jquery and other CSS files for UI once the data is retrieved when I try to append the data to the table the styling and other features(jquery) does not apply to new data and I can't refresh the whole page because it makes an another ajax call. 我目前正在开发一个Web应用程序,并且正在通过ajax请求获取大量数据,该请求是通过主体上的onload事件触发它来触发的。当我尝试将数据追加到数据库时,一旦检索到数据,我的表就需要jquery和其他CSS文件用于UI该表的样式和其他功能(jquery)不适用于新数据,我无法刷新整个页面,因为它进行了另一个ajax调用。 So, is there a way to reload(refresh) that particular table after the data is received or blocking the table from loading until after the data is received. 因此,有一种方法可以在接收到数据后重新加载(刷新)该特定表,或阻止该表加载直到接收到数据后。

There are multiple solutions and possibly not all are suitable for every setup. 有多种解决方案,可能并非所有解决方案都适合每种设置。 Specifically, if you use an MVVM framework such as Angular, Vue or similar, you should be able to leverage their components to do the job. 具体来说,如果您使用Angular,Vue或类似的MVVM框架,则应该能够利用它们的组件来完成这项工作。 Since you mention none such framework, I assume you want a vanilla JS solution. 既然您没有提到这样的框架,所以我假设您想要一个普通的JS解决方案。

One possibility is to create an empty table and build the missing HTML by hand inside the ajax call's callback: 一种可能性是创建一个空表,并在ajax调用的回调中手动构建缺少的HTML:

<table id="foo"></table>

<script>
    const callback = (results) => {
        const table = document.getElementById("foo");
        let innerHTML = "";
        results.forEach((result) => {
            innerHTML += `<tr><td>${result.column1}</td>` +
                         `<td>${result.column2}</td></tr>`;
        });
        table.innerHTML = innerHTML;
    };
</script>

You could also consider using HTML templates . 您也可以考虑使用HTML模板 In your ajax call's callback, you could process the data to fill the template and then append as many rows as needed to your table: 在ajax调用的回调中,您可以处理数据以填充模板,然后将所需的任意行追加到表中:

<table id="foo"></table>
<template id="bar">
    <tr>
        <td></td>
        <td></td>
    </tr>
</template>

<script>
    const callback = (results) => {
        const table = document.getElementById("foo");
        const rowTemplate = document.getElementById("bar");
        results.forEach(result => {
            const cells = rowTemplate.content.querySelectorAll("td");
            cells[0].textContent = result.column1;
            cells[1].textContent = result.column2;

            table.appendChild(document.importNode(rowTemplate.content, true));
        });
    };
</script>

An even more interesting way of leveraging the templates would probably be using a templating engine. 利用模板的一种更有趣的方法可能是使用模板引擎。 Here's an example using Nunjucks: 这是使用Nunjucks的示例:

<table id="foo"></table>
<template id="bar">
    {% for result in results %}
    <tr>
        <td>{{result.column1}}</td>
        <td>{{result.column2}}</td>
    </tr>
    {% endfor %}
</template>

<script>
    const callback = (results) => {
        const table = document.getElementById("foo");
        const templateHTML = document.getElementById("bar").innerHTML;
        const rowTemplate = nunjucks.compile(templateHTML);

        table.innerHTML = rowTemplate.render({results});
    };
</script>

Finally, if you have control over the data returned by the ajax call, you could also go the old-fashioned way and just return HTML there instead of, say, a JSON array. 最后,如果您可以控制ajax调用返回的数据,那么您也可以采用老式的方式,只返回HTML而不是JSON数组。

As for CSS styling, you need to make sure the CSS rules are written in a way that will be applied to the generated code. 至于CSS样式,您需要确保CSS规则以适用于所生成代码的方式编写。 They will be used automatically without the need to reload the page. 它们将自动使用,而无需重新加载页面。

Should you need to do further operations on the table rows, ie add event listeners, it would be best to just set them on the table element and then check the event target. 如果您需要对表行进行进一步的操作,即添加事件侦听器,则最好将它们设置在表元素上,然后检查事件目标。 This way you can manipulate the table contents without the need to remove and re-add any event listeners. 这样,您可以操作表内容,而无需删除和重新添加任何事件侦听器。

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

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