Search code examples
knockout.jskogrid

Can I use custom filters with KOGrid?


I have some custom filters, I could put them outside of the grid but it would look nicer if they were located at the same place as the built in one, can I somehow alter the filter template or in some other way alter the built in filter view?

This is the view I want to extend with custom filters

enter image description here


Solution

  • I started to implement this in a KoGrid fork, but the KoGrid template code is a bit complex, and I didnt have time to get it to work right now. So I did a little hack that does not alter the KoGrid source

    http://jsfiddle.net/t23Ub/10/

    (function() {
        function initGridTemplate() {
                var template = $(kg.defaultGridTemplate());
                var filter = template.find("div.kgColMenu > div[data-bind='visible: showFilter']");
            filter.attr("data-name", "config.filterOptions.model");
                filter.html("");
    
                kg.defaultGridTemplate = function () {
                    return template[0].outerHTML;
                };
        }
        initGridTemplate();
    }());
    

    It works by adding a view model to the filterOptions literal. data-name in the above code is my framework that find views on viewmodels types, you could instead do

    filter.attr("data-bind", "template: { name: config.filterOptions.templateName, data: config.filterOptions.model }");
    

    This is what my data-name attribute is doing under the hood