Ajax實(shí)現(xiàn)動態(tài)加載數(shù)據(jù)
前言:
1.這個(gè)隨筆實(shí)現(xiàn)了一個(gè)Ajax動態(tài)加載的例子。
2.使用.net 的MVC框架實(shí)現(xiàn)。
3.這個(gè)例子重點(diǎn)在前后臺交互,其它略寫。
開始:
1.控制器ActionResult代碼(用于顯示頁面)
/// <summary>
/// 電話查詢頁面
/// </summary>
/// <returns></returns>
public ActionResult PhoneSearch(string sql)
{
phoneList=從數(shù)據(jù)庫查詢數(shù)據(jù);
ViewBag.phoneList = phoneList;
return View();
}
2.前臺頁面主要代碼
說明:這個(gè)就是要展示數(shù)據(jù)的表格,里面的字段要和你建好的模型匹配。
<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.查詢條件的初始化(以公司這個(gè)為例)
4.1前臺的JavaScript代碼
//打開頁面的時(shí)候執(zhí)行
window.onunload = initCompanySelect();
//初始化“公司”下拉框
function initCompanySelect()
{
$.ajax({
type: 'POST',
url: '/Home/GetCompantListForPhone',
dataType: 'json',
data: { },
success: function (data) {
//1.清空這個(gè)下拉框的數(shù)據(jù)
// $('#company option').remove();//也能成功實(shí)現(xiàn)
$('#company').empty();
$("#company").append($('<option>' + '==請選擇公司==' + '</option>'));
//2.將返回值動態(tài)加載進(jìn)下拉框,動態(tài)生成標(biāo)簽。
for (i = 0; i < data.length;i++)
{
$("#company").append($('<option >' + data[i].Conpany + '</option>'));
}
},
error: function (XMLHttpRequest, textStatus, errorThown) {
alert("操作失敗!");
}
})
}
4.2初始化下拉框?qū)?yīng)的ActionResult代碼
/// <summary>
/// 獲取電話查詢公司下拉數(shù)據(jù)
/// </summary>
/// <returns></returns>
[HttpPost]
public JsonResult GetCompantListForPhone()
{
compantList = 從數(shù)據(jù)庫獲取這個(gè)下拉框數(shù)據(jù)的集合;
return Json(compantList);
}
其它兩個(gè)下拉框按照這個(gè)辦法完成后。就可以根據(jù)條件查詢了。下面兩個(gè)是對用的JavaScript和后臺方法。
5.傳查詢提交到后臺,然后根據(jù)返回的集合重新給table賦值。
//根據(jù)條件查詢電話
function QueryPhoneNum()
{
if ($('#group').val() == '==請選擇小組==')
{
return;
}
number = 0;
$.ajax({
type: 'POST',
url: '/Home/PhoneSearchSubmit',
dataType: 'json',
data: {
company:$('#company').val(),
dept: $('#department').val(),
group: $('#group').val()
},
success: function (phoneList) {
//1.清空這個(gè)表格的數(shù)據(jù)
$('#todeListTBODY tr').remove();
//2.將返回值動態(tài)加載進(jìn)表格。
$.each(phoneList, function (index, element) {
number = number + 1;
$('#todeListTBODY').prepend(function (i) {
return "<tr>" +
"<td>" +number +
"<td>" + element.Conpany +
"<td>" + element.Department +
"<td>" + element.Team +
"<td>" + element.Name +
"<td>" + element.Position +
"<td>" + element.PhoneNumber +
"</tr>";
})
})
},
error: function (XMLHttpRequest, textStatus, errorThown) {
alert("操作失敗!");
}
})
}
5.1與查詢數(shù)據(jù)對應(yīng)的ActionResult
/// <summary>
/// 電話查詢
/// </summary>
/// <returns></returns>
[HttpPost]
public JsonResult PhoneSearchSubmit(string company, string dept, string group)
{
phoneList = 根據(jù)條件查詢數(shù)據(jù);
return Json(phoneList);
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
有關(guān)Ajax中g(shù)et和post的使用問題
下面小編就為大家?guī)硪黄嘘P(guān)Ajax中g(shù)et和post的使用問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06
基于Ajax的formData圖片和數(shù)據(jù)上傳
這篇文章主要為大家詳細(xì)介紹了基于Ajax的formData圖片和數(shù)據(jù)上傳相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
django中使用jquery ajax post數(shù)據(jù)出現(xiàn)403錯(cuò)誤的解決辦法(兩種方法)
在django中,使用jquery ajax post數(shù)據(jù),會出現(xiàn)403的錯(cuò)誤,大家知道該如何解決嗎?下面由腳本之家小編給大家分享兩種解決辦法,需要的朋友可以參考下2015-09-09
用ajax實(shí)現(xiàn)預(yù)覽鏈接可以看到鏈接的內(nèi)容
用CSS設(shè)置預(yù)覽彈出窗口的樣式、用JavaScript進(jìn)行服務(wù)器請求并且顯示彈出窗口,需要的朋友可以參考下2014-08-08
JQuery ajax中error返回錯(cuò)誤及一直返回error的解答
本文由腳本之家小編給大家分享有關(guān) JQuery ajax中error返回錯(cuò)誤及一直返回error的解答總結(jié),需要的朋友可以參考下2015-09-09
ajax請求返回的數(shù)據(jù)看不到回調(diào)函數(shù)沒有執(zhí)行也沒報(bào)錯(cuò)
ajax請求返回的數(shù)據(jù)看不到回調(diào)函數(shù)沒有執(zhí)行,哪里都沒有報(bào)錯(cuò),有類似情況的朋友可以參考下本文2014-01-01

