Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Import-Module PSWriteHTML -Force

$Process = Get-Process | Select-Object -First 35

$Name = 'PSWriteHTML Regular Expression Search and Wildcard Conditional Formatting'
$Author = $env:USERNAME

$SaveFilePath = $PSCommandPath -replace "ps1$", "html"


New-HTML -Name $Name -Author $Author -FilePath $SaveFilePath -AutoRefresh 0 -ShowHTML -UseCssLinks:$false -UseJavaScriptLinks:$false -HTMLData {
$Buttons = @('copyHtml5', 'excelHtml5', 'csvHtml5', 'pdfHtml5', 'pageLength')
$PreContent = { New-HTMLText -Text 'Smart Search Enabled for Column Searches' -Color DodgerBlue -Alignment center -FontSize 20 }

New-HTMLTable -DataTable $Process -DefaultSortIndex 4 -ScrollCollapse -Buttons $Buttons -Filtering -FilteringLocation Bottom -FixedFooter -PreContent $PreContent -HTML {
# If item matches multiple rules, last matched rule is applied
New-HTMLTableCondition -Name Name -ComparisonType string -Operator like -Value "Co*" -BackgroundColor Crimson -Color White
New-HTMLTableCondition -Name Name -ComparisonType string -Operator like -Value "*me" -BackgroundColor Gold -Color Black
New-HTMLTableCondition -Name Name -ComparisonType string -Operator like -Value "C*de" -BackgroundColor Green -Color White
}
New-HTMLHorizontalLine
New-HTMLText -Text "RegEx Example - Search for all processes but 'Code': ^((?!Code).)*$"
New-HTMLText -Text "RegEx Example - Search for all processes but 'Code' OR 'Chrome': ^((?!Code|Chrome).)*$"
New-HTMLText -Text "RegEx Example - Search for all 'Code' OR 'Chrome' processes: Code|Chrome"
$PreContent = { New-HTMLText -Text 'Regular Expression Search Enabled for Column Searches' -Color DodgerBlue -Alignment center -FontSize 20 }
New-HTMLTable -DataTable $Process -DefaultSortIndex 4 -ScrollCollapse -Buttons $Buttons -Filtering -FilteringLocation Bottom -FixedFooter -PreContent $PreContent -SearchRegularExpression -HTML {
New-HTMLTableCondition -Name Path -ComparisonType string -Operator like -Value "*cri*" -BackgroundColor Crimson -Color White
New-HTMLTableCondition -Name Path -ComparisonType string -Operator like -Value "*me" -BackgroundColor Gold -Color Black
New-HTMLTableCondition -Name Path -ComparisonType string -Operator like -Value "C*de" -BackgroundColor Green -Color White
}
}
18 changes: 14 additions & 4 deletions Private/Tables/Add-TableFiltering.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@ function Add-TableFiltering {
param(
[bool] $Filtering,
[ValidateSet('Top', 'Bottom', 'Both')][string]$FilteringLocation = 'Bottom',
[string] $DataTableName
[string] $DataTableName,
[alias('RegularExpression')][switch]$SearchRegularExpression
)

if ($SearchRegularExpression.IsPresent) {
[string]$JSDataTableRegEx = 'true'
[string]$JSDataTableSmart = 'false'
} else {
[string]$JSDataTableRegEx = 'false'
[string]$JSDataTableSmart = 'true'
}

$Output = @{}
if ($Filtering) {
# https://datatables.net/examples/api/multi_filter.html
Expand All @@ -25,7 +35,7 @@ function Add-TableFiltering {

`$('input', this.footer()).on('keyup change', function () {
if (that.search() !== this.value) {
that.search(this.value).draw();
that.search(this.value, $JSDataTableRegEx, $JSDataTableSmart).draw();
}
});
});
Expand Down Expand Up @@ -65,7 +75,7 @@ function Add-TableFiltering {

`$('input', this.footer()).on('keyup change', function () {
if (that.search() !== this.value) {
that.search(this.value).draw();
that.search(this.value, $JSDataTableRegEx, $JSDataTableSmart).draw();
}
});
});
Expand Down Expand Up @@ -101,4 +111,4 @@ function Add-TableFiltering {
$Output.FilteringTopCode = $Output.FilteringBottomCode = '' # assign multiple same values trick
}
return $Output
}
}
19 changes: 8 additions & 11 deletions Private/Tables/New-TableConditionalFormatting.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,13 @@ function New-TableConditionalFormatting {
if ($null -eq $Condition.Type -or $Condition.Type -eq 'number' -or $Condition.Type -eq 'int' -or $Condition.Type -eq 'decimal') {
"if (data[$ConditionHeaderNr] $($Condition.Operator) $($Condition.Value)) {"
} elseif ($Condition.Type -eq 'string') {
switch ($Condition.Operator) {
"contains" {
"if (data[$($ConditionHeaderNr)].includes('$($Condition.Value)')) {"
}
"like" {
"if (data[$($ConditionHeaderNr)].includes('$($Condition.Value)')) {"
}
default {
"if (data[$ConditionHeaderNr] $($Condition.Operator) '$($Condition.Value)') {"
}
switch -Regex ($Condition.Operator) {
"contains|like" {
"if (/$($Condition.Value.Replace('*','.*'))/i.test(data[$($ConditionHeaderNr)])) {"
}
default {
"if (data[$ConditionHeaderNr] $($Condition.Operator) '$($Condition.Value)') {"
}
}
} elseif ($Condition.Type -eq 'date') {
"if (new Date(data[$ConditionHeaderNr]) $($Condition.Operator) new Date('$($Condition.Value)')) {"
Expand All @@ -66,4 +63,4 @@ function New-TableConditionalFormatting {
$Options = $Options -Replace ($TextToFind, $Condition)
}
return $Options
}
}
5 changes: 3 additions & 2 deletions Public/New-HTMLTable.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ function New-HTMLTable {
[alias('CompareWithColors')][switch] $HighlightDifferences,
[int] $First,
[int] $Last,
[alias('Replace')][Array] $CompareReplace
[alias('Replace')][Array] $CompareReplace,
[alias('RegularExpression')][switch]$SearchRegularExpression
)
if (-not $Script:HTMLSchema.Features) {
Write-Warning 'New-HTMLTable - Creation of HTML aborted. Most likely New-HTML is missing.'
Expand Down Expand Up @@ -478,7 +479,7 @@ function New-HTMLTable {

# Enable Custom Date fromat sorting
$SortingFormatDateTime = Add-CustomFormatForDatetimeSorting -DateTimeSortingFormat $DateTimeSortingFormat
$FilteringOutput = Add-TableFiltering -Filtering $Filtering -FilteringLocation $FilteringLocation -DataTableName $DataTableID
$FilteringOutput = Add-TableFiltering -Filtering $Filtering -FilteringLocation $FilteringLocation -DataTableName $DataTableID -SearchRegularExpression:$SearchRegularExpression
$FilteringTopCode = $FilteringOutput.FilteringTopCode
$FilteringBottomCode = $FilteringOutput.FilteringBottomCode
$LoadSavedState = Add-TableState -DataTableName $DataTableID -Filtering $Filtering -FilteringLocation $FilteringLocation -SavedState (-not $DisableStateSave)
Expand Down