前言:
1.这个随笔实现了一个Ajax动态加载的例子。
2.使用.net 的MVC框架实现。
3.这个例子重点在前后台交互,其它略写。
开始:
1.控制器ActionResult代码(用于显示页面)
/// <summary>
/// 电话查询页面
/// </summary>
/// <returns></returns>
public ActionResult PhoneSearch(string sql)
{
phoneList=从数据库查询数据;
ViewBag.phoneList = phoneList;
return View();
}
2.前台页面主要代码
说明:这个就是要展示数据的表格,里面的字段要和你建好的模型匹配。
<table border="1" cellspacing="0" cellpadding="0" class="toLang" id="phoneTable">
<tr>
<th>序号</th>
<th>公司</th>
<th>部门</th>
<th>小组</th>
<th>姓名</th>
<th>职位</th>
<th>电话</th>
</tr>
<tbody id="todeListTBODY">
@if (ViewBag.phoneList != null)
{
foreach (var item in ViewBag.phoneList)
{
number = number + 1;
<tr>
<td>@number</td>
<td>@item.Conpany</td>
<td>@item.Department</td>
<td>@item.Team</td>
<td>@item.Name</td>
<td>@item.Position</td>
<td>@item.PhoneNumber</td>
</tr>
}
}
</tbody>
</table>
3.我的查询条件
<div style="display:block;float:left; width:100%; ">
公司:
<select class="InputTestStyle" id="company" onclick="initDeptSelect()">
<option>==请选择公司==</option>
</select>
部门:
<select class="InputTestStyle" id="department" onclick="initGroupSelect()">
<option>==请选择公司==</option>
</select>
小组:
<select class="InputTestStyle" id="group" onclick="QueryPhoneNum()">
<option>==请选择公司==</option>
</select>
</div>
4.查询条件的初始化(以公司这个为例)
4.1前台的JavaScript代码
//打开页面的时候执行
window.onunload = initCompanySelect();
//初始化“公司”下拉框
function initCompanySelect()
{
$.ajax({
type: 'POST',
url: '/Home/GetCompantListForPhone',
dataType: 'json',
data: { },
success: function (data) {
//1.清空这个下拉框的数据
// $('#company option').remove();//也能成功实现
$('#company').empty();
$("#company").append($('<option>' + '==请选择公司==' + '</option>'));
//2.将返回值动态加载进下拉框,动态生成标签。
for (i = 0; i < data.length;i++)
{
$("#company").append($('<option >' + data[i].Conpany + '</option>'));
}
},
error: function (XMLHttpRequest, textStatus, errorThown) {
alert("操作失败!");
}
})
}









