Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Tuesday, January 15, 2013

Search within a SpGridView or GridView using JQuery




Ascx
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SJSPGridUserControl.ascx.cs" Inherits="SJSPGrid.SJSPGrid.SJSPGridUserControl" %>
<script language="javascript" type="text/javascript" src="/UserPages/JS/jquery.min.1.8.3.js"></script>
<script language="javascript" type="text/javascript" src="/UserPages/JS/JQuerySearch.JS"></script>

<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td>
<span style="vertical-align:top;">Enter Keyword to Search</span><span><input type="text" id="txtSearch" /></span>
</td>
</tr>
<tr>
<td align="center">
<SharePoint:SPGridView ID="sgvEmployeeSearchJQuery" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="Employee ID" DataField="Title" />
<asp:BoundField HeaderText="Employee Name" DataField="EmpName" />
<asp:BoundField HeaderText="Employee Age" DataField="EmpAge" />
</Columns>
</SharePoint:SPGridView>
</td>
</tr>
</table>

Ascx.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;

namespace SJSPGrid.SJSPGrid
{
    public partial class SJSPGridUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SPWeb currentWeb = SPContext.Current.Web;
            SPList lstEmployee = currentWeb.Lists["Employee Details Search"];
            SPQuery sQuery = new SPQuery();
            sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
            SPListItemCollection myColl = lstEmployee.GetItems(sQuery);
            if (myColl.Count > 0)
            {
                sgvEmployeeSearchJQuery.DataSource = myColl.GetDataTable();
                sgvEmployeeSearchJQuery.DataBind();
            }
        }
    }
}


JS
$(document).ready(function()
                     {
                           $('#txtSearch').keyup(function()
                           {
                                  searchTable($(this).val());
                           });
                     });
                     function searchTable(inputVal)
                     {
                           //'<%=sgvEmployeeSearchJQuery.ClientID%>'      
                           var table = $('#SpGridViewID');
                           table.find('tr').each(function(index, row)
                           {
                                  var allCells = $(row).find('td');
                                  if(allCells.length > 0)
                                  {
                                         var found = false;
                                         allCells.each(function(index, td)
                                         {
                                                var regExp = new RegExp(inputVal, 'i');
                                                if(regExp.test($(td).text()))
                                                {
                                                       found = true;
                                                       return false;
                                                }
                                         });
                                         if(found == true)$(row).show();else $(row).hide();
                                  }
                           });
                     }


2 comments:

  1. var table = $('#SpGridViewID');//What needs to fill here
    table.find('tr').each(function(index, row)//this line giving null exception

    ReplyDelete
  2. i am working in spgridview and wants to search. Please provide me working code

    ReplyDelete