界面如下:
然后,click filter button后的UI:
看,筛选出来了,代码如下(js能调C#写的UserInfo类的各个属性)
private void btnSearch_Click(object sender, EventArgs e)
{
List<UserInfo> users = FilterUsers(txtFilter.Text.Trim());
lstUsers.DataSource = users;
}
private List<UserInfo> FilterUsers(string jsFilter)
{
List<UserInfo> results = new List<UserInfo>();
if (jsFilter.Trim().Length == 0)
return this.users;
try
{
using (JavascriptContext context = new JavascriptContext())
{
foreach (UserInfo user in this.users)
{
context.SetParameter(\"user\", user);
context.SetParameter(\"isInclude\", false);
string js = string.Format(@\"
if({0})
isInclude=true;
else
isInclude=false;\", jsFilter);
context.Run(js);
bool isInclude = (bool)context.GetParameter(\"isInclude\");
if (isInclude)
results.Add(user);
}
}
}
catch
{
txtFilter.Focus();
txtFilter.SelectAll();
return this.users;
}
return results;
}
本文地址:https://www.stayed.cn/item/17635
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我