We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37984
    • 215 Posts
    Just now discovering the power of MIGXdb combined with BloX, and I'm loving it. One issue I'm running into is how to sort my lists by default.

    I would like to sort both my CMP list (using MIGXdb) and my frontend output by one of my custom table's columns (specifically, a date).

    On the frontend: I notice in the blox.php file in the core, there is an option called "sortConfig" but that is the only reference I see for sorting. How would I go about sorting my output by date?

    On the backend: I don't have the foggiest how to apply a sort by default. I can use the MIGX Configurator to update my date field to be sortable, but that's only once the user selects that column's sorting toggle. I would like it to be sorted correctly when the page loads, and when a user adds a new item.

    Any thoughts at all? I'm not looking of a complete solution if it's too much trouble; just a direction to head in. Thanks!

    This question has been answered by Bruno17. See the first response.

      Jesse Couch
      MODX Aficionado, Front-End Designer & Developer
      http://www.designcouch.com
      • 8274 ☆ A M B ☆
      • 222 Posts
      Backend is pretty easy. In the MIGXdb componet, edit your config, then go to the MIGXdb Settings tab. There is a field for getlist defaultSort. Just assign it the index name of the column (you can set the default sort direction below it. I believe it defaults to ASC).

      This will sort you backend grid by that column on load.

      As for blox, the blox.class.inc file uses the sortConfig setting to create a sortby for the database call. So, iyou should be able to add that to your blox call
      [[blox?&sortConfig=`column.name`]]


      give that a try and see. It looks like there hasn't been a config set up for sortDir, although the class looks for it, so you could always give it a try as
      [[blox?&sortDir=`DESC`]]
      to see.

      I haven't used blox since I typically load the class that MIGXdb created via modx's load Package function.
        • 37984
        • 215 Posts
        That first part absolutely works. My backend grid is sorting like a champ now. Thanks!

        The second part (blox call) doesn't seem to affect the sort on the frontend at all. I would think that by default, there would be some sort of default sorting ability, but I can't seem to figure it out.
          Jesse Couch
          MODX Aficionado, Front-End Designer & Developer
          http://www.designcouch.com
          • 8274 ☆ A M B ☆
          • 222 Posts
          If Bruno hadn't added that as a config for the call, then I am not sure how to get it to do it without getting into the php classes and setting it up.

          you can always roll your own snippet to access your MIGXdb package:
          http://rtfm.modx.com/xpdo/2.x/class-reference/xpdo/xpdo.addpackage

          Since MIGXdb essentially creates all the classes for your package, you can just load that package and use it to pull results for the custom table it created for your package. This may be better since you need further control over the query output.
          • discuss.answer
            • 4172
            • 5,888 Posts
            you can use bloX,migxLoopCollection,rowboat or pdoResources for frontend - listings.

            example with migxLoopCollection:

            [[!migxLoopCollection?
            &packageName=`yourPackage`
            &classname=`yourClass`
            &tpl=`yourTpl`
            &sortConfig=`[{"sortby":"date","sortdir":"DESC"}]`
            ]]


            I use bloX only, when I need to create very special custom-stuff.

            I think pdoResources is also a very good and fast option (I'm using it with migxMultiLang to get the translated stuff).
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 8274 ☆ A M B ☆
              • 222 Posts
              Does bloX take a JSON string for the sortConfig as well? I assume so (I totally missed the parsing part in the class file. Scrolling too fast!).

                • 37984
                • 215 Posts
                @dvstudiosinc: no, it won't take a JSON string. However, using migxLoopCollection works perfectly. Thanks, Bruno! I'd mark both of your answers as the correct answer if I could.
                  Jesse Couch
                  MODX Aficionado, Front-End Designer & Developer
                  http://www.designcouch.com
                  • 4172
                  • 5,888 Posts
                  Quote from: dvstudiosinc at Feb 18, 2014, 11:13 AM
                  Does bloX take a JSON string for the sortConfig as well? I assume so (I totally missed the parsing part in the class file. Scrolling too fast!).


                  it does:
                  https://github.com/Bruno17/bloX/blob/master/core/components/blox/inc/blox.class.inc.php#L78
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 37984
                    • 215 Posts
                    @Bruno17 - Hmm... I put the JSON string I'm using now with migxLoopCollection (it works) into blox, and it didn't work...
                      Jesse Couch
                      MODX Aficionado, Front-End Designer & Developer
                      http://www.designcouch.com