We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7434
    • 24 Posts
    Hi there

    I have a list of articles and I want to show, let's say, 10 of them in a page but I want to give the option to the visitor to sort them in 2 different ways. For example by date or by a TV I have created.
    There will be 2 different links (Sort by Date, Sort by TV) and the user will click what he wants

    Each sort requires a different ditto call.

    I'd like to avoid the solution of using a carousel where the content of both ditto calls will be loaded and then I will show/hide accordingly.

    I was thinking of putting each ditto call to a different chunk and use a url parameter (or something else?) which will load a different chunk each time (I don't mind the page reload). Is something like that possible?

    Thanks in advance
      • 4041
      • 788 Posts
      Create a new snippet with the code below (named whatever you like), modifying the $params in each spot accordingly. The params array is the same as the params in a ditto call (note no comma after the last param_value)
      Then add the snippet call to your page:
      [!snippet_name!]

      links would end something like this depending on furl usage:
      ?sort=Date / &sort=Date
      ?sort=TV / &sort=TV

      <?php
      $output ='';
      $sort = isset($_GET['sort']) ? $_GET['sort'] : 'Date';
      
          switch($sort){
              case "Date":
                  $params = array(
                      'tpl' => 'Date_tpl',
                      'param'  => 'param_value'
                  );
              break;
      
              case "TV":
                  $params =array(
                      'tpl' => 'TV_tpl',
                      'param'  => 'param_value'
                  );
              break
              default: '';
          }
      
      $output =$modx->runSnippet("Ditto",$params);
      return $output;
      ?>
        xforum
        http://frsbuilders.net (under construction) forum for evolution
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        You could simplify by having your Ditto snippet use an external config file. Since these files are ordinary .php files, you can have conditional statements and other PHP code. So the $orderBy variable (in the .php file the &orderBy parameter becomes a simple PHP variable) would look something like this:

        $sort = isset($_GET['sort']) ? $_GET['sort'] : 'Date';
        $orderBy = $sort;
        


        Or if you wanted to avoid the ugly URL, use a small form with two submit buttons and use POST, using the value attribute of the button.

        However, to avoid the page refresh, I would use AJAX in much the same way; have the content of the <div> element holding your Ditto call replaced by the result of the AJAX processor. This processor would be pretty much the same snippet in a resource with a blank template. See http://api.jquery.com/jQuery.post/
          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
          • 7434
          • 24 Posts
          Thanks both of you smiley
          Great ideas. I'll try them right away

          Regards
          Dimitris