From 75234aa42e4b3c7786c294c91e18dcb4f1e4aa6c Mon Sep 17 00:00:00 2001 From: Paul Warren Date: Thu, 28 Feb 2019 23:48:49 +0000 Subject: [PATCH 1/4] List.addElement - add item from list to provided HTML. --- src/index.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/index.js b/src/index.js index 887019ff..ad2bf005 100644 --- a/src/index.js +++ b/src/index.js @@ -138,6 +138,14 @@ module.exports = function(id, options, values) { return added; }; + this.addElement = function(elm) { + var item = null; + notCreate = (self.items.length > self.page) ? true : false; + item = new Item(this.valueNames, elm, notCreate); + self.items.push(item); + self.update(); + }; + this.show = function(i, page) { this.i = i; this.page = page; From abfe64e629717dfb6077964181fd6d3cbc801fc3 Mon Sep 17 00:00:00 2001 From: Paul Warren Date: Thu, 28 Feb 2019 23:49:16 +0000 Subject: [PATCH 2/4] Make sort() with no args re-run previous sort. --- src/sort.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/sort.js b/src/sort.js index 8a59b0a3..ddf9cc2b 100644 --- a/src/sort.js +++ b/src/sort.js @@ -46,10 +46,22 @@ module.exports = function(list) { } }; + var sortFunction; + var sort = function() { list.trigger('sortStart'); var options = {}; + /* Re-run the existing sort, if there is one */ + if (arguments.length == 0) { + if (sortFunction !== undefined) { + list.items.sort(sortFunction); + list.update(); + } + list.trigger('sortComplete'); + return; + } + var target = arguments[0].currentTarget || arguments[0].srcElement || undefined; if (target) { @@ -70,8 +82,7 @@ module.exports = function(list) { // caseInsensitive // alphabet var customSortFunction = (options.sortFunction || list.sortFunction || null), - multi = ((options.order === 'desc') ? -1 : 1), - sortFunction; + multi = ((options.order === 'desc') ? -1 : 1); if (customSortFunction) { sortFunction = function(itemA, itemB) { From 84a9ea1e9655aa6da3c92b03f365fee7745a5040 Mon Sep 17 00:00:00 2001 From: Paul Warren Date: Thu, 28 Feb 2019 23:49:35 +0000 Subject: [PATCH 3/4] Add refresh() to reload current rows from HTML. This is different to reIndex() which will recreate the list from the current visible rows. reIndex() will remove rows that are not currently shown due to filtering or pagination. --- src/index.js | 6 ++++++ src/item.js | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/src/index.js b/src/index.js index ad2bf005..d90a2de9 100644 --- a/src/index.js +++ b/src/index.js @@ -102,6 +102,12 @@ module.exports = function(id, options, values) { self.parse(self.list); }; + this.refresh = function() { + for (var i = 0; i < self.items.length; i++ ) { + self.items[i].reload(); + } + }; + this.toJSON = function() { var json = []; for (var i = 0, il = self.items.length; i < il; i++) { diff --git a/src/item.js b/src/item.js index f5ab9a18..8c7a4118 100644 --- a/src/item.js +++ b/src/item.js @@ -21,6 +21,11 @@ module.exports = function(list) { } }; + this.reload = function() { + var values = list.templater.get(item, initValues); + item.values(values); + }; + this.values = function(newValues, notCreate) { if (newValues !== undefined) { for(var name in newValues) { From 67400063e1d1a91bfbae95e863a3521b8c2b8f00 Mon Sep 17 00:00:00 2001 From: Paul Warren Date: Fri, 1 Mar 2019 14:59:09 +0000 Subject: [PATCH 4/4] Documentation of addItem(), sort() and refresh() --- docs/api.html | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/docs/api.html b/docs/api.html index 7b876afb..ce8ca758 100644 --- a/docs/api.html +++ b/docs/api.html @@ -39,6 +39,7 @@

Properties

Methods

  • add
  • +
  • addItem
  • clear
  • filter
  • fuzzySearch
  • @@ -218,6 +219,10 @@

    Methods

    console.log('All ' + items.length + ' were added!'); }); +
  • +

    addItem(element)
    + Adds an item to the list based on the supplied DOM element.

    +
  • remove(valueName, value)
    Removes items from the list where the value named valueName has value value. @@ -253,6 +258,7 @@

    Methods

    https://github.com/nwoltman/string-natural-compare, if you want to use your own, read the code and check out the tests.

    +

    If called with no arguments, the previous sort will be re-applied. This is useful if the list has been modified.

    listObj.sort('name', { order: "asc" }); // Sorts the list in abc-order based on names
     listObj.sort('name', { order: "desc" }); // Sorts the list in zxy-order based on names
    @@ -265,6 +271,9 @@ 

    Methods

    // Alphabet could also be on the actual listObj via listObj.alphabet = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvXxYyZzÅåÄäÖö"; + +// Re-apply the last sort +listObj.sort();
  • @@ -335,10 +344,19 @@

    Methods

    hides some items with the itemObj.hide() method then you have to call listObj.update() if you want the paging to update.

    +
  • +

    refresh()
    + Reload all existing rows from current HTML. This is useful if the content + of rows has been modified and will enable search and sort to operate on the updated data. Unlike reIndex, rows will not be added or removed. To add new HTML elements to a list, use addItem. +

    +
  • reIndex()
    Re-index list from HTML. Good to use if the HTML has been changed by something - else than List.js.

    + else than List.js. Note that this will remove any removes that are not + currently visible due to pagination or filtering.

  • fuzzySearch()