none
Search-driven navigation RRS feed

  • Question

  • Hi,

    I've read the article on how to implement a search-driven navigation here but can't get it to work properly

    I copy the method ByHierarchy to linq.js 

    ByHierarchy: function(firstLevel, connectBy, orderBy, ascending, parent) {
         ascending = ascending == undefined ? true : ascending;
         var orderMethod = ascending == true ? 'OrderBy' : 'OrderByDescending';
         var source = this;
         firstLevel = Utils.CreateLambda(firstLevel);
         connectBy = Utils.CreateLambda(connectBy);
         orderBy = Utils.CreateLambda(orderBy);
        
         //Initiate or increase level
         var level = parent === undefined ? 1 : parent.level + 1;
    
        return new Enumerable(function() {
             var enumerator;
             var index = 0;
    
            var createLevel = function() {
                     var obj = {
                         item: enumerator.Current(),
                         level : level
                     };
                     obj.children = Enumerable.From(source).ByHierarchy(firstLevel, connectBy, orderBy, ascending, obj);
                     if (orderBy !== undefined) {
                         obj.children = obj.children[orderMethod](function(d) {
                             return orderBy(d.item); //unwrap the actual item for sort to work
                         });
                     }
                     obj.children = obj.children.ToArray();
                     Enumerable.From(obj.children).ForEach(function(child) {
                         child.getParent = function() {
                             return obj;
                         };
                     });
                     return obj;
                 };
    
            return new IEnumerator(
    
            function() {
                 enumerator = source.GetEnumerator();
             }, function() {
                 while (enumerator.MoveNext()) {
                     var returnArr;
                     if (!parent) {
                         if (firstLevel(enumerator.Current(), index++)) {
                             return this.Yield(createLevel());
                         }
    
                    } else {
                         if (connectBy(parent.item, enumerator.Current(), index++)) {
                             return this.Yield(createLevel());
                         }
                     }
                 }
                 return false;
             }, function() {
                 Utils.Dispose(enumerator);
             })
         });
     },

    But there are a few issues like method names in linq.js starts with lowercase so Utils.Dispose has to be changed to Utils.dispose, ToArray to toArray and so on, there's no Yield but yieldBreak etc.

    Anyway when I get to 

                     if (orderBy !== undefined) {
                         obj.children = obj.children[orderMethod](function(d) {
                             return orderBy(d.item); //unwrap the actual item for sort to work
                         });
                     }

    It errors out with "Uncaught TypeError: obj.children[orderMethod] is not a function", apart from the things mentioned above is there anything else I have to change to get this to work? I have had a look at some other articles here and here but neither of them mention any issues with this method.

    Any ideas?

    Wednesday, July 18, 2018 7:06 AM

Answers