前端
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="calendar.aspx.cs" Inherits="YKTWeb.Example.calendar" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>vuejs日历、表单、多行录入</title>
<style type="text/css">
#calendar {
width: 100%;
border-collapse: collapse;
}
#calendar tr th {
font-size: 10px;
height: 30px;
text-align: center;
}
#calendar tr td {
font-size: 8px;
height: 45px;
text-align: center;
color: black;
border: 0.5px solid black;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="app" style="width: 500px;">
<div style="text-align:center;">
<select v-model="year" @change="selectChange">
<option v-for="item in years" :value="item">
{{item}}
</option>
</select>
<label>年</label>
<select v-model="month" @change="selectChange">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<label>月</label>
</div>
<table id="calendar">
<tbody>
<tr>
<th>星期一</th>
<th>星期二</th>
<th>星期三</th>
<th>星期四</th>
<th>星期五</th>
<th>星期六</th>
<th>星期日</th>
</tr>
<tr v-for="item in info">
<td v-for="dayData in item">{{dayData.day}}</td>
</tr>
</tbody>
</table>
<div>
<table>
<tr>
<td>文本</td>
<td><input v-model="formData.message" placeholder="文本输入..."/></td>
</tr>
<tr>
<td>多行文本</td>
<td>
<textarea v-model="formData.message2" placeholder="多行文本输入..."></textarea>
</td>
</tr>
<tr>
<td>下拉</td>
<td>
<select v-model="formData.message3">
<option value="">请选择</option>
<option value="1">选项1</option>
<option value="2">选项2</option>
</select>
</td>
</tr>
<tr>
<td colspan="2">
<button type="button" @click="onSubmit($event)">提交</button>
</td>
</tr>
</table>
<table>
<tr>
<td colspan="2">
<button type="button" @click="AddRow($event)">添加一行</button>
<button type="button" @click="RowsSubmit($event)">提交</button>
</td>
</tr>
<tr v-for="(item,index) in rows">
<td>第{{index+1}}行</td>
<td>
<input v-model="item.inputvalue" placeholder="文本输入..."/>
</td>
<td>
<button type="button" @click="del(index)">删除</button>
</td>
</tr>
</table>
</div>
</div>
<script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
<script type="text/javascript">
var obj = new Vue({
el: '#app',
data() {
return {
info: null,
year: new Date().getFullYear(),
month: new Date().getMonth() + 1,
years: new Array(),
formData: {
//message2:null
}, rows: [{ inputvalue: "第一行内容" }]
}
},
created() {
this.send(this.year, this.month);
for (var i = 2022; i >= 2010; i--) {
this.years.push(i);
}
//绑定表单数据
axios({
url: "/Example/calendar.aspx/GetData",
method: "post",
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
data: {
id: 1
}
}).then(response => (this.formData = response.data.d)).
catch(function (error) { // 请求失败处理
console.log(error);
});
},
watch: {//监听函数
'formData.message2': function (val) {
this.formData.message2 = val.replace(/\D/g, '');//只能输入数字
console.log(val);
}, month: function (val) {
console.log(val);
}
},
methods: {//自定义函数
send: function (year, month) {//日历
axios({
url: "/Example/calendar.aspx/GetCalendar",
method: "post",
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
data: {
year: year,
month: month
}
}).then(response => (this.info = response.data.d)).
catch(function (error) { // 请求失败处理
console.log(error);
});
},
selectChange: function () {//切换日历月份
this.send(this.year, this.month);
},
onSubmit: function (event) {//提交表单
var result = this.validate();
if (result.valiState) {
axios({
url: "/Example/calendar.aspx/formSubmit",
method: "post",
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
data: {
formData: JSON.stringify(this.formData)
}
}).then(response => (alert(response.data.d))).
catch(function (error) { // 请求失败处理
console.log(error);
});
} else {
alert(result.msg.join(','));
}
}, validate: function () { //表单验证
var result = {
valiState: true,
msg: new Array()
}
if (!this.formData.message) {
result.valiState = false;
result.msg.push("项目1不能为空");
}
if (!this.formData.message2) {
result.valiState = false;
result.msg.push("项目2不能为空");
}
if (!this.formData.message3) {
result.valiState = false;
result.msg.push("项目3不能为空");
}
return result;
}, AddRow: function ($event) {
this.rows.push({ inputvalue: "" });
}, RowsSubmit: function ($event) {
console.log(this.rows);
}, del: function (index) {
this.rows.splice(index, 1);
}
}
})
</script>
</form>
</body>
</html>
后端
/// <summary>
/// 日历
/// </summary>
[WebMethod]
public static List<List<Dictionary<string, object>>> GetCalendar(int year, int month)
{
List<List<Dictionary<string, object>>> monthList = new List<List<Dictionary<string, object>>>();
List<Dictionary<string, object>> weekList = new List<Dictionary<string, object>>();
DateTime day1 = new DateTime(year, month, 1);
int week1 = (int)day1.DayOfWeek;//获取此月1号的星期
if (week1 == 0)
{
week1 = 7;
}
int lastday = day1.AddMonths(1).AddDays(-1).Day; //获取此月的最后一天
bool bl = true;
for (int i = 1; i <= lastday; i++)
{
Dictionary<string, object> dayData = new Dictionary<string, object>();
if (bl)
{
for (int week = week1; week > 1; week--)
{
dayData = new Dictionary<string, object>();
dayData.Add("day", null);
dayData.Add("AQI", null);
weekList.Add(dayData);
}
bl = false;
}
dayData = new Dictionary<string, object>();
//DataRow[] rows = dt.Select("iDay=" + i);
dayData.Add("day", i);
dayData.Add("AQI", null);
//if (rows.Length > 0)
//{
// dayData["AQI"] = rows[0]["CityAQI24"];
//}
weekList.Add(dayData);
if ((i + week1 - 1) % 7 == 0)
{
monthList.Add(weekList);
weekList = new List<Dictionary<string, object>>();
}
if (lastday == i)
{
for (int j = lastday + week1; j <= 42; j++)
{
dayData = new Dictionary<string, object>();
dayData.Add("day", null);
dayData.Add("AQI", null);
weekList.Add(dayData);
if (j % 7 == 0)
{
monthList.Add(weekList);
weekList = new List<Dictionary<string, object>>();
}
}
}
}
return monthList;
//return JsonConvert.SerializeObject(monthList);
}