1

Adding sort code to the client

This commit is contained in:
Alex Yatskov 2015-03-27 12:03:23 +09:00
parent 19e897ae3f
commit 18458b83c0
4 changed files with 36 additions and 18 deletions

View File

@ -52,7 +52,7 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) {
features := fixFeatures(request.Features) features := fixFeatures(request.Features)
foundEntries := findRecords(entries, features, request.MinScore) foundEntries := findRecords(entries, features, request.MinScore)
sorter := recordSorter{entries: foundEntries, key: request.SortKey, ascending: request.SortAscending} sorter := recordSorter{entries: foundEntries, key: request.SortKey, ascending: request.SortAsc}
sorter.sort() sorter.sort()
response := jsonQueryResponse{ response := jsonQueryResponse{

View File

@ -96,13 +96,13 @@
<table class="table table-striped table-condensed"> <table class="table table-striped table-condensed">
<thead> <thead>
<tr> <tr>
<th>Name</th> <th><a href="javascript:sortReviewsBy('name');">Name</a></th>
<th>Distance to user</th> <th><a href="javascript:sortReviewsBy('distanceToUser');">Distance to user</a></th>
<th>Closest station</th> <th><a href="javascript:sortReviewsBy('closestStn');">Closest station</a></th>
<th>Distance to station</th> <th><a href="javascript:sortReviewsBy('distanceToStn');">Distance to station</a></th>
<th>Compatibility</th> <th><a href="javascript:sortReviewsBy('compatibility');">Compatibility</a></th>
<th>Score</th> <th><a href="javascript:sortReviewsBy('score');">Score</a></th>
<th>Access count</th> <th><a href="javascript:sortReviewsBy('accessCount');">Access count</a></th>
</tr> </tr>
</thead> </thead>
{{#each records}} {{#each records}}

View File

@ -35,7 +35,12 @@
} }
function onReady(geo) { function onReady(geo) {
_ctx = {geo: geo, query: {}}; _ctx = {
sortKey: 'score',
sortAsc: false,
query: {},
geo: geo
};
Handlebars.registerHelper('prettyFloat', function(precision, options) { Handlebars.registerHelper('prettyFloat', function(precision, options) {
return parseFloat(options.fn(this)).toFixed(precision); return parseFloat(options.fn(this)).toFixed(precision);
@ -55,12 +60,25 @@
onSearch(); onSearch();
}; };
window.sortReviewsBy = function(sortKey) {
if (sortKey === _ctx.sortKey) {
_ctx.sortAsc = !_ctx.sortAsc;
}
else {
_ctx.sortKey = sortKey;
}
onSearch();
};
onSearch(); onSearch();
} }
function onSearch() { function onSearch() {
_ctx.query = { _ctx.query = {
features: _ctx.query.features || {}, features: _ctx.query.features || {},
sortKey: _ctx.sortKey,
sortAsc: _ctx.sortAsc,
profile: getProfile(), profile: getProfile(),
walkingDist: parseFloat($('#walkingDist').val()), walkingDist: parseFloat($('#walkingDist').val()),
minScore: parseFloat($('#minScore').val()), minScore: parseFloat($('#minScore').val()),

View File

@ -37,15 +37,15 @@ type jsonGeoData struct {
} }
type jsonQueryRequest struct { type jsonQueryRequest struct {
Features featureMap `json:"features"` Features featureMap `json:"features"`
Geo *jsonGeoData `json:"geo"` Geo *jsonGeoData `json:"geo"`
HintSteps int `json:"hintSteps"` HintSteps int `json:"hintSteps"`
MaxResults int `json:"maxResults"` MaxResults int `json:"maxResults"`
MinScore float64 `json:"minScore"` MinScore float64 `json:"minScore"`
Profile featureMap `json:"profile"` Profile featureMap `json:"profile"`
SortAscending bool `json:"SortAscending"` SortAsc bool `json:"sortAsc"`
SortKey string `json:"sortKey"` SortKey string `json:"sortKey"`
WalkingDist float64 `json:"walkingDist"` WalkingDist float64 `json:"walkingDist"`
} }
type jsonColumn struct { type jsonColumn struct {