Asp.net为ListView控件实现搜索功能
我有一个 ListView,其中包含编辑、删除和添加。这里一切都很好,但是列表太大,我想为用户提供带有文本框和按钮的搜索功能。
当用户单击搜索按钮时,列表视图将按搜索条件进行过滤。
有人可以帮助我实现这一目标吗? 谢谢
I've got a ListView which contains edit,delete and add. All good here, however the List is too large and I would like give users a serach functionality with text box and button.
When user clicks on search button, List view gets filtered by search criteria.
could someone help me to achieve this please.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
(回应这个问题的评论...)
很大程度上取决于你的 DOM 结构。您需要知道
ListView
如何布置其元素。例如,如果它们都是div
元素,那么您需要了解 JavaScript 代码的情况。 (我将假设使用 jQuery,因为现在这是一个安全的假设。)本质上,您的过滤器将至少有一个文本输入元素:
您还可以有一个按钮来使用过滤器,但为了简洁起见让我们根据用户输入进行过滤:
对于过滤本身,您可以使用
:contains()
选择器。请在此处查看相关信息。基本上,您会隐藏所有元素,然后显示匹配的元素。像这样的东西(未经测试):这个想法是隐藏所有子 div(您的选择器可能需要更具体,具体取决于您的 DOM),然后显示与过滤器匹配的子 div。当然,不要忘记有一个默认情况,如果过滤器文本为空,则显示所有内容。
(In response to the comments on the question...)
Depends a lot on your DOM structure. You'll need to know how the
ListView
has laid out its elements. For example, if they're alldiv
elements then you'll need to know that for your JavaScript code. (I'm going to assume the use of jQuery, because it's a safe assumption these days.)Essentially, your filter is going to have at least a text input element:
You can also have a button to engage the filter, but for brevity let's just filter as the user types:
For the filtering itself, you could use the
:contains()
selector. See information about it here. Basically, you'd hide all of the elements and then show the ones which match. Something like this (untested):The idea is to hide all of the child divs (your selectors may need to be more specific, depending on your DOM) and then show the ones which match the filter. Don't forget, of course, to have a default case to show all if the filter text is empty.
嗯,你必须了解你的底层结构;假设您正在渲染一个表格,您需要编写 JavaScript 来循环每一行并执行以下操作:
Well, you have to know your underlying structure; say you are rendering a table, you need to write JavaScript to loop through each row and do something like:
这取决于你如何渲染 ListView,但如果你渲染一个表格并想要在客户端进行过滤,你可以使用 jQuery 插件,例如 UI表格过滤器
It depends on how you render your ListView but if you render a table and want to do the filtering client side you could use a jQuery plugin such as UI Table Filter
最终使用了这个:
ended up using this: