網(wǎng)站首頁 編程語言 正文
在電商網(wǎng)站中,有時候通過進(jìn)度條來直觀地顯示用戶是否到期以及用戶當(dāng)前的狀態(tài)。
設(shè)計這樣的一個Model。
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int CoopTime { get; set; }
public DateTime JoinTime { get; set; }
}
以上,合作時長屬性CoopTime,和加入時間屬性JoinTime是和進(jìn)度密切相關(guān)的2個屬性。
在HomeController中,一個action方法用來顯示界面,一個用來接收來自視圖的GET請求,返回json數(shù)據(jù)。
public ActionResult Index()
{
return View();
}
public ActionResult GetStatus()
{
User user = new User()
{
Id = 1,
Name = "某某用戶",
CoopTime = 1,
JoinTime = new DateTime(2014, 3, 20)
};
//判斷合作是否已經(jīng)到期
int result = DateTime.Compare(DateTime.Now, user.JoinTime.AddYears(user.CoopTime));
if (result <= 0) //當(dāng)前時間比合作到期時間早,合作未到期
{
//計算時間
var pastDays = (DateTime.Now.Subtract(user.JoinTime)).TotalDays;
var oneYearDays = (user.JoinTime.AddYears(user.CoopTime).Subtract(user.JoinTime)).TotalDays;
var pastPercentage = (pastDays / oneYearDays) * 100;
var dataSuccess = new { msg = true, p = pastPercentage };
return Json(dataSuccess, JsonRequestBehavior.AllowGet);
}
else //當(dāng)前時間比合作到期時間晚,合作已到期
{
var dataFail = new { msg = false, p = 100 };
return Json(dataFail, JsonRequestBehavior.AllowGet);
}
}
}
以上,
- 使用DateTime的靜態(tài)方法Compare來比較2個時間,一個是當(dāng)前時間,另一個是加入時間 + 合作時長,如果結(jié)果小于或等于0,就表示沒有過期。
- 使用DateTime的靜態(tài)方法Subtract來給2個時間做減法。
Home/Index.cshtml中,當(dāng)頁面加載完畢后,向服務(wù)端發(fā)出一個異步GET請求,把返回的數(shù)據(jù)顯示到progressbar中。
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link href="~/Content/themes/base/jquery-ui.css" />
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery-ui-1.8.24.min.js"></script>
<script type="text/javascript">
$(function () {
$.getJSON('@Url.Action("GetStatus","Home")', function(data) {
if (data.msg == true) {
var temp = parseInt(data.p);
$('#p').progressbar({
value: temp
});
} else {
$('#message').text('已到期');
$('#p').progressbar({
value: parseInt(data.p)
});
}
});
});
</script>
</head>
<body>
<div id="p">
</div>
<div>
<span id="message"></span>
</div>
</body>
原文鏈接:https://www.cnblogs.com/darrenji/p/3876760.html
相關(guān)推薦
- 2023-04-29 C++數(shù)組模擬之單鏈表與雙鏈表和棧和隊列的實現(xiàn)過程_C 語言
- 2022-07-20 SQL?Server中搜索特定的對象_MsSql
- 2022-05-10 MAC m1使用homebrew安裝redis報錯
- 2023-01-21 python?flask自定義404錯誤頁面方式_python
- 2022-07-15 SQL?Server中的游標(biāo)介紹_MsSql
- 2022-08-22 使用.NET?Core創(chuàng)建exe應(yīng)用程序_實用技巧
- 2022-07-20 centos 安裝jenkins 實現(xiàn)自動部署到遠(yuǎn)程服務(wù)器 (樹莓派可用)
- 2022-06-27 python結(jié)合shell自動創(chuàng)建kafka的連接器實戰(zhàn)教程_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支