說明:用于獲取文檔的評論列表、評論分頁列表
使用方法:{% commentList 變量名稱 with archiveId="1" type="page|list" %}
如將變量定義為 comments {% commentList comments with archiveId="1" type="page" %}...{% endcommentList %}
commentList
支持的參數(shù)有:archiveId
archiveId
為指定的文檔ID;order
order
可以指定顯示的排序規(guī)則,支持依據 id正序排序 order="id desc"
、按id倒敘排序 order="id desc"
;limit
數(shù)量的列表,比如limit="10"
則只會顯示10條,limit
在不是分頁列表的時候,支持offset
模式,也就是 ,
分隔模式,如想從第2條開始,獲取10條數(shù)據,可以設置成 limit="2,10"
;type
type
支持按 page、list 方式列出。默認值為list,如果type="page"
后續(xù)可用 分頁標簽 pagination
來組織分頁顯示 {% pagination pages with show="5" %}
。siteId
siteId
一般不需要填寫,如果你使用后臺的多站點管理創(chuàng)建了多個站點,并且想調用其他站點的數(shù)據,則可以通過指定 siteId
來實現(xiàn)調用指定站點的數(shù)據。comments 是一個數(shù)組對象,因此需要使用 for
循環(huán)來輸出
Id
ArchiveId
UserName
UserId
Ip
VoteCount
Content
ParentId
Status
Status = 1 表示審核通過, status = 0 時審核中,不要顯示出來Parent
Parent 包含上級評論的完整 item,字段和評論item相同CreatedTime
時間戳,需要使用格式化時間戳為日期格式 {{stampToDate(item.CreatedTime, "2006-01-02")}}
{# list 列表展示 #}
<div>
{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
{% for item in comments %}
<div>
<div>
<span>
{% if item.Status != 1 %}
審核中:{{item.UserName|truncatechars:6}}
{% else %}
{{item.UserName}}
{% endif %}
</span>
{% if item.Parent %}
<span>回復</span>
<span>
{% if item.Status != 1 %}
審核中:{{item.Parent.UserName|truncatechars:6}}
{% else %}
{{item.Parent.UserName}}
{% endif %}
</span>
{% endif %}
<span>{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
</div>
<div>
{% if item.Parent %}
<blockquote>
{% if item.Parent.Status != 1 %}
該內容正在審核中:{{item.Parent.Content|truncatechars:9}}
{% else %}
{{item.Parent.Content|truncatechars:100}}
{% endif %}
</blockquote>
{% endif %}
{% if item.Status != 1 %}
該內容正在審核中:{{item.Content|truncatechars:9}}
{% else %}
{{item.Content}}
{% endif %}
</div>
<div class="comment-control" data-id="{{item.Id}}" data-user="{{item.UserName}}">
<a class="item" data-id="praise">贊(<span class="vote-count">{{item.VoteCount}}</span>)</a>
<a class="item" data-id=reply>回復</a>
</div>
</div>
{% endfor %}
{% endcommentList %}
</div>
{# page 分頁列表展示 #}
<div>
{% commentList comments with archiveId=archive.Id type="page" limit="10" %}
{% for item in comments %}
<div>
<div>
<span>
{% if item.Status != 1 %}
審核中:{{item.UserName|truncatechars:6}}
{% else %}
{{item.UserName}}
{% endif %}
</span>
{% if item.Parent %}
<span>回復</span>
<span>
{% if item.Status != 1 %}
審核中:{{item.Parent.UserName|truncatechars:6}}
{% else %}
{{item.Parent.UserName}}
{% endif %}
</span>
{% endif %}
<span>{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
</div>
<div>
{% if item.Parent %}
<blockquote>
{% if item.Parent.Status != 1 %}
該內容正在審核中:{{item.Parent.Content|truncatechars:9}}
{% else %}
{{item.Parent.Content|truncatechars:100}}
{% endif %}
</blockquote>
{% endif %}
{% if item.Status != 1 %}
該內容正在審核中:{{item.Content|truncatechars:9}}
{% else %}
{{item.Content}}
{% endif %}
</div>
<div class="comment-control" data-id="{{item.Id}}" data-user="{{item.UserName}}">
<a class="item" data-id="praise">贊(<span class="vote-count">{{item.VoteCount}}</span>)</a>
<a class="item" data-id=reply>回復</a>
</div>
</div>
{% endfor %}
{% endcommentList %}
</div>
<div>
{% pagination pages with show="5" %}
<ul>
<li>總數(shù):{{pages.TotalItems}}條,總共:{{pages.TotalPages}}頁,當前第{{pages.CurrentPage}}頁</li>
<li class="{% if pages.FirstPage.IsCurrent %}active{% endif %}"><a href="{{pages.FirstPage.Link}}">{{pages.FirstPage.Name}}</a></li>
{% if pages.PrevPage %}
<li><a href="{{pages.PrevPage.Link}}">{{pages.PrevPage.Name}}</a></li>
{% endif %}
{% for item in pages.Pages %}
<li class="{% if item.IsCurrent %}active{% endif %}"><a href="{{item.Link}}">{{item.Name}}</a></li>
{% endfor %}
{% if pages.NextPage %}
<li><a href="{{pages.NextPage.Link}}">{{pages.NextPage.Name}}</a></li>
{% endif %}
<li class="{% if pages.LastPage.IsCurrent %}active{% endif %}"><a href="{{pages.LastPage.Link}}">{{pages.LastPage.Name}}</a></li>
</ul>
{% endpagination %}
</div>
評論使用form表單提交,提交后臺接收的地址為:/comment/publish
。需要提交的字段有
字段 | 是否必填 | 說明 |
---|---|---|
archive_id | 是 | 對應的文檔ID |
user_name | 是 | 評論的用戶名 |
content | 是 | 評論內容 |
parent_id | 否 | 上級評論ID,當回復,某條評論的時候,要帶上才會產生關聯(lián) |
return | 否 | 提交后,指定后端返回的格式,可選的值有:`html`、`json`,默認為 html |
表單代碼示例
<form method="post" action="/comment/publish">
<input type="hidden" name="return" value="html">
<input type="hidden" name="archive_id" value="{% archiveDetail with name="Id" %}">
<div>
<label>用戶名</label>
<div>
<input type="text" name="user_name" required lay-verify="required" placeholder="請?zhí)顚懩年欠Q" autocomplete="off">
</div>
</div>
<div>
<label>評論內容</label>
<div>
<textarea name="content" placeholder="" id="comment-content-field" rows="5"></textarea>
</div>
</div>
<div>
<div>
<button type="submit">提交評論</button>
<button type="reset">重置</button>
</div>
</div>
</form>
可以給某條評論的內容點贊,點贊使用form表單提交,點贊提交后臺接收地址為:/comment/praise
。點贊需要提交的字段有:
字段 | 是否必填 | 說明 |
---|---|---|
id | 是 | 評論內容ID |
評論點贊只支持返回 json 格式的數(shù)據,因此需要用 js post 來提交。
示例代碼
<div class="comment-control">
<a class="item vote-comment" data-id="praise" data-id="{{item.Id}}">贊(<span class="vote-count">{{item.VoteCount}}</span>)</a>
</div>
$('.vote-comment').click(function(e) {
let that = $(this);
let commentId = $(this).data('id');
//贊
$.ajax({
url: '/comment/praise',
method: "post",
data: {id: commentId},
dataType: "json",
success: function (res) {
if(res.code === 0) {
alert(res.msg);
that.find('.vote-count').text(res.data.vote_count)
} else {
alert(res.msg);
}
},
error: function (err) {
alert(res.msg);
}
});
});