Last active
August 29, 2015 14:24
-
-
Save bademux/35596b30835f3b8346c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// predicates filter to extjs query format | |
//usage put in html <input st-search="like['test.text']"> | |
//then parse SmartTable's tableState var criteria = queryParserFilter(tableState); | |
myModule | |
.filter('queryParser', function () { | |
return function (tableState) { | |
var criteria = { | |
start: tableState.pagination.start, | |
limit: tableState.pagination.number | |
}; | |
if (tableState.sort.predicate) { | |
criteria.sort = [{ | |
property: tableState.sort.predicate, | |
direction: tableState.sort.reverse ? 'DESC' : 'ASC' | |
}]; | |
} else { | |
criteria.sort = []; | |
} | |
var predicates = tableState.search.predicateObject; | |
//parse query, e.g. eq['content.id']='test' | |
if (predicates) { | |
criteria.filter = Object.keys(predicates).map(function (op) { | |
return Object.keys(predicates[op]).map(function (prop) { | |
var val = predicates[op][prop]; | |
if ('like' == op) { | |
val = '%' + val + '%' | |
} | |
if (!prop || !op || !val) { | |
return undefined; | |
} | |
return { | |
property: prop, | |
operator: op, | |
value: val | |
}; | |
}); | |
})[0].filter(function (element) { | |
return element; | |
}); | |
} else { | |
criteria.filter = []; | |
} | |
return criteria; | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment