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

    I’m trying to refresh a part of a page through an AJAX call, to change the way a list of items is displayed. If the select list change, an ajax call is made to refresh the list.

    Here’s the code JS Code :
        $('#ChangeListeItems').change(function(){
           // Disparition du contenu textuel actuel
           $("#ListeItem").html('</br>Récupération de la liste en cours...');
           // Récupération de la valeur
           Value=$(this).attr('value');
            // Construction de la ligne de paramètres
            $Parameters="&SelectValue="+Value;
            // Appel en ajax de la gestion de la mailing list
            $.ajax({
                async: false,
                url: "assets/templates/mlab/snippet/loadListItems.php",
                type: 'POST',
                data: $Parameters, // données à transmettre
                dataType: 'json',
                timeout: 1500,
                cache: false,
                error: function(XMLHttpRequest, textStatus, errorThrown){
                        $("#errorDiv").html("<p>"+errorThrown+"</p>");
                        $("#errorDiv").css({'display':'block'});
                },
                success: function(retour){ // si l'appel a bien fonctionné
                    if (retour.success) {
                        $("#ListeItem").html(retour.success);
                    } else {
                        $("#ListeItem").html(retour.error);
                    }
                }
            });
    
        });
    


    PHP Code "loadListItems.php"
    // Headers
    $path=explode("assets",realpath(dirname(__FILE__)));
    define('MODX_CORE_PATH', $path[0].'core/');
    define('MODX_CONFIG_KEY', 'config');
    require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
    $modx= new modX();
    $modx->initialize('mgr');
    $modx->initialize( 'connector' );
    $modx->getRequest();
    $modx->request->sanitizeRequest();
    
    // Récupération des paramètres : Id, TargetTemplate, FilterTV, SortTV, Tpl
    $Parameters=explode('|',$_POST['SelectValue']);
    
    // Appel du snippet
    $listItems = $modx->runSnippet('getResources', array('parents'=>'4'));
    // Gestion du retour
    $output=array();
    if (!empty($listItems)) {
        $output["success"] = 1;
        $ouput["html"]=$listItems;
    } else {
        $output["success"] = 0;
        $output["error"]= "Problème dans l'affichage des informations";
    }
    echo json_encode($output);
    ?>
    


    This code is working, getResources is executed but for example L90 $total is 0 : no resources is found!
    $total = $modx->getCount('modResource', $criteria);


    I must say that there are child resources under docid 4. Did I miss something in the headers for a snippet to be correctly excecuted?


    Many thanks for your help!


    Configuration :
    MODx Revolution 2.0.8-pl (traditional)
    Mysql server 5.1.49
    Apache server 2.2.16
    OS Ubuntu 10.10
      Equilibre Webdesign - Conception et r
      • 8930
      • 44 Posts
      After a look to index.php to see what include is needed, I obtain this code
      define('MODX_API_MODE', true);
      $path=explode("assets",realpath(dirname(__FILE__)));
      @include($path[0]. '/config.core.php');
      if (!defined('MODX_CORE_PATH')) define('MODX_CORE_PATH', $path[0].'core/');
      @include_once MODX_CORE_PATH . 'model/modx/modx.class.php';
      /* start output buffering */
      ob_start();
      $modx= new modX();
      $modx->initialize('web');


      There was two small mistakes in JS call. Now, it works pretty well, except that code included in tpl is not evaluate. I get that :
      Etude de cas et généralisation scientifique. [[!getTVList? &id=`72` &tvName=`ListeMembresTV` &tpl=`ListeHorizontaleTpl` &tplLast=`ListeHorizontaleTpl-last`]] [[!If? &subject=`` &operator=`notempty` &then=` , `]] - [[!If? &subject=`2004` &operator=`eq` &operand=`9999` &then=`A paraître` &else=`2004`]] [[!If? &subject=`Revue Sciences de Gestion, n°39, décembre 2004` &operator=`notempty` &then=` - Revue Sciences de Gestion, n°39, décembre 2004 `]] 
      


      Any way to evaluate calls in the tpl?

      Thanks


        Equilibre Webdesign - Conception et r
        • 4172
        • 5,888 Posts
        Its allways the easiest way to create just a resource with blank template and put your snippet-call there.
        Use the url of this resource in your ajax-request.
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 8930
          • 44 Posts
          Thanks Bruno for your answer. I will try until this week-end and will post the result.

            Equilibre Webdesign - Conception et r
            • 8930
            • 44 Posts
            Hi all,

            thanks to Bruno I made a resource for each filter and it works perfectly.

            Here’s an example of resource :
            [[getFilters? &tvFilters=`ListeThemesTV` &tpl=`ListeFiltresTpl`]]
            ||SEPARATION||
            <ul>
            [[!getResourcesFilter? &parents=`5` &limit=`0` &where=`{"template:=":7}` &tvFilters=`ListeThemesTV` &sortbyTV=`ProjetAnneeTV` &includeTVs=`1` &tpl=`ListeProjetsTpl` &sectionTpl=`ListeSectionsTpl`]]
            </ul>


            getFilters snippet will return a form like that :
            <form>
                            <select id="ChangeListeItems">
                                <option value="97">par Date</option>
                                <option value="96">par Type/Date</option>
                                <option value="94">par Thème/Date</option>
                                <option value="98">par Auteur/Date</option>
                            </select>
            
            </form>



            The value of each option is the corresponding resource id, used for the ajax call :
                $('#ChangeListeItems').change(function(){
                   // Disparition du contenu textuel actuel
                   $("#ListeItem").html('</br>Récupération de la liste en cours...');
                   // Récupération de la valeur
                   Value=$(this).attr('value');
                    // Construction de la ligne de paramètres
                    $Parameters="id="+Value;
                    // Appel en ajax de la gestion de la mailing list
                    $.ajax({
                        async: false,
                        url: indexURL+"?id="+Value,
                        type: 'POST',
                        data: '', // données à transmettre
                        dataType: 'html',
                        timeout: 1000,
                        cache: false,
                        error: function(XMLHttpRequest, textStatus, errorThrown){
                                $("#ListeItem").html("<p>"+errorThrown+"</p>");
                        },
                        success: function(retour){ // si l'appel a bien fonctionné
                            // Traitement du retour -> séparation des deux zones
                            Retours = retour.split("||SEPARATION||");
                            // Affectation dans les zones qui vont bien
                            $("#FormFiltre").html(Retours[0]);
                            $("#ListeItem").html(Retours[1]);
                        }
                    });
                });
            


            Hope it helps!
              Equilibre Webdesign - Conception et r