要求:在頁面添加日期選擇控件,并顯示日期相應的標題和數據。進入頁面默認2017年01月。
實現:在頁面上添加select控件,js通過調用id名,實現日期控件的范圍。
主要問題1:月份控件的1-9月與數據庫的兩位數據無法對應。
【解決:當月份為1-9時,在前面添加0
function SetSelect1() {
????????? $(".js-example-basic-single").select2();
????????? var selYear = document.getElementById('selYear');
????????? var selMonth = document.getElementById('selMonth');
????????? setyear("selYear");
????????? setmonth("selMonth");
??????? ? if (new Date().getMonth() >= 1 && new Date().getMonth() <= 9) {
??????????????????? selYear.value = new Date().getFullYear();
??????????????????? selMonth.value = "0".concat(new Date().getMonth());
???????? } else {
?????????????????? selYear.value = new Date().getFullYear();
?????????????????? selMonth.value = new Date().getMonth();
??????? }
}
?】
主要問題2:使用js實現標題動態變化。
【解決:動態拼接標題語句,并渲染到頁面上。
function SetTitle() {
????????????? $(".page-header").empty();
????????????? $(".page-header").html(
???????????? selYear.value + "年" + selMonth.value + "月" + "醫聯常規統計信息:")
}
】
主要問題3:使用ajax傳入日期選擇參數,并動態顯示數據。當數據不存在時,提示“暫無數據!”
【解決:
function ylcgtjQuery() {
??????? var selYear = $('#selYear').val();
??????? var selMonth = $('#selMonth').val();
??????? SetTitle();
??????? $.ajax({
?????????????????? url : "ylcgtjQuery!getYuzhiset.action",
?????????????????? type : "POST",
?????????????????? data: "selYear="+selYear+"&selMonth="+selMonth,
?????????????????? dataType : "text",
?????????????????? success : function(result) {
????????????????????????? if (result != null) {
???????????????????????? ? ? ? if(result!="nodata"){
???????????????????????? ? ? ? ? ?? ? $("#ylcjtjDesc").empty();
?????????????????????????????????????? var str = result;
??????????????????????????????????????? //返回數據中數字顯示紅色
??????????????????????????????????????? var nums = result.match(/[1-9]\d*\.\d*|0\.\d*[1-9]\d*/g);
??????????????????????????????????????? if(nums!=null){
????????????????????????????????????????????? for ( var i = 0; i < nums.length; i++) {
?????????????????????????????????????????????????????? str = str.replace(nums[i], "" + nums[i]+ "");
?????????????????????????????????????????? ? ? }
???????????????????????????? ? ? ? ?? ? $("#ylcjtjDesc").append(str);
???????????????????????????????????????? }
?????????????????????????????? }else{
???????????????????????????????????????? $("#ylcjtjDesc").empty();
???????????????????????????????????????? var str = "暫無數據!";
??????????????????????????????????????? $("#ylcjtjDesc").append(str);
??????????????????????????????? }
???????????????????????? }
????????????????? }
???????? }
);}
】