Skip to content

Commit 2611236

Browse files
committed
Added "public getter" feature and fixed a small bug regarding totalPages
1 parent c39b926 commit 2611236

9 files changed

+350
-58
lines changed

CHANGELOG.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
- Clear search filter method [#89](http://github.com/rstaib/jquery-bootgrid/issues/89)
1010
- Overridable ajax request settings; see issue [#27](http://github.com/rstaib/jquery-bootgrid/issues/27) for more details
1111
- Adjusting search input field to perform search not on every key #109 -> todo
12-
- Public getter for internal variables -> todo
12+
- Public getters for internal variables; see issues [#121](http://github.com/rstaib/jquery-bootgrid/issues/121) and [#116](http://github.com/rstaib/jquery-bootgrid/issues/116) for more details
1313

1414
### Bug Fixes
1515
- Fixed bug [#120](http://github.com/rstaib/jquery-bootgrid/issues/120)
16-
- Fixed bug [#58](http://github.com/rstaib/jquery-bootgrid/issues/58) -> use getText when resolving actionDropDown #65
17-
- Fixing sort method for jQuery plugin #84 (pull request) -> todo
16+
- Fixed bug [#58](http://github.com/rstaib/jquery-bootgrid/issues/58) by using the pull request [#65](http://github.com/rstaib/jquery-bootgrid/issues/65)
17+
- Fixing sort method for jQuery plugin [#84](http://github.com/rstaib/jquery-bootgrid/issues/84)
1818

1919
### Breaking Changes
2020
There are no breaking changes but some HTML templates changed during development. In case you want to use the full new feature set be sure you did not override any affected templates.

demo/index.htm

+36
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454
<button id="init" type="button" class="btn btn-default">Init</button>
5555
<button id="clearSearch" type="button" class="btn btn-default">Clear Search</button>
5656
<button id="clearSort" type="button" class="btn btn-default">Clear Sort</button>
57+
<button id="getCurrentPage" type="button" class="btn btn-default">Current Page Index</button>
58+
<button id="getRowCount" type="button" class="btn btn-default">Row Count</button>
59+
<button id="getTotalRowCount" type="button" class="btn btn-default">Total Row Count</button>
60+
<button id="getTotalPageCount" type="button" class="btn btn-default">Total Page Count</button>
61+
<button id="getSearchPhrase" type="button" class="btn btn-default">Search Phrase</button>
62+
<button id="getSortDictionary" type="button" class="btn btn-default">Sort Dictionary</button>
5763
<!--div class="table-responsive"-->
5864
<table id="grid" class="table table-condensed table-hover table-striped" data-selection="true" data-multi-select="true" data-row-select="true" data-keep-selection="true">
5965
<thead>
@@ -217,6 +223,36 @@
217223
{
218224
$("#grid").bootgrid("sort");
219225
});
226+
227+
$("#getCurrentPage").on("click", function ()
228+
{
229+
alert($("#grid").bootgrid("getCurrentPage"));
230+
});
231+
232+
$("#getRowCount").on("click", function ()
233+
{
234+
alert($("#grid").bootgrid("getRowCount"));
235+
});
236+
237+
$("#getTotalPageCount").on("click", function ()
238+
{
239+
alert($("#grid").bootgrid("getTotalPageCount"));
240+
});
241+
242+
$("#getTotalRowCount").on("click", function ()
243+
{
244+
alert($("#grid").bootgrid("getTotalRowCount"));
245+
});
246+
247+
$("#getSearchPhrase").on("click", function ()
248+
{
249+
alert($("#grid").bootgrid("getSearchPhrase"));
250+
});
251+
252+
$("#getSortDictionary").on("click", function ()
253+
{
254+
alert($("#grid").bootgrid("getSortDictionary"));
255+
});
220256
});
221257
</script>
222258
</body>

dist/jQuery.Bootgrid.1.2.0.nupkg

680 Bytes
Binary file not shown.

dist/jquery.bootgrid-1.2.0.zip

862 Bytes
Binary file not shown.

dist/jquery.bootgrid.js

+155-27
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@
189189
function update(rows, total)
190190
{
191191
that.currentRows = rows;
192-
that.total = total;
193-
that.totalPages = Math.ceil(total / that.rowCount);
192+
setTotals.call(that, total);
194193

195194
if (!that.options.keepSelection)
196195
{
@@ -288,14 +287,18 @@
288287
appendRow.call(that, row);
289288
});
290289

291-
this.total = this.rows.length;
292-
this.totalPages = (this.rowCount === -1) ? 1 :
293-
Math.ceil(this.total / this.rowCount);
294-
290+
setTotals.call(this, this.rows.length);
295291
sortRows.call(this);
296292
}
297293
}
298294

295+
function setTotals(total)
296+
{
297+
this.total = total;
298+
this.totalPages = (this.rowCount === -1) ? 1 :
299+
Math.ceil(this.total / this.rowCount);
300+
}
301+
299302
function prepareTable()
300303
{
301304
var tpl = this.options.templates,
@@ -1614,7 +1617,6 @@
16141617
return this;
16151618
};
16161619

1617-
16181620
/**
16191621
* Sorts the rows by a given sort descriptor dictionary.
16201622
* The sort filter will be reseted, if no argument is provided.
@@ -1640,6 +1642,123 @@
16401642
return this;
16411643
};
16421644

1645+
/**
1646+
* Gets a list of the column settings.
1647+
* This method returns only for the first grid instance a value.
1648+
* Therefore be sure that only one grid instance is catched by your selector.
1649+
*
1650+
* @method getColumnSettings
1651+
* @return {Array} Returns a list of the column settings.
1652+
**/
1653+
Grid.prototype.getColumnSettings = function()
1654+
{
1655+
return $.merge([], this.columns);
1656+
};
1657+
1658+
/**
1659+
* Gets the current page index.
1660+
* This method returns only for the first grid instance a value.
1661+
* Therefore be sure that only one grid instance is catched by your selector.
1662+
*
1663+
* @method getCurrentPage
1664+
* @return {Number} Returns the current page index.
1665+
**/
1666+
Grid.prototype.getCurrentPage = function()
1667+
{
1668+
return this.current;
1669+
};
1670+
1671+
/**
1672+
* Gets the current rows.
1673+
* This method returns only for the first grid instance a value.
1674+
* Therefore be sure that only one grid instance is catched by your selector.
1675+
*
1676+
* @method getCurrentPage
1677+
* @return {Array} Returns the current rows.
1678+
**/
1679+
Grid.prototype.getCurrentRows = function()
1680+
{
1681+
return $.merge([], this.currentRows);
1682+
};
1683+
1684+
/**
1685+
* Gets a number represents the row count per page.
1686+
* This method returns only for the first grid instance a value.
1687+
* Therefore be sure that only one grid instance is catched by your selector.
1688+
*
1689+
* @method getRowCount
1690+
* @return {Number} Returns the row count per page.
1691+
**/
1692+
Grid.prototype.getRowCount = function()
1693+
{
1694+
return this.rowCount;
1695+
};
1696+
1697+
/**
1698+
* Gets the actual search phrase.
1699+
* This method returns only for the first grid instance a value.
1700+
* Therefore be sure that only one grid instance is catched by your selector.
1701+
*
1702+
* @method getSearchPhrase
1703+
* @return {String} Returns the actual search phrase.
1704+
**/
1705+
Grid.prototype.getSearchPhrase = function()
1706+
{
1707+
return this.searchPhrase;
1708+
};
1709+
1710+
/**
1711+
* Gets the complete list of currently selected rows.
1712+
* This method returns only for the first grid instance a value.
1713+
* Therefore be sure that only one grid instance is catched by your selector.
1714+
*
1715+
* @method getSelectedRows
1716+
* @return {Array} Returns all selected rows.
1717+
**/
1718+
Grid.prototype.getSelectedRows = function()
1719+
{
1720+
return $.merge([], this.selectedRows);
1721+
};
1722+
1723+
/**
1724+
* Gets the sort dictionary which represents the state of column sorting.
1725+
* This method returns only for the first grid instance a value.
1726+
* Therefore be sure that only one grid instance is catched by your selector.
1727+
*
1728+
* @method getSortDictionary
1729+
* @return {Object} Returns the sort dictionary.
1730+
**/
1731+
Grid.prototype.getSortDictionary = function()
1732+
{
1733+
return $.extend({}, this.sortDictionary);
1734+
};
1735+
1736+
/**
1737+
* Gets a number represents the total page count.
1738+
* This method returns only for the first grid instance a value.
1739+
* Therefore be sure that only one grid instance is catched by your selector.
1740+
*
1741+
* @method getTotalPageCount
1742+
* @return {Number} Returns the total page count.
1743+
**/
1744+
Grid.prototype.getTotalPageCount = function()
1745+
{
1746+
return this.totalPages;
1747+
};
1748+
1749+
/**
1750+
* Gets a number represents the total row count.
1751+
* This method returns only for the first grid instance a value.
1752+
* Therefore be sure that only one grid instance is catched by your selector.
1753+
*
1754+
* @method getTotalRowCount
1755+
* @return {Number} Returns the total row count.
1756+
**/
1757+
Grid.prototype.getTotalRowCount = function()
1758+
{
1759+
return this.total;
1760+
};
1761+
16431762
// GRID COMMON TYPE EXTENSIONS
16441763
// ============
16451764

@@ -1818,27 +1937,36 @@
18181937

18191938
$.fn.bootgrid = function (option)
18201939
{
1821-
var args = Array.prototype.slice.call(arguments, 1);
1822-
return this.each(function ()
1823-
{
1824-
var $this = $(this),
1825-
instance = $this.data(namespace),
1826-
options = typeof option === "object" && option;
1827-
1828-
if (!instance && option === "destroy")
1829-
{
1830-
return;
1831-
}
1832-
if (!instance)
1833-
{
1834-
$this.data(namespace, (instance = new Grid(this, options)));
1835-
init.call(instance);
1836-
}
1837-
if (typeof option === "string")
1940+
var args = Array.prototype.slice.call(arguments, 1),
1941+
returnValue = null,
1942+
elements = this.each(function (index)
18381943
{
1839-
return instance[option].apply(instance, args);
1840-
}
1841-
});
1944+
var $this = $(this),
1945+
instance = $this.data(namespace),
1946+
options = typeof option === "object" && option;
1947+
1948+
if (!instance && option === "destroy")
1949+
{
1950+
return;
1951+
}
1952+
if (!instance)
1953+
{
1954+
$this.data(namespace, (instance = new Grid(this, options)));
1955+
init.call(instance);
1956+
}
1957+
if (typeof option === "string")
1958+
{
1959+
if (option.indexOf("get") === 0 && index === 0)
1960+
{
1961+
returnValue = instance[option].apply(instance, args);
1962+
}
1963+
else if (option.indexOf("get") !== 0)
1964+
{
1965+
return instance[option].apply(instance, args);
1966+
}
1967+
}
1968+
});
1969+
return (typeof option === "string" && option.indexOf("get") === 0) ? returnValue : elements;
18421970
};
18431971

18441972
$.fn.bootgrid.Constructor = Grid;

dist/jquery.bootgrid.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/internal.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ function loadData()
179179
function update(rows, total)
180180
{
181181
that.currentRows = rows;
182-
that.total = total;
183-
that.totalPages = Math.ceil(total / that.rowCount);
182+
setTotals.call(that, total);
184183

185184
if (!that.options.keepSelection)
186185
{
@@ -278,14 +277,18 @@ function loadRows()
278277
appendRow.call(that, row);
279278
});
280279

281-
this.total = this.rows.length;
282-
this.totalPages = (this.rowCount === -1) ? 1 :
283-
Math.ceil(this.total / this.rowCount);
284-
280+
setTotals.call(this, this.rows.length);
285281
sortRows.call(this);
286282
}
287283
}
288284

285+
function setTotals(total)
286+
{
287+
this.total = total;
288+
this.totalPages = (this.rowCount === -1) ? 1 :
289+
Math.ceil(this.total / this.rowCount);
290+
}
291+
289292
function prepareTable()
290293
{
291294
var tpl = this.options.templates,

src/plugin.js

+29-20
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,36 @@ var old = $.fn.bootgrid;
55

66
$.fn.bootgrid = function (option)
77
{
8-
var args = Array.prototype.slice.call(arguments, 1);
9-
return this.each(function ()
10-
{
11-
var $this = $(this),
12-
instance = $this.data(namespace),
13-
options = typeof option === "object" && option;
14-
15-
if (!instance && option === "destroy")
8+
var args = Array.prototype.slice.call(arguments, 1),
9+
returnValue = null,
10+
elements = this.each(function (index)
1611
{
17-
return;
18-
}
19-
if (!instance)
20-
{
21-
$this.data(namespace, (instance = new Grid(this, options)));
22-
init.call(instance);
23-
}
24-
if (typeof option === "string")
25-
{
26-
return instance[option].apply(instance, args);
27-
}
28-
});
12+
var $this = $(this),
13+
instance = $this.data(namespace),
14+
options = typeof option === "object" && option;
15+
16+
if (!instance && option === "destroy")
17+
{
18+
return;
19+
}
20+
if (!instance)
21+
{
22+
$this.data(namespace, (instance = new Grid(this, options)));
23+
init.call(instance);
24+
}
25+
if (typeof option === "string")
26+
{
27+
if (option.indexOf("get") === 0 && index === 0)
28+
{
29+
returnValue = instance[option].apply(instance, args);
30+
}
31+
else if (option.indexOf("get") !== 0)
32+
{
33+
return instance[option].apply(instance, args);
34+
}
35+
}
36+
});
37+
return (typeof option === "string" && option.indexOf("get") === 0) ? returnValue : elements;
2938
};
3039

3140
$.fn.bootgrid.Constructor = Grid;

0 commit comments

Comments
 (0)