How to Filter a Datatable

How I can filter a Datatable?

You can use DataView.

DataView dv = new DataView(yourDatatable);
dv.RowFilter = "query"; // query example = "id = 10"


http://www.csharp-examples.net/dataview-rowfilter/

Filter data from DataTable

Simply check if your Select returns anything?

 DataTable dtt = (DataTable)Session["ByBrand"];
DataRow[] rows = dtt.Select("Price >= " + HiddenField1.Value + " and Price <= " + HiddenField2.Value + "");
if(rows.Length > 0)
{
var filldt = rows.CopyToDataTable();
}

Well, the Linq example from Tim is really nice, but to complete my answer.
The Select method returns Always a DataRow array also if there is no row selected, but then you cannot ask to build a datatable from this empty array. Think about it. What schema the CopyToDataTable should build for the resulting table if no rows are present in the array?

Column filtering DataTable, remove specific column filter

To avoid drawing the control in the first place, take a look at this line:

$('#table thead tr:eq(0) th').each( function (i) { 

Here, i represents a loop counter. When the counter is 0, you are building the input control for column index 0 (the first column). You can therefore use an if(i > 0) { ... } statement inside that function to ignore the first iteration of the loop.

Because you are cloning a heading row which already contains a checkbox in the first column, you may also need to remove the "cloned" checkbox using $( this ).empty();.

$('#table thead tr:eq(0) th').each(function(i) {

if (i == 0) {
$( this ).empty();
} else {
var title = $(this).text();

$(this).html('<input type="text" />');

$('input', this).on('keyup change', function() {
if (table.column(i).search() !== this.value) {
table
.column(i)
.search(this.value)
.draw();
}
});
}

});

How to correctly filter a datatable (datatable.select)

dt.Select() returns an array of DataRows.

Why don't you use a DataView?

 DataView dv = new DataView(dt);
dv.RowFilter = "GroupingID = 0";
GridView1.DataSource = dv;

How to filter and style a data table

We could just wrap the datatable code around the get(input$df_test)

library(shiny)
library(dplyr)

df1 <- data.frame(Name = c("a", "b", "c"),
Parameter = c("pH", "pH", "pH"),
Amount = c(7, 7.5, 6.8),
Violation = c("No", "No", "Yes"))

df2 <- data.frame(Name = c("d", "e", "f"),
Parameter = c("pH", "pH", "pH"),
Amount = c(6.7, 7.2, 7.3),
Violation = c("Yes", "No", "No"))

ui = fluidPage(

titlePanel("Title"),

fluidRow(
selectInput(inputId = "df_test",
label = "Select DF",
choices = c("DF 1" = "df1",
"DF 2" = "df2"),
selected = "DF 1",
width = "50%"),


DT::dataTableOutput("test_table")
)
)

server <- function(input, output) {

output$test_table <- DT::renderDataTable({


DT::datatable(get(input$df_test), filter = "top",
options = list(pageLength = 25, autoWidth = TRUE)) %>%
DT::formatStyle("Violation", valueColumns = "Violation", color = DT::styleEqual("Yes", 'red'))
})
}
shinyApp(ui, server)

-ouput

Sample Image

How to Filter Data Table Rows with condition on column of Type list() in R

You can use sapply function to check if any of the values in vals is in Product for each row:

vals = c("UG12210","UG10000-WISD")

dt[Period %chin% "2018-Q1" & sapply(Product, function(v) any(vals %chin% v))]

# Id Period Product
# 1: 1000797366 2018-Q1 UG10000-WISD
# 2: 1000797366 2018-Q1 NX11100,UG10000-WISD,UG12210
# 3: 1000797366 2018-Q1 UG10000-WISD,UG12210
# 4: 1000797366 2018-Q1 UG10000-WISD,UG12210
# 5: 1000797366 2018-Q1 UG12210

How to filter/subset a data table based on condition on other columns in R?

Just group by Date and ID, count observations and filter when there are greater than one:

Data[, n:=.N, by = .(Date, ID)][n>1]
# Date ID Value n
# 1: 2020-01-04 1 189 3
# 2: 2020-01-04 1 654 3
# 3: 2020-01-04 1 333 3


Related Topics



Leave a reply



Submit