GridView中的搜索选项,无需搜索按钮

时间:2017-06-01 04:41:06

标签: javascript jquery asp.net sql-server gridview

我有一个显示数据的网格我需要一个搜索过滤器,当我们输入时会自动过滤。无需搜索按钮。我尝试了一些我发现的JS代码,但似乎没有用。

<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="jquery/ASPSnippets_Pager.min.js"></script>
<script src="ASPSnippets_Pager.min.js" type="text/javascript"></script>
<script type="text/javascript">
 $(function () {
            GetStores(1);
        });
        $("[id*=txtSearch]").live("keyup", function () {
            GetStores(parseInt(1));
        });
        $(".Pager .page").live("click", function () {
            GetStores(parseInt($(this).attr('page')));
        });
        function SearchTerm() {
            return jQuery.trim($("[id*=txtSearch]").val());
        };
        function GetStores(pageIndex) {
            $.ajax({
                type: "POST",
                url: "Default.aspx/GetStores",
                data: '{searchTerm: "' + SearchTerm() + '", pageIndex: ' + pageIndex + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess,
                failure: function (response) {
                    alert(response.d);
                },
                error: function (response) {
                    alert(response.d);
                }
            });
        }
        var row;
        function OnSuccess(response) {
            var xmlDoc = $.parseXML(response.d);
            var xml = $(xmlDoc);
            var Stores = xml.find("Stores");
            if (row == null) {
                row = $("[id*=GridView1] tr:last-child").clone(true);
            }
            $("[id*=GridView1] tr").not($("[id*=GridView1] tr:first-child")).remove();
            if (Stores.length > 0) {
                $.each(Stores, function () {
                    var customer = $(this);
                    $("td", row).eq(0).html($(this).find("<%# Eval("zone") %>").text());
                    $("td", row).eq(1).html($(this).find("<%# Eval("displayName") %>").text());
                    $("td", row).eq(0).html($(this).find("StoreName").text());
                    $("td", row).eq(1).html($(this).find("ZoneName").text());
                    $("td", row).eq(0).html($(this).find("StoreNames").text());
                    $("td", row).eq(1).html($(this).find("ZoneNames").text());
                    $("[id*=GridView1]").append(row);
                    row = $("[id*=GridView1] tr:last-child").clone(true);
                });
                var pager = xml.find("Pager");
                $(".Pager").ASPSnippets_Pager({
                    ActiveCssClass: "current",
                    PagerCssClass: "pager",
                    PageIndex: parseInt(pager.find("PageIndex").text()),
                    PageSize: parseInt(pager.find("PageSize").text()),
                    RecordCount: parseInt(pager.find("RecordCount").text())
                });

                $(".ContactName").each(function () {
                    var searchPattern = new RegExp('(' + SearchTerm() + ')', 'ig');
                    $(this).html($(this).text().replace(searchPattern, "<span class = 'highlight'>" + SearchTerm() + "</span>"));
                });
            } else {
                var empty_row = row.clone(true);
                $("td:first-child", empty_row).attr("colspan", $("td", row).length);
                $("td:first-child", empty_row).attr("align", "center");
                $("td:first-child", empty_row).html("No records found for the search criteria.");
                $("td", empty_row).not($("td:first-child", empty_row)).remove();
                $("[id*=GridView1]").append(empty_row);
            }
        };
</script>

我试过这个,但页面继续加载似乎没有停止。我正在寻找替代方案。

0 个答案:

没有答案