簡體   English   中英

Express.js到EJS部分渲染數據

[英]Express.js to EJS rendering data partially

想象如下的數據庫。

// 'Article'  Schema
{ 
subject : 'Hello world', 
content : 'I want to display this content partially. 
           The content would has verbose text like long
           blabla...........blabla.......blabla......blabla...........
           blabla.......blabla......blabla...........b........
           and even more, It would has <img src=".......jpg"/>
}

現在,我將這些數據渲染到一些ejs文件中,

<table>
<% Articles.forEach(function(article, index){ %>
    <tr>
       <td> <%= article.subject %> </td>
       <td> <%= article.content %> </td>
    </tr>
<% }) %>
</table>


// To don't display verbosely, give style attribute (Of course, on top of html)

<style>
     td { white-space: nowrap; overflow: hidden; }
</style>

但是這樣,它可能會降低應用程序性能,對嗎? 如果一頁上有10、20、30個文章,則服務器將嘗試顯示全部內容。 我要做的是一些內容概述,我該如何巧妙地做到這一點?

對於夏季內容,請在開頭顯示幾句,並在最后加上省略號

<table>
<% Articles.forEach(function(article, index){ %>
    <tr>
       <td> <%= article.subject %> </td>
       <td> <%= article.content.substring(0,10) %> ...</td>
       <td> <a id="read_more" content_id="cont-id" href = "javascript:(void)"> read more..</a></td>
    </tr>
<% }) %>

然后使用ajax應用程序來獲得完整的數據,點擊read more取決於您使用的是jquery還是angular等。

如果有更多文章,請使用ajax application or page refresh pagination ajax application or page refresh

在節點側,創建API,以與頁面加載期間相同的格式提供此內容數據或新文章

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM