這篇文章給大家分享的是有關(guān)Ajax如何實(shí)現(xiàn)動(dòng)態(tài)加載數(shù)據(jù)的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)專注于安岳網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供安岳營(yíng)銷型網(wǎng)站建設(shè),安岳網(wǎng)站制作、安岳網(wǎng)頁(yè)設(shè)計(jì)、安岳網(wǎng)站官網(wǎng)定制、微信小程序服務(wù),打造安岳網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供安岳網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
1.這個(gè)隨筆實(shí)現(xiàn)了一個(gè)Ajax動(dòng)態(tài)加載的例子。
2.使用.net 的MVC框架實(shí)現(xiàn)。
3.這個(gè)例子重點(diǎn)在前后臺(tái)交互,其它略寫。
開始:
1.控制器ActionResult代碼(用于顯示頁(yè)面)
/// <summary> /// 電話查詢頁(yè)面 /// </summary> /// <returns></returns> public ActionResult PhoneSearch(string sql) { phoneList=從數(shù)據(jù)庫(kù)查詢數(shù)據(jù); ViewBag.phoneList = phoneList; return View(); }
2.前臺(tái)頁(yè)面主要代碼
說明:這個(gè)就是要展示數(shù)據(jù)的表格,里面的字段要和你建好的模型匹配。
<table border="1" cellspacing="0" cellpadding="0" class="toLang" id="phoneTable"> <tr> <th>序號(hào)</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 > 公司: <select class="InputTestStyle" id="company" onclick="initDeptSelect()"> <option>==請(qǐng)選擇公司==</option> </select> 部門: <select class="InputTestStyle" id="department" onclick="initGroupSelect()"> <option>==請(qǐng)選擇公司==</option> </select> 小組: <select class="InputTestStyle" id="group" onclick="QueryPhoneNum()"> <option>==請(qǐng)選擇公司==</option> </select> </div>
4.查詢條件的初始化(以公司這個(gè)為例)
4.1前臺(tái)的JavaScript代碼
//打開頁(yè)面的時(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>' + '==請(qǐng)選擇公司==' + '</option>')); //2.將返回值動(dòng)態(tài)加載進(jìn)下拉框,動(dòng)態(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ù)庫(kù)獲取這個(gè)下拉框數(shù)據(jù)的集合; return Json(compantList); }
其它兩個(gè)下拉框按照這個(gè)辦法完成后。就可以根據(jù)條件查詢了。下面兩個(gè)是對(duì)用的JavaScript和后臺(tái)方法。
5.傳查詢提交到后臺(tái),然后根據(jù)返回的集合重新給table賦值。
//根據(jù)條件查詢電話 function QueryPhoneNum() { if ($('#group').val() == '==請(qǐng)選擇小組==') { 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.將返回值動(dòng)態(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ù)對(duì)應(yīng)的ActionResult
/// <summary> /// 電話查詢 /// </summary> /// <returns></returns> [HttpPost] public JsonResult PhoneSearchSubmit(string company, string dept, string group) { phoneList = 根據(jù)條件查詢數(shù)據(jù); return Json(phoneList); }
ajax是一種在無需重新加載整個(gè)網(wǎng)頁(yè)的情況下,能夠更新部分網(wǎng)頁(yè)的技術(shù),可以通過在后臺(tái)與服務(wù)器進(jìn)行少量數(shù)據(jù)交換,使網(wǎng)頁(yè)實(shí)現(xiàn)異步更新。
感謝各位的閱讀!關(guān)于“Ajax如何實(shí)現(xiàn)動(dòng)態(tài)加載數(shù)據(jù)”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
網(wǎng)站標(biāo)題:Ajax如何實(shí)現(xiàn)動(dòng)態(tài)加載數(shù)據(jù)
URL標(biāo)題:http://m.newbst.com/article22/gcsgcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、動(dòng)態(tài)網(wǎng)站、營(yíng)銷型網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)