I’m trying to better understand how filters work under the hood. The KTL works pretty well, but it always refreshes the whole page instead of only the view involved when a User Filter is clicked.
I found this code, which does exactly what I want, but I don’t understand the var u = new t.Model line. What is t? I breakpoint execution there and I can see the object, but can’t find a way to replicate this in my code. All the rest is ok, but not the t.
Help!
Normand
handleChangeFilters: function(e) {
var n = this
, i = n.this
, r = Knack.getSceneHash()
, a = e && e.rules && e.rules.length
, o = e && e.length;
if (a || o) {
var s = e
, c = {};
c[i.model.view.key + "_" + n.index + "_filters"] = encodeURIComponent(JSON.stringify(s)),
(l = Knack.getQueryString(c)) && (r += "?" + l)
} else {
var l;
(l = Knack.getQueryString(null, [i.model.view.key + "_" + n.index + "_filters"])) && (r += "?" + l)
}
i.menu_filters_changed = !0,
Knack.router.navigate(r, !1),
Knack.setHashVars(),
Knack.showSpinner();
var u = new t.Model;
u.url = i.model.data.url + "/" + n.index,
e && (u.url += "?filters_" + n.index + "=" + encodeURIComponent(JSON.stringify(e))),
i.model.view.scene && i.model.view.scene.scene_id && (u.url += "&" + i.model.view.scene.slug + "_id=" + i.model.view.scene.scene_id),
u.fetch({
success: function (e, t) {
Knack.hideSpinner(),
i.renderReport(n, t.report, n.index + 1),
i.triggerRecordsRendered()
}
})
},
I found them in the Sources view of the DevTools window (F12). I clicked Pause (F8 = pause/resume execution) a few times until I saw a weird filename like k_796ffd2fc065b2defb109c89d6d560f191cacf98.js This is Knack’s code (BTW, to see your code, pause until you see VMxxxx as the filename).
I pretty-printed (with the {} icon) and saved this Knack’s code file to my HD and did some regex searches on various strings such as handle.*sort and handle.*filter
Wow! That’s very interesting. I can’t wait to test it.
I wonder if it’s possible to get the normal (non-minified) version of Knack’s code. After all, it can’t be that much of secret if we already have it under our eyes - just a bit hard to read and understand. If I could get this, the KTL would jump to a whole new level.
Why do you have three backticks ``` at the end?
If I remove one, I don’t get any error anymore in Visual Studio, but I still don’t see the reason for this.
For all those interested to see the solution (hard-coded for quick testing), here it is:
var filters = {
"match": "and",
"rules": [
{
"field": "field_28",
"operator": "higher than",
"value": prompt('value'),
"field_name": "Prod Qty"
}
]
};
const updateFilters = (viewKey, filters) =>
new Promise((resolve) => {
const sceneHash = Knack.getSceneHash();
// getQueryString not only gets the query string from current hash vars, but **also** sets query string params when provided an object
const queryString = Knack.getQueryString({ [`${viewKey}_filters`]: encodeURIComponent(JSON.stringify(filters)) });
// navigates to new query string via Backbone router
Knack.router.navigate(`${sceneHash}?${queryString}`, false);
// updates internal hash vars from current url
Knack.setHashVars();
// set new filters on view's model
Knack.models[viewKey].setFilters(filters);
// refetch view data, using new filters
return Knack.models[viewKey].fetch({
success: () => resolve(),
});
});
updateFilters('view_2248', filters)
.then(function () {
console.log('Done filtering');
})
.catch(function () {
console.log('Error filtering');
})
I’ve bound the call to updateFilters from the F2 hotkey and boom! I saw the table update itself properly without the whole page refresh! Soooo much better.
This is great! I’ve just updated the KTL and the tables are refreshed smoothly - at last.
The User Filters also store the sort column/order, the records per page value and even the search text.
I strongly recommend you try this feature, it’s awesome.
If you’re curious to see the final code, search for function onFilterBtnClicked in the KTL.js file. Finally, I set all the parameters and invoke fetch only once for all elements of the filter, which is pretty cool. I wasn’t sure this would work, but it does.
Thanks again David (aka hmnd) for your help, I learned a lot during the last two days.
Can I use this for Map views too? I.e. create public filters that can be applied on the rendered records (properties/ addresses) once the initial map search is done?