Javascript Express.js到EJS部分呈现数据
想象一下下面的数据库Javascript Express.js到EJS部分呈现数据,javascript,node.js,express,ejs,Javascript,Node.js,Express,Ejs,想象一下下面的数据库 // '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...........
// '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"/>
}
/“文章”模式
{
主题:“你好,世界”,
内容:'我想部分显示此内容。
内容将包含像long这样的详细文本
呜呜……呜呜……呜呜……呜呜……呜呜。。。。。。。。。。。
布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉布拉。。。。。。。。
更重要的是,它会
}
现在我将这些数据呈现到某个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>
//为了不显示得过于冗长,请给出style属性(当然,在html之上)
td{空白:nowrap;溢出:隐藏;}
但这样做可能会降低应用程序的性能,对吗?如果一个页面上有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
应用程序在单击readmore
时获取完整数据,具体取决于您使用的是jquery还是angular等
如果有更多文章,则使用ajax应用程序或页面刷新
在节点端创建API,该API以页面加载期间相同的格式提供此内容数据或新文章对于摘要内容,显示最初的几个文本,并在末尾加上省略号
<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
应用程序在单击readmore
时获取完整数据,具体取决于您使用的是jquery还是angular等
如果有更多文章,则使用ajax应用程序或页面刷新
在节点端创建API,该API以页面加载期间相同的格式提供此内容数据或新文章谢谢!这很容易理解,谢谢!这很容易理解。