This is a migrated thread and some comments may be shown as answers.

FilterTemplate automated for RadGrid

1 Answer 703 Views
Filter
This is a migrated thread and some comments may be shown as answers.
Joaquin
Top achievements
Rank 1
Joaquin asked on 18 Jun 2014, 07:00 PM
Aquí el código de una plantilla para automatizar el filtro de una columna de tipo fecha para el RadGrid. Los pasos son:

1. En el evento "ColumnCreated" del RadGrid en la columna especificada se le agrega la plantilla por ejemplo así:
Private Sub NombreRadGrid_ColumnCreated(sender As Object, e As GridColumnCreatedEventArgs) Handles rgdSolicitudes.ColumnCreated
   if e.Column Is GridDateTimeColumn And NombreRadGrid.MasterTableView.AllowFilteringByColumn then
      Dim itemDate As GridDateTimeColumn = TryCast(e.Column, GridDateTimeColumn)
      itemDate.FilterTemplate = New hrrFiltroRangoFecha(Page, itemDate.UniqueName, CDate(startDate), CDate(endDate), CDate("01-01-2014"), Now)
        'itemDate.UniqueName ES EL NOMBRE DE LA COLUMNA O DEL CAMPO
        'CDate(startDate) ES LA FECHA DE INICIO SELECCIONADA
        'CDate(endDate) ES LA FECHA FINAL SELECCIONADA
        'CDate("01-01-2014") ES LA FECHA MINIMA PARA SELECCIONAR
        'Now ES LA FECHA MAXIMA PARA SELECCIONAR
   end if
End Sub

2. En el evento "ItemCommand" del RadGrid, cuando el comando sea "RadGrid.FilterCommandName" sucede esto:
Private Sub NombreRadGrid_ItemCommand(sender As Object, e As GridCommandEventArgs) Handles rgdSolicitudes.ItemCommand
    If e.CommandName = RadGrid.FilterCommandName Then
       Dim item As GridFilteringItem = CType(e.Item, GridFilteringItem)
       For Each column As GridColumn In rgdSolicitudes.MasterTableView.RenderColumns
          If TypeOf column Is GridBoundColumn Then
               Dim boundColumn As GridBoundColumn = TryCast(column, GridBoundColumn)
               Select Case boundColumn.DataField
               Case "Fecha"  <------------------- (Es el nombre del campo o de la columna)
                   If boundColumn.CurrentFilterValue <> "" Then
                      Me.startDate = CType(Mid(boundColumn.CurrentFilterValue, 1, 10), Date)
                      Me.endDate = CType(Mid(boundColumn.CurrentFilterValue, 12, 10), Date)
                  End If
               End Select
          End If
      Next
    End If
End Sub

3. En el código de la pagina se agrega estas dos propiedades:
Protected Property startDate() As System.Nullable(Of DateTime)
    Get
       If ViewState("strD") IsNot Nothing Then
          Return DirectCast(ViewState("strD"), DateTime)
       Else
          Return New DateTime(2014, 1, 1) '<------- ESTA ES LA FECHA INICIAL MINIMA PARA EL FILTRO DE FECHA
      End If
    End Get
    Set(ByVal value As System.Nullable(Of DateTime))
        ViewState("strD") = value
    End Set
End Property

Protected Property endDate() As System.Nullable(Of DateTime)
    Get
       If ViewState("endD") IsNot Nothing Then
          Return DirectCast(ViewState("endD"), DateTime)
       Else
          Return New DateTime(Year(Now), Month(Now), Day(Now)) '<------ ESTA ES LA FECHA FINAL MAXIMA PARA EL FILTRO FECHA
       End If
     End Get
     Set(ByVal value As System.Nullable(Of DateTime))
         ViewState("endD") = value
      End Set
End Property

4. Se crea una clase por ejemplo con el nombre de "hrrFiltroRangoFecha":
Imports System.Web.UI.WebControls

Public Class hrrFiltroRangoFecha
Implements ITemplate
Private rdpFechaInicio As RadDatePicker
Private rdpFechaFinal As RadDatePicker
Private strColumna As String '<------ ES EL NOMBRE DE LA COLUMNA DE TIPO FECHA DEL RADGRID
 Private fchInicialMinima As Date '<------ ES LA FECHA MINIMA DEL DATEPICKER INICIAL
Private fchFinalMaxima As Date '<------ ES LA FECHA MAXIMA DEL DATEPICKER FINAL
Private fchInicial As Date '<----------- ES LA FECHA SELECCIONADA DEL DATEPICKER INICIAL
Private fchFinal As Date '<----------- ES LA FECHA SELECCIONADA DEL DATEPICKER FINAL
Protected Pagina As Page

Public Sub New(ByVal Pag As Page, ByVal strCol As String, ByVal fchIni As Date, ByVal fchFin As Date, ByVal fchIniMin As Date, ByVal fchFinMax As Date)
Pagina = Pag
strColumna = strCol
fchInicialMinima = fchIniMin
fchFinalMaxima = fchFinMax
fchInicial = fchIni
fchFinal = fchFin
rdpFechaInicio = New RadDatePicker()
rdpFechaFinal = New RadDatePicker()
ScriptManager.RegisterStartupScript(Pagina, Pagina.GetType(), "FormatoFecha", "function FormatoFecha(picker) {var date = picker.get_selectedDate(); var dateInput = picker.get_dateInput(); var formattedDate = dateInput.get_dateFormatInfo().FormatDate(date, dateInput.get_displayDateFormat());return formattedDate;}", True)
End Sub

Private Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
container.ClientIDMode = ClientIDMode.AutoID
Dim lbl1 As New Label
Dim lbl2 As New Label
lbl1.Text = "Del "
lbl2.Text = " al "
container.Controls.Add(lbl1)

rdpFechaInicio.EnableViewState = False
rdpFechaInicio.Width = 130
rdpFechaInicio.MinDate = fchInicialMinima 
rdpFechaInicio.MaxDate = fchFinalMaxima
rdpFechaInicio.FocusedDate = fchInicialMinima 
rdpFechaInicio.ToolTip = "Fecha de inicio"
rdpFechaInicio.ID = String.Format("FechaInicio{0}", strColumna)
rdpFechaInicio.DbSelectedDate = fchInicial
AddHandler rdpFechaInicio.DataBinding, AddressOf FechaInicio_DataBinding
rdpFechaInicio.ClientEvents.OnDateSelected = String.Format("FechaInicioSeleccionada_{0}", strColumna)
container.Controls.Add(rdpFechaInicio)

container.Controls.Add(lbl2)

rdpFechaFinal.EnableViewState = False
rdpFechaFinal.Width = 130
rdpFechaFinal.MinDate = fchInicialMinima 
rdpFechaFinal.MaxDate = fchFinalMaxima 
rdpFechaFinal.FocusedDate = Now
rdpFechaFinal.ToolTip = "Fecha de final"
rdpFechaFinal.ID = String.Format("FechaFinal{0}", strColumna)
rdpFechaFinal.DbSelectedDate = fchFinal
AddHandler rdpFechaFinal.DataBinding, AddressOf FechaFinal_DataBinding
rdpFechaFinal.ClientEvents.OnDateSelected = String.Format("FechaFinalSeleccionada_{0}", strColumna)
container.Controls.Add(rdpFechaFinal)
End Sub

Public Sub FechaInicio_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim rdpInicio As RadDatePicker = DirectCast(sender, RadDatePicker)
Dim contenido As GridItem = DirectCast(rdpInicio.NamingContainer, GridItem)
Dim contenidoFinal As GridItem = DirectCast(rdpFechaFinal.NamingContainer, GridItem)
Dim script As String = "function FechaInicioSeleccionada_" + strColumna + "(sender,args) {var tableView=$find(""" + TryCast(contenido, GridItem).OwnerTableView.ClientID + """);var ToPicker = $find(""" + TryCast(contenidoFinal, GridItem).FindControl(String.Format("FechaFinal{0}", strColumna)).ClientID + """);var fromDate = FormatoFecha(sender);var toDate = FormatoFecha(ToPicker);tableView.filter(""" + strColumna + """, fromDate + "" "" + toDate, ""Between"");}"
ScriptManager.RegisterStartupScript(Pagina, Pagina.GetType(), String.Format("FechaInicioSeleccionada_{0}", strColumna), script, True)
End Sub

Public Sub FechaFinal_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim rdpFinal As RadDatePicker = DirectCast(sender, RadDatePicker)
Dim contenido As GridItem = DirectCast(rdpFinal.NamingContainer, GridItem)
Dim contenidoInicio As GridItem = DirectCast(rdpFechaInicio.NamingContainer, GridItem)
Dim script As String = "function FechaFinalSeleccionada_" + strColumna + "(sender,args) {var tableView=$find(""" + TryCast(contenido, GridItem).OwnerTableView.ClientID + """);var FromPicker = $find(""" + TryCast(contenidoInicio, GridItem).FindControl(String.Format("FechaInicio{0}", strColumna)).ClientID + """);var fromDate = FormatoFecha(FromPicker);var toDate = FormatoFecha(sender);tableView.filter(""" + strColumna + """, fromDate + "" "" + toDate, ""Between"");}"
ScriptManager.RegisterStartupScript(Pagina, Pagina.GetType(), String.Format("FechaFinalSeleccionada_{0}", strColumna), script, True)
End Sub
End Class

EL UNICO PROBLEA QUE TIENE ES QUE SE PIERDE EL FILTRO CUANDO SE QUIERE FILTRAR AL MISMO TIEMPO OTRA COLUMNA. ALGUIEN QUE PUEDA APOYAR Y SOLUCIONAR EL PROBLEMA. POR OTRO LADO TENGO OTROS FILTROS CON PLANTILLAS PARA EL RADGRID (RADCOMBOBOX Y RADSLIDER) QUE DESPUES PUBLICARÉ.

1 Answer, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 23 Jun 2014, 03:00 PM
Hi Joaquin,

The official language used for communication in Telerik is English. This is why I would like to kindly ask you to rewrite your question in English and in future to use this language when you are contacting us, so we can understand your inquiry and help you with it.

Regards,
Pavlina
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Filter
Asked by
Joaquin
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
Share this question
or