今天做一个功能,需要实现读取一个文件夹的内容,根据文件的日期进行排序,取出并绑定在页面上面。研究了很久,看到人家写的有点启发。
HTML:
<table cellspacing="0">
<thead>
<tr>
<th>标题</th>
<th>发件人</th>
<th class="thead-lasttd">日期</th>
</tr>
</thead>
<tbody id="trAnnouncement"></tbody>
</table>
Jquery :
<script>
$(function () {
var _fileAddress = ehr.servicesUrl + "Content1/Announcement/";
var trAnnouncement="";
//动态加载菜单
ehr.ajax(ehr.servicesUrl + 'get/Announcement', data, function (json) {
if (json.Success)
{
var datas = json.Value.announcement;
for (var i = 0; i < datas.length; i++) {
trAnnouncement += "<tr><td width=\"460px\" style=\"padding-top: 10px;\"><a href= '" + _fileAddress + datas[i].Name + " ' target=\"_blank\" style=\"color:#5F5D5D;\">" + datas[i].FirstName + "</a></td>";
trAnnouncement += "<td width=\"130px\" style=\"padding-top: 10px;\">总务部</td> <td width=\"130px\" style=\"padding-top: 10px;\">" + datas[i].LastTime + "</td> </tr>";
}
}
else
{
trAnnouncement += "<tr><td width=\"460px\" style=\"padding-top: 10px;\"></td><td width=\"130px\" style=\"padding-top: 10px;\"></td> <td width=\"130px\" style=\"padding-top: 10px;\"></td> </tr>";
}
$("#trAnnouncement").html(trAnnouncement);
});
});
</script>
后台代码;
public ActionResult getAnnouncement()
{
JsonRult result = new JsonRult();
string _fileAddress = Server.MapPath("~/Content1/Announcement");
var directory = new DirectoryInfo(_fileAddress);
string FirstName = "";
string LastTime = "";
string json = "{\"announcement\":[";
foreach (var item in directory.GetFileSystemInfos().OrderByDescending(x => x.LastWriteTime))
//OrderByDescending 升序 OrderBy降序 x.排序条件
{
if (item.Extension.ToLower() != ".files")
{
FirstName = item.Name.Substring(0, (item.Name.LastIndexOf(".")));//标题
LastTime = item.LastWriteTime.ToString("yyyy-MM-dd");//发布日期
json += "{\"FirstName\":\"" + FirstName + "\",\"Name\":\"" + item.Name + "\",\"LastTime\":\"" + LastTime + "\"},";
}
}
json = json+"]}";
result.Value = json;
result.Message = "";
result.Success = (FirstName != "" ? true : false);
var rultResult = Json(result, JsonRequestBehavior.AllowGet);
return rultResult;
}
显示的效果