beego golang bootstrap-table做月度考勤(打卡、簽到)統計表

涉及到知識點:

1.bootstrap table動態表頭

每個月的天數不一樣,所以要根據服務端的json數據,動態改變表頭。

https://blog.csdn.net/abubu123/article/details/78060321

2.golang動態結構體字段/用map

因爲golang無法構建動態的結構體字段多少,所以才用map[int]string

比如某個用戶某月1日已經簽到:map[1]="1",2日沒簽到則爲map[1]=""

3.golang求出每月天數

	//當月天數
	const base_format = "2006-01-02"
	year := c.Input().Get("year")
	month := c.Input().Get("month")
	if len(month) == 1 {
		month = "0" + month
	}
	SelectMonth1, err := time.Parse(base_format, year+"-"+month+"-01")//每月的第一天
	if err != nil {
		beego.Error(err)
	}
	SelectMonth2 := SelectMonth1.AddDate(0, 1, -1)//每月的最後一天,其實就是這個月加上1個月然後減1天,很巧妙
	//建立一個動態月日數組

	days := SelectMonth2.Sub(SelectMonth1) / 24   //這個月天數,其實應該是下個月第一天減這個月第一天。減出來的其實是duration類型,而且是小時單位,必須除以24,單位還是小時
	dayss := days.Hours()//小時轉爲float64
	daysss := strconv.FormatFloat(dayss, 'f', -1, 64) //float64轉string
	dayssss, err := strconv.Atoi(daysss)              //string轉int
	// beego.Info(reflect.TypeOf(dayss)) //float64
	beego.Info(dayssss)

4.給map賦值

定義一個map數組,一個map,記得給map[0]=username,第一列作爲用戶名的處理

s := []map[int]interface{}{}
var checkmap = make(map[int]interface{}, dayssss+2)
//給checkmap 賦值
//排序
//然後追加給數組
s = append(s, checkmap2)
//轉成json
b, err := json.Marshal(s)

5.map排序

不寫了。

6.前端bootstrap table處理第一列用戶名

在前面第1點基礎上加個判斷

       var columnsArray = [];
        columnsArray.push({field: "state", checkbox: true});
        for (var i = 0; i < (Object.keys(json[0])).length; i++) {//Object.keys(obj) 獲取key名稱
          var property = (Object.keys(json[0]))[i];//id   username
          if (property==0) {
              columnsArray.push({
              "title": "name",
              "field": property,
              "align" : 'center',
              "valign" : 'middle',
              switchable: true,
            });
          }else{
            columnsArray.push({
              "title": property,
              "field": property,
              "align" : 'center',
              "valign" : 'middle',
              switchable: true,
            });
          }
        }

就是這麼個樣子了:name列應該用用戶名錶示。 

對應小程序打卡記錄:用戶9

 

我的git上有源碼,engineercms和對應的小程序端。歡迎star。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章