【问题标题】:Express.js to EJS rendering data partiallyExpress.js 到 EJS 部分呈现数据
【发布时间】:2016-03-20 20:17:45
【问题描述】:

想象一下下面的数据库。

// '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 篇文章,服务器会尝试显示全部内容。我想做的是一些内容摘要,我该如何巧妙地做到这一点?

【问题讨论】:

    标签: javascript node.js express ejs


    【解决方案1】:

    对于摘要内容,在末尾显示带有省略号的开头几个文本

    <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

    在节点端创建 API,以与页面加载期间相同的格式提供此内容数据或新文章

    【讨论】:

    • 谢谢!它简单易懂。
    猜你喜欢
    • 2016-02-15
    • 2020-05-10
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 2021-03-27
    • 1970-01-01
    相关资源
    最近更新 更多