MediaWiki:Common.js: Difference between revisions

From Emmy The Robot Fandom Wiki
Content added Content deleted
No edit summary
No edit summary
Line 13: Line 13:
searchInput.setAttribute('type', 'text');
searchInput.setAttribute('type', 'text');
searchInput.setAttribute('id', 'mediawiki-text-parser-input');
searchInput.setAttribute('id', 'mediawiki-text-parser-input');
searchInput.setAttribute('placeholder', 'Type to search');
searchInput.setAttribute('placeholder', 'Type to search1');
document.getElementById('mw-content-text').insertBefore(searchInput, document.getElementById('mw-content-text').firstChild);
document.getElementById('mw-content-text').insertBefore(searchInput, document.getElementById('mw-content-text').firstChild);

// Get the table
var table = document.querySelector('table.wikitable.sortable.mw-datatable.jquery-tablesorter');


// Attach an event listener to the search input field
// Attach an event listener to the search input field
Line 20: Line 23:
var searchText = this.value.toLowerCase();
var searchText = this.value.toLowerCase();


// Get all paragraphs in the content area
// Get all rows in the table
var rows = document.querySelectorAll('table.wikitable.sortable.mw-datatable.jquery-tablesorter tr:not(:first-child)');
var rows = table.querySelectorAll('tr');


// Loop through each row
// Loop through each row
rows.forEach(function(row) {
rows.forEach(function(row, index) {
var rowCells = row.cells;
if (index === 0) {
// Show the first row (headers) always
var rowShouldBeVisible = false;

// Loop through each cell in the row
Array.from(rowCells).forEach(function(cell) {
var cellText = cell.textContent.toLowerCase();
// Check if any cell contains the search text
if (cellText.includes(searchText)) {
rowShouldBeVisible = true;
}
});

// Set the display property of the row and its cells
if (rowShouldBeVisible) {
row.style.display = 'table-row';
row.style.display = 'table-row';
} else {
var rowCells = row.cells;
var rowShouldBeVisible = false;

// Loop through each cell in the row
Array.from(rowCells).forEach(function(cell) {
Array.from(rowCells).forEach(function(cell) {
cell.style.display = 'table-cell';
var cellText = cell.textContent.toLowerCase();
// Check if any cell contains the search text
if (cellText.includes(searchText)) {
rowShouldBeVisible = true;
}
});
});

} else {
row.style.display = 'none';
// Set the display property of the row and its cells
if (rowShouldBeVisible) {
row.style.display = 'table-row';
Array.from(rowCells).forEach(function(cell) {
cell.style.display = 'table-cell';
});
} else {
row.style.display = 'none';
}
}
}
});
});