This article mainly introduces in detail a small example of Ajax dynamic loading of data. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.
Foreword:
1. This essay implements an example of Ajax dynamic loading.
2. Implemented using the .net MVC framework.
3. This example focuses on the front-end and back-end interactions, and the others are abbreviated.
Start:
1. Controller ActionResult code (used to display the page)
1 2 3 4 5 6 7 8 9 10 | public ActionResult PhoneSearch(string sql)
{
phoneList=从数据库查询数据;
ViewBag.phoneList = phoneList;
return View();
}
|
Copy after login
2. Main code of the front page
Description: This is the table to display data, and the fields in it must match the model you have built.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <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>
|
Copy after login
3. My query conditions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <p 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>
</p>
|
Copy after login
4. Initialization of query conditions (take the company as an example )
4.1 JavaScript code at the front end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | window.onunload = initCompanySelect();
function initCompanySelect()
{
$.ajax({
type: 'POST',
url: '/Home/GetCompantListForPhone',
dataType: 'json',
data: { },
success: function (data) {
$('#company'). empty ();
$( "#company" ).append($('<option>' + '==请选择公司==' + '</option>'));
for (i = 0; i < data.length;i++)
{
$( "#company" ).append($('<option >' + data[i].Conpany + '</option>'));
}
},
error: function (XMLHttpRequest, textStatus, errorThown) {
alert( "操作失败!" );
}
})
}
|
Copy after login
4.2 ActionResult code corresponding to the initialization drop-down box
1 2 3 4 5 6 7 8 9 10 11 | [HttpPost]
public JsonResult GetCompantListForPhone()
{
compantList = 从数据库获取这个下拉框数据的集合;
return Json(compantList);
}
|
Copy after login
After the other two drop-down boxes are completed according to this method. You can query based on conditions. The following two are JavaScript and background methods used in conjunction.
5. Submit the query to the background, and then reassign the table based on the returned set.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 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) {
$('#todeListTBODY tr').remove();
$.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( "操作失败!" );
}
})
}
|
Copy after login
5.1 ActionResult corresponding to the query data
1 2 3 4 5 6 7 8 9 10 | [HttpPost]
public JsonResult PhoneSearchSubmit(string company, string dept, string group)
{
phoneList = 根据条件查询数据;
return Json(phoneList);
}
|
Copy after login
Related recommendations:
Ajax realizes the detailed explanation of the dynamic loading of the combo box example
JavaScript uses Ajax to dynamically load the CheckBox check box
How to implement it in Java Dynamically loaded code example
The above is the detailed content of Ajax realizes the sharing of the strength of dynamically loading data. For more information, please follow other related articles on the PHP Chinese website!