MVC實現有關時間的進度條,使用jQuery ui的progressbar

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   使用   os   io   

在電商網站中,有時候通過進度條來直觀地顯示使用者是否到期以及使用者當前的狀態。

 

設計這樣的一個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是和進度密切相關的2個屬性。

 

在HomeController中,一個action方法用來顯示介面,一個用來接收來自視圖的GET請求,返回json資料。

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetStatus()
        {
            User user = new User()
            {
                Id = 1,
                Name = "某某使用者",
                CoopTime = 1,
                JoinTime = new DateTime(2014, 3, 20)
            };
            //判斷合作是否已經到期
            int result = DateTime.Compare(DateTime.Now, user.JoinTime.AddYears(user.CoopTime));
            if (result <= 0) //目前時間比合作到期時間早,合作未到期
            {
                //計算時間
                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 //目前時間比合作到期時間晚,合作已到期
            {
                var dataFail = new { msg = false, p = 100 }; 
                return Json(dataFail, JsonRequestBehavior.AllowGet);
            }
        }
    }

以上,

● 使用DateTime的靜態方法Compare來比較2個時間,一個是目前時間,另一個是加入時間 + 合作時間長度,如果結果小於或等於0,就表示沒有到期。
● 使用DateTime的靜態方法Subtract來給2個時間做減法。

 

Home/Index.cshtml中,當頁面載入完畢後,向服務端發出一個非同步GET請求,把返回的資料顯示到progressbar中。

<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />
    <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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.