使用具有自动完成功能的ajax [英] Using ajax with autocomplete feature

查看:67
本文介绍了使用具有自动完成功能的ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

引用:

任何人都知道当用户键入某个字符时,如何从存储过程中获取设备的名称,例如G它应该显示基于字母G的名称。顺便说一句,我目前正在使用POST方法从数据库中检索设备名称

anyone know how can i get the name of the device from the stored procedure when the user type a certain character for example G and it should show the name based on the letter G. By the way, i am currently using the POST Method in retrieving the device name from the database





////db.cs



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Data;

使用System.Data.SqlClient;

使用System.Configuration;



namespace Prototype3.Database_Access_Data

{

public class db

{

SqlConnection con = newSqlConnection(ConfigurationManager.ConnectionStrings [localhost]。ConnectionString);

public void SendLocation(Loc as cs)

{



SqlCommand com = new SqlCommand(SendGPS,con);

< br $>


com.CommandType = CommandType.StoredProcedure;

com.Parameters.AddWithValue(@ DeviceImei,cs.DeviceImei);

com.Parameters.AddWithValue(@ Latitude,cs.Latitude);

com.Parameters.AddWithValue(@ Longitude,cs.Longitude);

com.Parameters.AddWithValue(@ Distance,cs.Distance);

com.Parameters.AddWithValue(@ LocationSend,cs.LocationSend);

con.Open();

com.Connection = con;

com.ExecuteNonQuery();

con.Close(); < br $>


}



公共DataTable LocationHistory(LocationHistory cs)

{

DataTable dt = new DataTable();



using(SqlCommand sqlCmd = new SqlCommand(GetLocationHistory,con))

{

sqlCmd.CommandType = CommandType.StoredProcedure;

sqlCmd。 Parameters.Add(new SqlParameter(DeviceImei,cs.DeviceImei));



try

{

con.Open();

dt.Load(sqlCmd.ExecuteReader(CommandBehavior.CloseConnection));

}

catch(SqlException ex )

{

//在你认为合适时处理错误

}

}



返回dt;

}

公共DataSet GetUser()

{

SqlCommand com = new SqlCommand(GetUser,con);

com.CommandType = CommandType.StoredProcedure;

SqlDataAdapter da = new SqlDataAdapter(com);

DataSet ds = new DataSet();

da.Fill(ds);

return ds ;





}

public void SendDistance(Location cs)

{



SqlCommand com = new SqlCommand(SendDistance,con);





com.CommandType = CommandType.StoredProcedure;

com.Parameters.AddWithValue(@ DeviceImei,cs.DeviceImei);

com.Parameters.AddWithValue( @Distance,cs.Distance);

com.Parameters.AddWithValue(@ LocationSend,cs.LocationSend);

con.Open();

com.Connection = con;

com.ExecuteNonQuery();

con.Close();

}



公共DataTable FlagingDevice(FlagingDevice cs)

{

DataTable dt = new DataTable();



使用(SqlCommand sqlCmd = new SqlCommand(FlagingDevice,con))

{

sqlCmd.CommandType = CommandType.StoredProcedure;

sqlCmd.Parameters.Add(new SqlParameter(Login,cs.Login));



试试

{

con.Open();

dt。加载(sqlCmd.ExecuteReader(CommandBehavior.CloseConnection));

}

catch(SqlException ex)

{

//在你认为合适时处理错误

}

}



返回dt;

}

public DataTable SearchByOfficerName(SearchHistory cs)

{

DataTable dt = new DataTable() ;



使用(SqlCommand sqlCmd = new SqlCommand(SearchByOfficer,con))

{

sqlCmd .CommandType = CommandType.StoredProcedure;

sqlCmd.Parameters.Add(new SqlParameter(Name,cs.Name));

sqlCmd.Parameters.Add(new SqlParameter (StationID,cs.StationID));

sqlCmd.Parameters.Add(new SqlParameter(ID,cs.DivisionID));

sqlCmd.Parameters。添加(新的SqlParameter(DeviceName,cs.DeviceName));



尝试

{

con .Open();

dt.Load(sqlCmd.ExecuteReader(CommandBehavior.CloseConnection));

}

catch(SqlException ex)

{

//在你认为合适时处理错误

}

}



返回dt;

}

公共DataTable SearchByDeviceName(SearchHistory cs)

{

DataTable dt = new DataTable();



使用(SqlCommand sqlCmd = new SqlCommand(SearchByDeviceName,con))

{

sqlCmd.CommandType = CommandType.StoredProcedure;

sqlCmd.Parameters.Add(new SqlParameter(Name,cs.OfficerName) );

sqlCmd.Parameters.Add(new SqlParameter(StationID,cs.StationID));

sqlCmd.Parameters.Add(new SqlParameter(DivisionID, cs.DivisionID));

sqlCmd.Parameters.Add(new SqlParameter(DeviceName,cs.DeviceName));

sqlCmd.Parameters.Add(new SqlParameter( BoxID,cs.BoxID));

试试

{

con.Open();

dt.Load(sqlCmd.ExecuteReader(CommandBehavior.CloseConnection));

}

catch(SqlException ex)

{

//处理你认为合适的错误

}

}



返回dt;

}

公共DataTable SearchByElectoralDivision(SearchHistory cs)

{

DataTable dt = new DataTable();



使用(SqlCommand sqlCmd = new SqlCommand(SearchByElectoralDivision,con))

{

sqlCmd.CommandType = CommandType.StoredProcedure;

sqlCmd.Parameters.Add(new SqlParameter(DivisionID,cs.DivisionID));

sqlCmd.Parameters。添加(new SqlParameter(Name,cs.Name));

sqlCmd.Parameters.Add(new SqlPara meter(StationID,cs.StationID));

sqlCmd.Parameters.Add(new SqlParameter(DeviceName,cs.DeviceName));



尝试

{

con.Open();

dt.Load(sqlCmd.ExecuteReader(CommandBehavior.CloseConnection)) ;

}

catch(SqlException ex)

{

//处理你认为合适的错误

}

}



返回dt;

}

public DataTable SearchByPollingStation(SearchHistory cs)

{

DataTable dt = new DataTable();



使用(SqlCommand sqlCmd = new SqlCommand(SearchByPollingStation,con))

{

sqlCmd.CommandType = CommandType.StoredProcedure;

sqlCmd.Parameters.Add(new SqlParameter(StationID,cs.StationID));

sqlCmd.Parameters.Add(new SqlParameter(OfficerName,cs.OfficerName) );

sqlCmd.Parameters.Add(new SqlParameter(DivisionID,cs.DivisionID));

sqlCmd.Parameters.Add(new SqlParameter(DeviceName, cs.DeviceName));



尝试

{

con.Open();

dt.Load(sqlCmd.ExecuteReader(CommandBehavior.CloseConnection));

}

catch(SqlException ex)

{

//在你认为合适的情况下处理错误

}

}



返回dt;

}

public void SendBox(Box cs)

{

SqlCommand com = new SqlCommand(SendBox,con);





com.CommandType = CommandType.StoredProcedure;

com .Parameters.AddWithValue(@ Id,cs.Id);

com.Parameters.AddWithValue(@ StationID,cs.PollingStationID);

com.Parameters .AddWithValue(@ DeviceImei,cs.DeviceImei);

con.Open();

com.Connection = con;

com .ExecuteNonQuery();

con.Close();

}



public DataSet FlagingDeviceWithoutParameters()

{

SqlCommand com = new SqlCommand(FlagingDevice(Without Parameters),con);

com.CommandType = CommandType.StoredProcedure;

SqlDataAdapter da = new SqlDataAdapter(com);

DataSet ds = new DataSet();

da.Fill(ds);

返回ds;

}

公共DataTable SearchByBox(SearchHistory cs)

{



DataTable dt =新的DataTable();



使用(SqlCommand sqlCmd = new SqlCommand(SearchByBox,con))

{

sqlCmd.CommandType = CommandType.StoredProcedure;

sqlCmd.Parameters.Add(new SqlParameter(DeviceName,cs.DeviceName));

sqlCmd.Parameters。添加(新的SqlParameter(StationID,cs.StationID));

sqlCmd.Parameters.Add(new SqlParameter(ElectoralDivisionID,cs.ElectoralDivisionID));

sqlCmd.Parameters.Add(new SqlParameter(OfficerName,cs.OfficerName));



try

{

con.Open();

dt.Load(sqlCmd.ExecuteReader(CommandBehavior.CloseConnection));

}

catch(SqlException ex)

{

}

返回dt;

}



}

}

}



///// ValuesController.cs





使用Prototype3.Database_Access_Data;

使用系统;

使用System.Collections.Generic;

使用System.Data;

使用System.Linq;

使用System.Net;

使用System.Net.Http;

使用System.Threading.Tasks;

使用System.Web.Http;

使用System.Web.Http.Description;



namespace Prototype3.Controllers

{

public class ValuesController:ApiController

{

Database_Access_Data.db dblayer = new Datab ase_Access_Data.db();



[HttpPost]

[路线(api / Values / SendLocation)]

public IHttpActionResult SendLocation([FromBody]位置cs)

{

尝试

{

if( !ModelState.IsValid)

{

返回BadRequest(ModelState);

}

dblayer.SendLocation(cs );

返回Ok(成功);

}

catch(例外e)

{

返回Ok(出错了+ e);

}



}



[HttpPost]

[路线(api / Values / GetLocationHistory)]

公共IHttpActionResult GetLocationHistory([FromBody] LocationHistory cs)

{

试试

{

if(!ModelState.IsValid)

{

返回BadRequest(ModelState);

}

var table = dblayer.LocationHistory(cs);

返回Ok(表格);

}

catch(例外e)

{

返回确定(事情变得错误+ e);

} < br $>


}

[HttpPost]

[路线(api / Values / SendDistance)]

public IHttpActionResult SendDistance([FromBody]位置cs)

{

试试

{

if(!ModelState.IsValid)

{

返回BadRequest(ModelState);

}

dblayer.SendDistance (cs);

返回Ok(成功);

}

catch(例外e)

{

返回Ok(有些事情错了+ e);

}



}



[HttpGet]

[路线(api / Values / GetUser)]

公共DataSet GetUser()

{

DataSet ds = dblayer.GetUser();

返回ds;

}

[HttpPost]

[路线(api / Values / FlagingDevice)]

公共IHttpActionResult FlagingDevice([FromBody] FlagingDevice cs)

{

尝试

{

if(!ModelState.IsValid)

{

返回BadRequest(ModelState);

}

var table = dblayer.FlagingDevice (cs);

返回Ok(表格);



}

catch(例外e)

{

返回确定(事情变得错误+ e);

}



}



[HttpPost]

[路线(api / Values / SendBox)]

public IHttpActionResult SendBox([FromBody] Box cs)

{

try

{

if(!ModelState。 IsValid)

{

返回BadRequest(ModelState);

}

dblayer.SendBox(cs);



返回Ok(成功);

}

catch(例外e)

{

返回确定(事情变得错误+ e);

}



}



[HttpGet]

[路线(api / Values / FlagingDevice(无参数)))

public DataSet FlagingDevice1()

{

DataSet ds = dblayer.FlagingDeviceWithoutParameters();

return ds;

}















}

}



我尝试了什么:



我试图在线研究解决方案,但无济于事。



////db.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Prototype3.Database_Access_Data
{
public class db
{
SqlConnection con = newSqlConnection(ConfigurationManager.ConnectionStrings["localhost"].ConnectionString);
public void SendLocation(Location cs)
{

SqlCommand com = new SqlCommand("SendGPS", con);


com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@DeviceImei", cs.DeviceImei);
com.Parameters.AddWithValue("@Latitude", cs.Latitude);
com.Parameters.AddWithValue("@Longitude", cs.Longitude);
com.Parameters.AddWithValue("@Distance", cs.Distance);
com.Parameters.AddWithValue("@LocationSend", cs.LocationSend);
con.Open();
com.Connection = con;
com.ExecuteNonQuery();
con.Close();

}

public DataTable LocationHistory(LocationHistory cs)
{
DataTable dt = new DataTable();

using (SqlCommand sqlCmd = new SqlCommand("GetLocationHistory", con))
{
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.Add(new SqlParameter("DeviceImei", cs.DeviceImei));

try
{
con.Open();
dt.Load(sqlCmd.ExecuteReader(CommandBehavior.CloseConnection));
}
catch (SqlException ex)
{
//Handle errors as you see fit
}
}

return dt;
}
public DataSet GetUser()
{
SqlCommand com = new SqlCommand("GetUser", con);
com.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;


}
public void SendDistance(Location cs)
{

SqlCommand com = new SqlCommand("SendDistance", con);


com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@DeviceImei", cs.DeviceImei);
com.Parameters.AddWithValue("@Distance", cs.Distance);
com.Parameters.AddWithValue("@LocationSend", cs.LocationSend);
con.Open();
com.Connection = con;
com.ExecuteNonQuery();
con.Close();
}

public DataTable FlagingDevice(FlagingDevice cs)
{
DataTable dt = new DataTable();

using (SqlCommand sqlCmd = new SqlCommand("FlagingDevice", con))
{
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.Add(new SqlParameter("Login", cs.Login));

try
{
con.Open();
dt.Load(sqlCmd.ExecuteReader(CommandBehavior.CloseConnection));
}
catch (SqlException ex)
{
//Handle errors as you see fit
}
}

return dt;
}
public DataTable SearchByOfficerName(SearchHistory cs)
{
DataTable dt = new DataTable();

using (SqlCommand sqlCmd = new SqlCommand("SearchByOfficer", con))
{
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.Add(new SqlParameter("Name", cs.Name));
sqlCmd.Parameters.Add(new SqlParameter("StationID", cs.StationID));
sqlCmd.Parameters.Add(new SqlParameter("ID", cs.DivisionID));
sqlCmd.Parameters.Add(new SqlParameter("DeviceName", cs.DeviceName));

try
{
con.Open();
dt.Load(sqlCmd.ExecuteReader(CommandBehavior.CloseConnection));
}
catch (SqlException ex)
{
//Handle errors as you see fit
}
}

return dt;
}
public DataTable SearchByDeviceName(SearchHistory cs)
{
DataTable dt = new DataTable();

using (SqlCommand sqlCmd = new SqlCommand("SearchByDeviceName", con))
{
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.Add(new SqlParameter("Name", cs.OfficerName));
sqlCmd.Parameters.Add(new SqlParameter("StationID", cs.StationID));
sqlCmd.Parameters.Add(new SqlParameter("DivisionID", cs.DivisionID));
sqlCmd.Parameters.Add(new SqlParameter("DeviceName", cs.DeviceName));
sqlCmd.Parameters.Add(new SqlParameter("BoxID", cs.BoxID));
try
{
con.Open();
dt.Load(sqlCmd.ExecuteReader(CommandBehavior.CloseConnection));
}
catch (SqlException ex)
{
//Handle errors as you see fit
}
}

return dt;
}
public DataTable SearchByElectoralDivision(SearchHistory cs)
{
DataTable dt = new DataTable();

using (SqlCommand sqlCmd = new SqlCommand("SearchByElectoralDivision", con))
{
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.Add(new SqlParameter("DivisionID", cs.DivisionID));
sqlCmd.Parameters.Add(new SqlParameter("Name", cs.Name));
sqlCmd.Parameters.Add(new SqlParameter("StationID", cs.StationID));
sqlCmd.Parameters.Add(new SqlParameter("DeviceName", cs.DeviceName));

try
{
con.Open();
dt.Load(sqlCmd.ExecuteReader(CommandBehavior.CloseConnection));
}
catch (SqlException ex)
{
//Handle errors as you see fit
}
}

return dt;
}
public DataTable SearchByPollingStation(SearchHistory cs)
{
DataTable dt = new DataTable();

using (SqlCommand sqlCmd = new SqlCommand("SearchByPollingStation", con))
{
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.Add(new SqlParameter("StationID", cs.StationID));
sqlCmd.Parameters.Add(new SqlParameter("OfficerName", cs.OfficerName));
sqlCmd.Parameters.Add(new SqlParameter("DivisionID", cs.DivisionID));
sqlCmd.Parameters.Add(new SqlParameter("DeviceName", cs.DeviceName));

try
{
con.Open();
dt.Load(sqlCmd.ExecuteReader(CommandBehavior.CloseConnection));
}
catch (SqlException ex)
{
//Handle errors as you see fit
}
}

return dt;
}
public void SendBox(Box cs)
{
SqlCommand com = new SqlCommand("SendBox", con);


com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@Id", cs.Id);
com.Parameters.AddWithValue("@StationID", cs.PollingStationID);
com.Parameters.AddWithValue("@DeviceImei", cs.DeviceImei);
con.Open();
com.Connection = con;
com.ExecuteNonQuery();
con.Close();
}

public DataSet FlagingDeviceWithoutParameters()
{
SqlCommand com = new SqlCommand("FlagingDevice(Without Parameters)", con);
com.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
public DataTable SearchByBox(SearchHistory cs)
{

DataTable dt = new DataTable();

using (SqlCommand sqlCmd = new SqlCommand("SearchByBox", con))
{
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.Add(new SqlParameter("DeviceName", cs.DeviceName));
sqlCmd.Parameters.Add(new SqlParameter("StationID", cs.StationID));
sqlCmd.Parameters.Add(new SqlParameter("ElectoralDivisionID", cs.ElectoralDivisionID));
sqlCmd.Parameters.Add(new SqlParameter("OfficerName", cs.OfficerName));

try
{
con.Open();
dt.Load(sqlCmd.ExecuteReader(CommandBehavior.CloseConnection));
}
catch (SqlException ex)
{
}
return dt;
}

}
}
}

///// ValuesController.cs


using Prototype3.Database_Access_Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;

namespace Prototype3.Controllers
{
public class ValuesController : ApiController
{
Database_Access_Data.db dblayer = new Database_Access_Data.db();

[HttpPost]
[Route("api/Values/SendLocation")]
public IHttpActionResult SendLocation([FromBody]Location cs)
{
try
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
dblayer.SendLocation(cs);
return Ok("Success");
}
catch (Exception e)
{
return Ok("Something went Wrong" + e);
}

}

[HttpPost]
[Route("api/Values/GetLocationHistory")]
public IHttpActionResult GetLocationHistory([FromBody]LocationHistory cs)
{
try
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var table = dblayer.LocationHistory(cs);
return Ok(table);
}
catch (Exception e)
{
return Ok("Something went Wrong" + e);
}

}
[HttpPost]
[Route("api/Values/SendDistance")]
public IHttpActionResult SendDistance([FromBody]Location cs)
{
try
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
dblayer.SendDistance(cs);
return Ok("Success");
}
catch (Exception e)
{
return Ok("Something went Wrong" + e);
}

}

[HttpGet]
[Route("api/Values/GetUser")]
public DataSet GetUser()
{
DataSet ds = dblayer.GetUser();
return ds;
}
[HttpPost]
[Route("api/Values/FlagingDevice")]
public IHttpActionResult FlagingDevice([FromBody]FlagingDevice cs)
{
try
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var table = dblayer.FlagingDevice(cs);
return Ok(table);

}
catch (Exception e)
{
return Ok("Something went Wrong" + e);
}

}

[HttpPost]
[Route("api/Values/SendBox")]
public IHttpActionResult SendBox([FromBody]Box cs)
{
try
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
dblayer.SendBox(cs);

return Ok("Success");
}
catch (Exception e)
{
return Ok("Something went Wrong" + e);
}

}

[HttpGet]
[Route("api/Values/FlagingDevice(WithoutParameters)")]
public DataSet FlagingDevice1()
{
DataSet ds = dblayer.FlagingDeviceWithoutParameters();
return ds;
}







}
}

What I have tried:

I have tried to research for solution online but to no avail.

推荐答案

尝试使用此代码自动完成文本框

Try with this code for auto complete textbox
<input type="text" value="" placeholder="Search" id="txtSearch" />


(document).ready(function(){
(document).ready(function () {


(#txtSearch)。autocomplete({
source:function(request,request,响应){
("#txtSearch").autocomplete({ source: function (request, response) {


这篇关于使用具有自动完成功能的ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆