...
 
Commits (5)
......@@ -81,6 +81,8 @@ export function formhandler(js_options) {
options.columns = []
else if (typeof options.columns == 'string')
options.columns = _.map(options.columns.split(/\s*,\s*/), function (col) { return { name: col } })
// If any column name is '*', show all columns
var star_col_opts = _.find(options.columns, { name: '*' })
// Compile all templates
var template = {}
......@@ -94,24 +96,22 @@ export function formhandler(js_options) {
meta.rows = data.length
meta.columns = data.length ? _.map(data[0], function (val, col) { return { name: col } }) : []
// If any column name is '*', show all columns
var star_col = _.find(options.columns, function (o) { return o['name'] === '*' })
if (star_col) {
var action_header_cols = _.cloneDeep(meta.columns)
_.map(options.columns, function (option_col) {
var found = _.find(meta.columns, function (o) { return o['name'] === option_col.name })
if (!found && option_col.name !== '*')
action_header_cols.push(option_col)
})
action_header_cols = _.map(action_header_cols, function (col) {
var options_col = _.find(options.columns, function (o) { return o['name'] === col.name })
return options_col ? options_col : $.extend({}, star_col, col)
// If '*' is used as a column name, create the new column list, preserving
// column order
if (star_col_opts) {
var new_columns = []
options.columns.forEach(function (col_opts) {
if (col_opts.name === '*')
_.differenceBy(meta.columns, options.columns, 'name')
.forEach(function (meta_col_opts) {
new_columns.push(Object.assign({}, star_col_opts, meta_col_opts))
})
else
new_columns.push(col_opts)
})
options.columns = new_columns
}
options.columns = action_header_cols ? action_header_cols : options.columns
// Render all components into respective targets
var template_data = {
data: data,
......@@ -141,12 +141,20 @@ export function formhandler(js_options) {
var url_args = parse(location.hash.replace(/^#/, '')).searchList
url_args = namespace(url_args, options.name)
// Create arguments passed to the FormHandler. Override with the user URL args
var args = _.extend({
c: options.columns.map(function (d) { return d.name }),
var default_url_args = {
_limit: options.pageSize,
_format: 'json',
_meta: 'y'
}, url_args)
}
// If columns are specified, fetch them. If '*' is used (or no columns
// are mentioned), fetch all columns.
// TODO: add test case for options.columns as a comma separated list?
// TODO: add test case for when columns are not mentioned at all
if (!star_col_opts)
default_url_args['_c'] = _.map(options.columns, 'name')
var args = _.extend(default_url_args, url_args)
$('.loader', $this).removeClass('d-none')
function done(data, status, xhr) {
......
......@@ -35,6 +35,8 @@
<body>
<div class="no-link" data-src="/formhandler-data"></div>
<div class="star_order" data-src="/formhandler-data"></div>
<div class="star_order2" data-src="/formhandler-data"></div>
<div class="row_template1" data-src="/formhandler-data"></div>
<div class="delete_btn" data-src="/formhandler-data"></div>
<div class="test-star-without-title" data-src="/formhandler-data"></div>
......@@ -48,12 +50,40 @@
<script>
tape('$() test * column retains order when columns is a comma seperated string', function(t) {
$('.star_order').formhandler({
columns: 'Stripes,*,ID',
pageSize: 2
})
.on('load', function(args) {
var cols = $('.star_order th').map(function () { return this.getAttribute('data-col') }).get()
t.deepEquals(cols, [
"Stripes",
"Continent", "Cross", "Name", "Shapes", "Symbols", "Text&", "Union-Flag", "c1",
"c2", "c3", "c4", "c5", "c6", "c7", "c8", "date col", "Bool Col",
"ID"
])
t.end()
})
})
tape('$() test * column retains order when columns are not mentioned at all', function(t) {
$('.star_order2').formhandler({
pageSize: 2
})
.on('load', function(args) {
var cols = $('.star_order2 th').map(function () { return this.getAttribute('data-col') }).get()
t.deepEquals(cols, [
"Continent", "Cross", "ID", "Name", "Shapes", "Stripes", "Symbols", "Text&",
"Union-Flag", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "date col", "Bool Col"
])
t.end()
})
})
tape('$() test link false renders just td tag, without a tag', function(t) {
$('.no-link').formhandler({
columns: [
{
name: '*'
},
{
name: 'Continent',
link: '?ID=AND',
......@@ -73,6 +103,9 @@
'Diagonal'
]
}
},
{
name: '*'
}
],
pageSize: 3,
......@@ -81,8 +114,8 @@
edit: true
})
.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(2) > td:nth-child(3)').text().trim(), "ARE")
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(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').attr('href'), '?ID=AND')
......@@ -93,9 +126,6 @@
tape('$() test delete action for empty data case and test for edit and add button', function(t) {
$('.delete_btn').formhandler({
columns: [
{
name: '*'
},
{
name: 'ID',
editable: false
......@@ -109,7 +139,10 @@
'Horizontal',
'Diagonal'
]
}
},
},
{
name: '*'
},
{
name: 'DELETE Action',
......@@ -131,7 +164,7 @@
$('.add-btn').click()
t.ok($('.edit-btn').prop('disabled'))
// 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
t.equals($('div.delete_btn tr.new-row td:nth-child(1) input').length, 1)
$('.add-btn').click()
......