Commit 6c0920fb authored by Tejesh's avatar Tejesh 🖖
Browse files

FIX: datafilter doesnt filter undefined values, Retain order of columns when ...

FIX: datafilter doesnt filter undefined values, Retain order of columns when  is used in formhandler, fixes #162, #89
parent 9ba67cc3
Pipeline #83652 passed with stage
in 2 minutes and 26 seconds
...@@ -85,7 +85,6 @@ export function datafilter(data, filters, dataset_name) { ...@@ -85,7 +85,6 @@ export function datafilter(data, filters, dataset_name) {
filters = namespace(filters, dataset_name) filters = namespace(filters, dataset_name)
var data_types = types(data, { convert: true }) var data_types = types(data, { convert: true })
// apply WHERE clause // apply WHERE clause
for (var key in filters) { for (var key in filters) {
if (key[0] == '_') continue if (key[0] == '_') continue
...@@ -103,7 +102,7 @@ export function datafilter(data, filters, dataset_name) { ...@@ -103,7 +102,7 @@ export function datafilter(data, filters, dataset_name) {
} }
result = result.filter(function (row) { result = result.filter(function (row) {
return (typeof row[col] != 'undefined') ? operators[operator](value, row[col]) : true return (typeof row[col] != 'undefined') ? operators[operator](value, row[col]) : false
}) })
} }
......
...@@ -95,23 +95,23 @@ export function formhandler(js_options) { ...@@ -95,23 +95,23 @@ export function formhandler(js_options) {
meta.columns = data.length ? _.map(data[0], function (val, col) { return { name: col } }) : [] meta.columns = data.length ? _.map(data[0], function (val, col) { return { name: col } }) : []
// If any column name is '*', show all columns // If any column name is '*', show all columns
var star_col = _.find(options.columns, function (o) { return o['name'] === '*' }) var star_col_index = _.findIndex(options.columns, function (o) { return o['name'] === '*' })
if (star_col) { if (star_col_index >= 0) {
var action_header_cols = _.cloneDeep(meta.columns) var resolved_star_cols = options.columns.map(function (each_col_options) {
_.map(options.columns, function (option_col) { if (each_col_options.name !== '*') return each_col_options
var found = _.find(meta.columns, function (o) { return o['name'] === option_col.name }) return meta.columns
if (!found && option_col.name !== '*') .filter(function (col_opts) { return _.findIndex(options.columns, function (o) { return o['name'] === col_opts.name }) < 0 })
action_header_cols.push(option_col) .map(function (col_opts) {
}) return Object.assign({}, options.columns[star_col_index], col_opts)
})
}).reduce(function (acc, curr) {
if (!Array.isArray(curr)) return acc.concat([curr])
return acc.concat(curr)
}, [])
action_header_cols = _.map(action_header_cols, function (col) { options.columns = _.cloneDeep(resolved_star_cols)
var options_col = _.find(options.columns, function (o) { return o['name'] === col.name })
return options_col ? options_col : $.extend({}, star_col, col)
})
} }
options.columns = action_header_cols ? action_header_cols : options.columns
// Render all components into respective targets // Render all components into respective targets
var template_data = { var template_data = {
data: data, data: data,
...@@ -141,12 +141,15 @@ export function formhandler(js_options) { ...@@ -141,12 +141,15 @@ export function formhandler(js_options) {
var url_args = parse(location.hash.replace(/^#/, '')).searchList var url_args = parse(location.hash.replace(/^#/, '')).searchList
url_args = namespace(url_args, options.name) url_args = namespace(url_args, options.name)
// Create arguments passed to the FormHandler. Override with the user URL args // Create arguments passed to the FormHandler. Override with the user URL args
var args = _.extend({ var default_url_args = {
c: options.columns.map(function (d) { return d.name }),
_limit: options.pageSize, _limit: options.pageSize,
_format: 'json', _format: 'json',
_meta: 'y' _meta: 'y'
}, url_args) }
var star_col = _.find(options.columns, function (o) { return o['name'] === '*' })
if (!star_col) default_url_args['_c'] = options.columns.map(function (d) { return d.name })
var args = _.extend(default_url_args, url_args)
$('.loader', $this).removeClass('d-none') $('.loader', $this).removeClass('d-none')
function done(data, status, xhr) { function done(data, status, xhr) {
......
...@@ -14,7 +14,7 @@ const app = express() ...@@ -14,7 +14,7 @@ const app = express()
app.route('/formhandler-data').get(function(req, res) { app.route('/formhandler-data').get(function(req, res) {
var url = req.protocol + '://' + req.get('host') + req.originalUrl var url = req.protocol + '://' + req.get('host') + req.originalUrl
res.send(g1.datafilter(data, g1.url.parse(url).searchList, 'fhname13')) res.send(g1.datafilter(data, g1.url.parse(url).searchList))
}) })
app.route('/data-list').get(function (req, res) { app.route('/data-list').get(function (req, res) {
......
...@@ -9,9 +9,9 @@ const product_sales = [ ...@@ -9,9 +9,9 @@ const product_sales = [
{"ID": 1, "product": "Fan", "sales": true, "date": Date.parse("06-10-2018"), "city": "NY"}, {"ID": 1, "product": "Fan", "sales": true, "date": Date.parse("06-10-2018"), "city": "NY"},
{"ID": 2, "product": "Fan", "sales": false, "date": Date.parse("06-05-2018"), "city": "London"}, {"ID": 2, "product": "Fan", "sales": false, "date": Date.parse("06-05-2018"), "city": "London"},
{"ID": 3, "product": "Fan", "sales": true, "date": Date.parse("06-04-2018"), "city": "NJ"}, {"ID": 3, "product": "Fan", "sales": true, "date": Date.parse("06-04-2018"), "city": "NJ"},
{"ID": 4, "product": "Fan", "sales": false, "date": Date.parse("05-15-2018"), "city": "London"}, {"ID": 4, "product": "Fan", "sales": false, "date": Date.parse("05-15-2018")},
{"ID": 5, "product": "Light", "sales": true, "date": Date.parse("06-07-2016"), "city": "NY"}, {"ID": 5, "product": "Light", "sales": true, "date": Date.parse("06-07-2016"), "city": "NY"},
{"ID": 5, "product": "Light", "sales": false, "date": Date.parse("06-10-2019"), "city": "London"} {"ID": 5, "product": "Light", "sales": false, "date": Date.parse("06-10-2019")}
] ]
function clone_pluck(source, include_keys, exclude_keys) { function clone_pluck(source, include_keys, exclude_keys) {
...@@ -70,6 +70,19 @@ test('g1.datafilter test', function(t) { ...@@ -70,6 +70,19 @@ test('g1.datafilter test', function(t) {
t.end() t.end()
}) })
t.test('g1.datafilter([data], filter) with one conditions (=) works for undedined values', function (t) {
var result = g1.datafilter(product_sales, { "city": ["London"] })
var length_of_valid_data = 0
for (var row in product_sales) {
if (product_sales[row]["city"] == 'London') length_of_valid_data++
}
t.equals(result.length, length_of_valid_data)
t.ok(result.every(function (val) {
return val["city"] == 'London'
}))
t.end()
})
t.test('g1.datafilter([data], filter) with one conditions (!) works', function (t) { t.test('g1.datafilter([data], filter) with one conditions (!) works', function (t) {
var result = g1.datafilter(continent_data, { "ID!": ["AND"] }) var result = g1.datafilter(continent_data, { "ID!": ["AND"] })
var length_of_valid_data = 0 var length_of_valid_data = 0
...@@ -385,8 +398,7 @@ test('g1.datafilter test', function(t) { ...@@ -385,8 +398,7 @@ test('g1.datafilter test', function(t) {
t.test('g1.datafilter([data], filter, dataset_name) testing multiple dataset filter with dataset name not defined', function(t) { t.test('g1.datafilter([data], filter, dataset_name) testing multiple dataset filter with dataset name not defined', function(t) {
var ds2 = g1.datafilter(continent_data_small, {"ds1:Continent":["Asia"]}) var ds2 = g1.datafilter(continent_data_small, {"ds1:Continent":["Asia"]})
t.equals(ds2.length, continent_data_small.length) t.deepEquals(ds2, [])
t.deepEquals(ds2, continent_data_small)
t.end() t.end()
}) })
...@@ -394,7 +406,7 @@ test('g1.datafilter test', function(t) { ...@@ -394,7 +406,7 @@ test('g1.datafilter test', function(t) {
t.test('g1.datafilter([data], filter) testing datafilter with column filter not exists in the dataset', function(t) { t.test('g1.datafilter([data], filter) testing datafilter with column filter not exists in the dataset', function(t) {
var result = g1.datafilter(continent_data, {"Continent_not_exists":["Asia"]}) var result = g1.datafilter(continent_data, {"Continent_not_exists":["Asia"]})
t.equals(result.length, continent_data.length) t.equals(result.length, 0)
t.end() t.end()
}) })
......
...@@ -51,9 +51,6 @@ ...@@ -51,9 +51,6 @@
tape('$() test link false renders just td tag, without a tag', function(t) { tape('$() test link false renders just td tag, without a tag', function(t) {
$('.no-link').formhandler({ $('.no-link').formhandler({
columns: [ columns: [
{
name: '*'
},
{ {
name: 'Continent', name: 'Continent',
link: '?ID=AND', link: '?ID=AND',
...@@ -73,6 +70,9 @@ ...@@ -73,6 +70,9 @@
'Diagonal' 'Diagonal'
] ]
} }
},
{
name: '*'
} }
], ],
pageSize: 3, pageSize: 3,
...@@ -81,8 +81,8 @@ ...@@ -81,8 +81,8 @@
edit: true edit: true
}) })
.on('load', function () { .on('load', function () {
t.equals($('div.no-link tr:nth-child(1) > td:nth-child(3)').text().trim(), "AND") t.equals($('div.no-link tr:nth-child(1) > td:nth-child(2)').text().trim(), "AND")
t.equals($('div.no-link tr:nth-child(2) > td:nth-child(3)').text().trim(), "ARE") t.equals($('div.no-link tr:nth-child(2) > td:nth-child(2)').text().trim(), "ARE")
t.ok($('div.no-link tr:nth-child(1) > td:nth-child(1) a').hasClass('urlfilter'), 'has class urlfilter') t.ok($('div.no-link tr:nth-child(1) > td:nth-child(1) a').hasClass('urlfilter'), 'has class urlfilter')
t.ok($('div.no-link tr:nth-child(1) > td:nth-child(1) a').attr('href'), '?ID=AND') t.ok($('div.no-link tr:nth-child(1) > td:nth-child(1) a').attr('href'), '?ID=AND')
...@@ -93,9 +93,6 @@ ...@@ -93,9 +93,6 @@
tape('$() test delete action for empty data case and test for edit and add button', function(t) { tape('$() test delete action for empty data case and test for edit and add button', function(t) {
$('.delete_btn').formhandler({ $('.delete_btn').formhandler({
columns: [ columns: [
{
name: '*'
},
{ {
name: 'ID', name: 'ID',
editable: false editable: false
...@@ -109,7 +106,10 @@ ...@@ -109,7 +106,10 @@
'Horizontal', 'Horizontal',
'Diagonal' 'Diagonal'
] ]
} },
},
{
name: '*'
}, },
{ {
name: 'DELETE Action', name: 'DELETE Action',
...@@ -131,7 +131,7 @@ ...@@ -131,7 +131,7 @@
$('.add-btn').click() $('.add-btn').click()
t.ok($('.edit-btn').prop('disabled')) t.ok($('.edit-btn').prop('disabled'))
// stripes column is dropdown select HTML element and not input textbox // stripes column is dropdown select HTML element and not input textbox
t.equals($('div.delete_btn tr.new-row td:nth-child(6) select').length, 1) t.equals($('div.delete_btn tr.new-row td:nth-child(2) select').length, 1)
// all other columns must be input textbox and editable, overriding isEditable: false option also // all other columns must be input textbox and editable, overriding isEditable: false option also
t.equals($('div.delete_btn tr.new-row td:nth-child(1) input').length, 1) t.equals($('div.delete_btn tr.new-row td:nth-child(1) input').length, 1)
$('.add-btn').click() $('.add-btn').click()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment