We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 35781
    • 25 Posts
    Hello, I am trying to get wf menu through ajax, but i have a little problem: wf menu doesn't worh as it should work. No active classes are added to items and so on.
    My page template
    ...
    <div class="ajax"></div>
    <script>
    $.post('/ajax',function(data){
      $('.ajax').html(data);
    });
    </script>
    ...
    

    In backend I've got resource with blank template(id 0) and alias ajax (resource is published), it contents:
    [[!ajax?]]
    

    so, i have snippet 'ajax':
    return $modx->runSnippet("wayfinder",array('startId'=>'9','level'=>'3','rowTpl'=>'rowTpl','levelClass'=>'level','sortBy'=>'pagetitle'));
    

    All work fine, but when I click menu item, wf doesn't add active class to it. I understand, that when I use '$modx->runSnippet', wf doesn't know active item. But I can't understand, how to make it work. [ed. note: Qwarble last edited this post 14 years, 8 months ago.]
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      I would try simply using the Wayfinder snippet in a resource with no template as your AJAX processor.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 35781
        • 25 Posts
        i've just tried this and nothing changed. Wf put menu as if i would open main page. Without any 'active' class.
          • 22427
          • 793 Posts
          It might help to include the active class explicitely, using &hereClass:
          return $modx->runSnippet("wayfinder",array('startId'=>'9','level'=>'3','rowTpl'=>'rowTpl','levelClass'=>'level','hereClass'=>'active','sortBy'=>'pagetitle'));
            • 4172
            • 5,888 Posts
            you can tr this:

            put your complete template-content in a chunk (defaultChunk).
            and put the wayfinder-call to another chunk (ajaxChunk)

            create a snippet called ajaxSwitch with this code:

            $output = '';
            
            $properties =& $scriptProperties;
            $properties['ajaxid'] = isset($_REQUEST['ajaxid']) ? $_REQUEST['ajaxid'] : 'none';
            
            
            if ($properties['ajaxid'] == 'none'){
                $output = $modx->getChunk($defaultChunk);      
            }else{
                $output = $modx->getChunk($ajaxChunk,$_REQUEST);  
            }
            
            return $output;
            


            and put in your Template only this snippet-call:

            [[!ajaxSwitch? &ajaxChunk=`ajaxChunk` &defaultChunk=`defaultChunk` ]]


            your javascript something like that:
            <script>
            $.post('[[~[[*id]]? &ajaxid=`test`]]',function(data){
              $('.ajax').html(data);
            });
            </script>
            

            [ed. note: Bruno17 last edited this post 14 years, 9 months ago.]
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 35781
              • 25 Posts
              Everybody thanks for help!

              I've solved the problem. When I made ajax call of WF to resource 'ajax', it gives to chunk id of resource 'ajax', but not the current id. To make it work in normal way, I have to add parameter 'hereId' to my WF call (which i found when i was looking through WF source).
              So, now I have:
              snippet 'ajax':
              return $modx->getChunk($ajaxChunk,array('hereId'=>$_REQUEST['hid']));
              

              resource with alias 'ajax':
              [[!ajax? &ajaxChunk=`ajaxChunk`]]
              

              ajaxChunk:
              [[Wayfinder? &hereId=`[[+hereId]]` &startId=`9` &level=`3` &rowTpl=`rowTpl` &levelClass=`level` &sortBy=`pagetitle`]]
              

              And a chunk, that maks ajax call and should be in template:
              <div class="ajax">/div>
              <script>
              $.post('/ajax',{hid:[[*id]]},function(data){
                $('.ajax').html(data);
              });
              </script>
              

              And now all works in a propper way