最近使用到了全文搜索功能,这里对这一功能做个简单介绍。
什么叫做全文搜索呢,效果如下:
在搜索框中输入任意汉字或者词组,系统会自动在特定数据库中检索包含这个字或者词组的信息。
我的实现过程是将这个功能单独做成了一个控件,使用的使用直接引用这个控件,然后给数据源赋值,并且重写数据校验方法即可。
实现代码如下:
<span style="white-space:pre"> </span>protected override void AutoCompleteExtender_GetDataSource(string sPrefix, int iCount, object context, ref IEnumerable result)
{
if (GetDataSource != null)
{
var dataSource = GetDataSource(sPrefix, context);
if (dataSource != null)
{
var typeName = "";
foreach (var data in dataSource)
{
typeName = data.GetType().AssemblyQualifiedName;
break;
}
result = new object[] { dataSource, typeName };
}
}
}
protected override string SaveClientState()
{
string result = string.Empty;
if (this.SelectedData != null)
{
result = JSONSerializerExecute.SerializeWithType(this.SelectedData);
}
return result;
}
protected override void LoadClientState(string clientState)
{
if (!string.IsNullOrEmpty(clientState))
{
IList state = JSONSerializerExecute.Deserialize<IList>(clientState);
this.SelectedData = state;
}
}
/// <summary>
/// 从客户端回掉,验证输入的内容
/// </summary>
/// <param name="chkString">通过这个参数传入的信息进行数据校验</param>
/// <param name="context">用户设置的上下文</param>
[ScriptControlMethod]
public virtual object[] CheckInput(string chkString, object context)
{
if (ValidateInput != null)
{
var data = ValidateInput(chkString, context);
if (data.Count > 0)
{
object[] result = new object[] { data, data[0].GetType().AssemblyQualifiedName };
return result;
}
}
return null;
}
页面javascript
//回掉后台验证成功后调用这个进行处理,如果结果为1个则,直接创建SPAN并显示,多个则弹出对话框让用户进行选择
_onValidateInvokeComplete: function (result) {
this._setInvokingStatus(false);
if (this._autoCompleteControl) {
this._autoCompleteControl.set_isInvoking(false);
}
var obj = null;
if (result && result.length == 2) {
var data = result[0];
var dataType = result[1];
if (data.length > 1)//多于一个
{
for (var i = 0; i < data.length; i++) {
data[i].__type = dataType;
}
var sFeature = "dialogWidth:500px; dialogHeight:300px;center:yes;help:no;resizable:yes;scroll:no;status:no";
data.nameTable = $NT;
data.keyName = this.get_dataKeyName();
data.displayPropName = this.get_dataDisplayPropName();
data.descriptionPropName = this.get_dataDescriptionPropName();
var resultStr = window.showModalDialog(this.get_selectObjectDialogUrl(), data, sFeature);
obj = data[resultStr];
}
else {
obj = data[0];
obj.__type = dataType;
}
}
if (obj != null) {
this._tmpText = "";
if (this._multiSelect == false) {
this.set_selectedData(new Array());
}
if (this._allowSelectDuplicateObj || !this._checkDataInList(obj))
Array.add(this.get_selectedData(), obj);
this.notifyDataChanged();
}
this.setInputAreaText();
},
loadClientState: function (value) {
if (value && value != "") {
var state = Sys.Serialization.JavaScriptSerializer.deserialize(value);
if (state != null && state.length) {
this.set_selectedData(state);
}
}
},
saveClientState: function () {
var state = "";
var data = this.get_selectedData();
for (var i = 0; i < data.length; i++) {
data[i]._dataType = undefined;
}
if (data) {
state = Sys.Serialization.JavaScriptSerializer.serialize(data);
}
return state;
},
dataBind: function () {
this.setInputAreaText();
},
add_checkDataMask: function (handler) {
this.get_events().addHandler("onCheckDataMask", handler);
},
remove_checkDataMask: function (handler) {
this.get_events().removeHandler("onCheckDataMask", handler);
}
}
数据校验
protected IEnumerable CommonAutoCompleteWithSelectorControl1_GetDataSource(string chkstring, object context)
{
IEnumerable result = null;
if (chkstring.IsNotWhiteSpace())
result = OtherFieldData.SearchKeyPointInfoByCnName(chkstring);
return result;
}
protected IList CommonAutoCompleteWithSelectorControl1_ValidateInput(string chkstring, object context)
{
IList result = null;
if (chkstring.IsNotWhiteSpace())
result = OtherFieldData.SearchKeyPointInfoByCnName(chkstring);
return result;
}
将数据显示功能作为公共部分抽取出来,然后封装起来,留下数据源绑定借口和数据校验借口,以实现控件的复用。