Skip to content

Commit dfbe95a

Browse files
committed
Add ability to search grid with typing
1 parent 9553024 commit dfbe95a

File tree

1 file changed

+163
-77
lines changed

1 file changed

+163
-77
lines changed

js/dynamic-table.jquery.js

Lines changed: 163 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@
603603
//Create a table
604604
myHtml += "<table class=\"ui-dynamic-table-page\">";
605605

606-
//Calculate the range of rows of this page
606+
//Calculate the range of rows of this page
607607
var myStartRow = (i * myOptions.pageSize);
608608
var myEndRow = Math.min(myStartRow + myOptions.pageSize - 1, myData.data.length -1);
609609

@@ -689,6 +689,10 @@
689689

690690
//console.log("Rendering Page " + i + " Complete: " + ((new Date()).getTime() - myStartTime));
691691
}
692+
693+
setTimeout(function() {
694+
methods.private_highlightSelected(aComponent);
695+
})
692696
}
693697

694698

@@ -1614,29 +1618,17 @@
16141618
*================================================================================*/
16151619
private_selectRow: function (aComponent, aCell, aTableLocation) {
16161620

1617-
var myData = aComponent.data("dynamicTable");
1618-
1619-
//Mark the current row as selected
1620-
aComponent.find("tr.selected").removeClass("selected");
1621-
1622-
var myRow = aCell.closest("tr");
1623-
myRow.addClass ("selected");
1624-
1625-
//Trigger the row select event
1626-
aComponent.trigger({
1627-
type: "rowSelect",
1628-
row: myData.data[aTableLocation[0]]
1629-
});
1630-
1631-
myData.location = aTableLocation;
1632-
1633-
var myContainer = aComponent.children(".ui-dynamic-table-private-row-container");
1621+
var myData = aComponent.data("dynamicTable");
1622+
var myRowNumber = parseInt(aTableLocation);
16341623

1635-
var myRowBottom = (myContainer.scrollTop() + myRow.offset().top + myRow.height()) - myContainer.offset().top;
1636-
var myContainerBottom = (myContainer.scrollTop() + myContainer.height()) - myRow.height();
1637-
1638-
var myRowTop = (myContainer.scrollTop() + myRow.offset().top) - myContainer.offset().top;
1639-
var myContainerTop = myContainer.scrollTop();
1624+
myData.location = aTableLocation;
1625+
1626+
var myRowTop = myRowNumber * myData.options.rowHeight;
1627+
var myRowBottom = myRowTop + myData.options.rowHeight;
1628+
1629+
var myContainer = aComponent.children(".ui-dynamic-table-private-row-container");
1630+
var myContainerBottom = (myContainer.scrollTop() + myContainer.height()) - myData.options.rowHeight;
1631+
var myContainerTop = myContainer.scrollTop();
16401632

16411633
if (myRowBottom > myContainerBottom)
16421634
{
@@ -1648,8 +1640,37 @@
16481640
{
16491641
var myDifference = myContainerTop - myRowTop;
16501642

1651-
myContainer.scrollTop(myContainer.scrollTop() - myDifference);
1643+
myContainer.scrollTop(myContainer.scrollTop() - myDifference);
16521644
}
1645+
1646+
methods.private_highlightSelected(aComponent)
1647+
},
1648+
1649+
/**=================================================================================
1650+
* Called to do the actual highlighting of selected elements.
1651+
*================================================================================*/
1652+
private_highlightSelected : function(aComponent) {
1653+
1654+
var myData = aComponent.data("dynamicTable");
1655+
var myLocation = myData.location;
1656+
1657+
if (!myLocation) {
1658+
return;
1659+
}
1660+
1661+
var myCell = aComponent.find("#ui-dynamic-table-page-cell-" + myLocation[0] + "-" + myLocation[1]);
1662+
1663+
//Mark the current row as selected
1664+
aComponent.find("tr.selected").removeClass("selected");
1665+
1666+
var myRow = myCell.closest("tr");
1667+
myRow.addClass ("selected");
1668+
1669+
//Trigger the row select event
1670+
aComponent.trigger({
1671+
type: "rowSelect",
1672+
row: myData.data[myLocation[0]]
1673+
});
16531674
},
16541675

16551676
/**=================================================================================
@@ -1659,14 +1680,8 @@
16591680

16601681
var myKey = aEvent.which;
16611682

1662-
if (myKey != $.ui.keyCode.UP &&
1663-
myKey != $.ui.keyCode.DOWN)
1664-
{
1665-
return;
1666-
}
1667-
16681683
var myComponent = aEvent.data.component;
1669-
1684+
16701685
// If this grid does not have the focus ignore the events
16711686
if (!myComponent.is(":focus") && myComponent.find(":focus").length == 0) {
16721687
return;
@@ -1681,48 +1696,88 @@
16811696

16821697
var myLocation = myData.location;
16831698
var myLocationChanged = false;
1684-
1699+
16851700
if (!myLocation)
16861701
{
16871702
//TODO the "1" as a cell is not logical, but gives trouble if set to 0
16881703
myLocation = [0, 1];
16891704
}
16901705

1691-
if (myKey == $.ui.keyCode.UP)
1692-
{
1693-
myLocation[0] = parseInt(myLocation[0]) - 1;
1694-
1695-
if (myLocation[0] < 0)
1696-
{
1697-
myLocation[0] = 0;
1706+
// Capture basic up and down movement
1707+
if (myKey == $.ui.keyCode.UP ||
1708+
myKey == $.ui.keyCode.DOWN) {
1709+
if (myKey == $.ui.keyCode.UP) {
1710+
myLocation[0] = parseInt(myLocation[0]) - 1;
1711+
1712+
if (myLocation[0] < 0)
1713+
{
1714+
myLocation[0] = 0;
1715+
}
1716+
1717+
myLocationChanged = true;
1718+
}
1719+
else if (myKey == $.ui.keyCode.DOWN) {
1720+
myLocation[0] = parseInt(myLocation[0]) + 1;
1721+
1722+
if (myLocation[0] >= myData.data.length)
1723+
{
1724+
myLocation[0] = myData.data.length - 1;
1725+
}
1726+
1727+
myLocationChanged = true;
16981728
}
1699-
1700-
myLocationChanged = true;
17011729
}
1702-
else if (myKey == $.ui.keyCode.DOWN)
1703-
{
1704-
myLocation[0] = parseInt(myLocation[0]) + 1;
1705-
1706-
if (myLocation[0] >= myData.data.length)
1707-
{
1708-
myLocation[0] = myData.data.length - 1;
1730+
// Search by content and select first matching
1731+
else {
1732+
var myInput = String.fromCharCode(myKey);
1733+
1734+
if (myInput) {
1735+
if (!myData.inputBuffer) {
1736+
myData.inputBuffer = new $.fn.dynamicTable.inputBuffer(1000);
1737+
}
1738+
1739+
myData.inputBuffer.append(myInput);
1740+
1741+
var myBuffer = myData.inputBuffer.getBuffer().toUpperCase();
1742+
1743+
var myCurrentIndex = -1;
1744+
var myBestIndex = 9999999999999;
1745+
1746+
for (var i = 0; i < myData.data.length; i++) {
1747+
var myIndex = (i + parseInt(myLocation[0])) % myData.data.length;
1748+
var myRow = myData.data[myIndex];
1749+
var myTestString = null;
1750+
1751+
if (Array.isArray(myRow)) {
1752+
myTestString = myRow.join(",").toUpperCase();
1753+
}
1754+
else {
1755+
myTestString = JSON.stringify(myData.data[i]).toUpperCase();
1756+
}
1757+
1758+
var myTempIndex = myTestString.indexOf(myBuffer);
1759+
1760+
if (myTempIndex >= 0 && myTempIndex < myBestIndex) {
1761+
myCurrentIndex = myIndex;
1762+
myBestIndex = myTempIndex;
1763+
}
1764+
}
1765+
1766+
if (myCurrentIndex >= 0) {
1767+
myLocation[0] = myCurrentIndex;
1768+
myLocationChanged = true;
1769+
}
1770+
17091771
}
1710-
1711-
myLocationChanged = true;
17121772
}
1713-
1714-
if (myLocationChanged)
1715-
{
1716-
//console.log(myLocation);
1717-
//console.log($("#ui-dynamic-table-page-cell-" + myLocation[0] + "-" + myLocation[1]));
1718-
1773+
1774+
if (myLocationChanged) {
17191775
methods.private_selectRow(
17201776
myComponent,
17211777
$("#ui-dynamic-table-page-cell-" + myLocation[0] + "-" + myLocation[1]),
17221778
myLocation
17231779
);
17241780
}
1725-
17261781
},
17271782

17281783
/**=================================================================================
@@ -1945,30 +2000,33 @@
19452000
* Removes all filters from the dynamic table
19462001
*================================================================================*/
19472002
clearAllFilters : function() {
1948-
var myContainer = $(this);
1949-
var myData = myContainer.data("dynamicTable");
2003+
return this.each(function() {
2004+
2005+
var myContainer = $(this);
2006+
var myData = myContainer.data("dynamicTable");
19502007

1951-
myData.activeFilters = [];
2008+
myData.activeFilters = [];
19522009

1953-
//Apply the filter
1954-
methods.private_applyFilter (myData);
1955-
1956-
//Applies the current sort again, if there is one
1957-
if (myData.currentSort)
1958-
{
1959-
methods.private_sortBy (myContainer, myData.currentSort, myData, true);
1960-
}
2010+
//Apply the filter
2011+
methods.private_applyFilter (myData);
2012+
2013+
//Applies the current sort again, if there is one
2014+
if (myData.currentSort)
2015+
{
2016+
methods.private_sortBy (myContainer, myData.currentSort, myData, true);
2017+
}
19612018

1962-
//Render placeholders
1963-
methods.private_renderPlaceHolders (myContainer, myData.options.rowHeight, myData.options.pageSize, myData.data.length);
1964-
1965-
//Render the visible parts of the table
1966-
methods.private_renderVisible (myContainer);
2019+
//Render placeholders
2020+
methods.private_renderPlaceHolders (myContainer, myData.options.rowHeight, myData.options.pageSize, myData.data.length);
2021+
2022+
//Render the visible parts of the table
2023+
methods.private_renderVisible (myContainer);
19672024

1968-
//Remove all filtered highlights
1969-
myContainer
1970-
.find(".ui-dynamic-table-filtered")
1971-
.removeClass("ui-dynamic-table-filtered");
2025+
//Remove all filtered highlights
2026+
myContainer
2027+
.find(".ui-dynamic-table-filtered")
2028+
.removeClass("ui-dynamic-table-filtered");
2029+
})
19722030
},
19732031

19742032
/**=================================================================================
@@ -2269,4 +2327,32 @@
22692327
return new CallbackSettingsHandler(aSaveColumn, aUpdateColumn);
22702328
}
22712329

2330+
/**====================================================================================
2331+
* This class/function allows the buffering of an input string until a certain time
2332+
* of inactivity is reached. At that point the buffer gets cleared.
2333+
*
2334+
* @param {Number} aTimeout
2335+
* The timeout in milliseconds after which the buffer clears
2336+
*===================================================================================*/
2337+
$.fn.dynamicTable.inputBuffer = function(aTimeout) {
2338+
2339+
var buffer = "";
2340+
var timeout = null;
2341+
2342+
this.append = function(aString) {
2343+
if (timeout) {
2344+
clearTimeout(timeout)
2345+
}
2346+
2347+
buffer += aString;
2348+
timeout = setTimeout(function() {
2349+
buffer = "";
2350+
}, aTimeout);
2351+
}
2352+
2353+
this.getBuffer = function() {
2354+
return buffer;
2355+
}
2356+
}
2357+
22722358
})(jQuery);

0 commit comments

Comments
 (0)