Skip to content

Commit

Permalink
rearrange and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aldeed committed Jan 20, 2015
1 parent 34aaa95 commit 91e4cf7
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 214 deletions.
2 changes: 1 addition & 1 deletion .versions
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
aldeed:tabular@1.0.3
aldeed:tabular@1.0.4
application-configuration@1.0.4
base64@1.0.2
binary-heap@1.0.2
Expand Down
32 changes: 32 additions & 0 deletions client/pubSelector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* global getPubSelector:true, _ */

getPubSelector = function getPubSelector(selector, searchString, searchFields, searchCaseInsensitive) {
if (!searchString || !searchFields || searchFields.length === 0) {
return selector;
}

// See if we can resolve the search string to a number,
// in which case we use an extra query because $regex
// matches string fields only.
var numSearchString = Number(searchString), searches = [];

_.each(searchFields, function(field) {
var m1 = {}, m2 = {};

// String search
m1[field] = {$regex: searchString};
// DataTables searches are case insensitive by default
if (searchCaseInsensitive !== false) {
m1[field].$options = "i";
}
searches.push(m1);

// Number search
if (!isNaN(numSearchString)) {
m2[field] = numSearchString;//{$where: '/' + numSearchString + '/.test(this.' + field + ')'};
searches.push(m2);
}
});

return _.extend({}, selector, {$or: searches});
};
69 changes: 69 additions & 0 deletions client/tableInit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* global tableInit:true, _, Blaze, Util */

/**
* Uses the Tabular.Table instance to get the columns, fields, and searchFields
* @param {Tabular.Table} tabularTable The Tabular.Table instance
* @param {Template} template The Template instance
*/
tableInit = function tableInit(tabularTable, template) {
var columns = _.clone(tabularTable.options.columns);
var fields = {}, searchFields = [];

// Loop through the provided columns object
_.each(columns, function (col) {
// The `tmpl` column option is special for this
// package. We parse it into other column options
// and then remove it.
var tmpl = col.tmpl;
if (tmpl) {
col.defaultContent = "";
col.orderable = false;
col.createdCell = function (cell, cellData, rowData) {
Blaze.renderWithData(tmpl, rowData, cell);
};
delete col.tmpl;
}

// Automatically protect against errors from null and undefined
// values
if (!("defaultContent" in col)) {
col.defaultContent = "";
}

// Build the list of field names we want included
var dataProp = col.data;
if (typeof dataProp === "string") {
// If it's referencing an instance function, don't
// include it. Prevent sorting and searching because
// our pub function won't be able to do it.
if (dataProp.indexOf("()") !== -1) {
col.sortable = false;
col.searchable = false;
return;
}

fields[Util.cleanFieldName(dataProp)] = 1;

// DataTables says default value for col.searchable is `true`,
// so we will search on all columns that haven't been set to
// `false`.
if (col.searchable !== false) {
searchFields.push(Util.cleanFieldNameForSearch(dataProp));
}
}

// If we're displaying a template for this field,
// don't pass the data prop along to DataTables.
// This prevents both the data and the template
// from displaying in the same cell. We wait until
// now to do this to be sure that we still include
// the data prop in the list of fields.
if (tmpl) {
col.data = null;
}
});

template.tabular.columns = columns;
template.tabular.fields.set(fields);
template.tabular.searchFields = searchFields;
};
Loading

0 comments on commit 91e4cf7

Please sign in to comment.