0

I have a views with three displays which sorts comments of a node in newest, oldest and most popular order. These views are shown below the content and works with contextual filters. I have used ajax_comment and rate and vote api module in order to add/reply comments by ajax and let users vote other people's comments too. Now I want to load one of these views at a time so I have created an HTML like this:

<select name="sort" onChange="return sort1()">
    <option value="newest">newest comments</option>
    <option value="oldest">oldest commenst</option>
    <option value="popular">most popular comments</option>
</select>
<div id="cm"></div>

and a jquery code like this:

<script>
function sort1(){
$.ajax({
    url: Drupal.settings.basePath + '/views/ajax',
    type: 'post',
    data: {
        view_name: 'comments_views',
        view_display_id: 'block_2', //your display id
    },
    dataType: 'json',
    success: function(html){
        $('#cm').html(html);
    }
});
    return false;   
}
</script> 

But it does not work.

I have tested quicktabs module too but comment reply link does not work with ajax_comment then. I have seen JQuery AJAX Load module too but it needs a url to load.

If my code is incorrect please correct it or if you have a better solution please let me know it.

  • When you say it doesn't work, what part doesn't work? What debugging have you tried, for example did you test if the ajax call is being called? – longboardnode Jan 23 '17 at 5:10
0

I post my answer for anyone who is facing this problem. After all it worked by some changes:

<div onclick="return sort1()">newest comments</div>

<div onclick="return sort2()">oldest commenst</div>

<div onclick="return sort3()">most popular comments</div>
<div id="cm">
     <div id='news'><?php print views_embed_view('comments_views', 'block_3'); ?>

and jQuery code is:

function sort1(){
var nid='<?php echo $node->nid; ?>';    
$.ajax({
    url: 'http://www.yoursitename.com/views/ajax',
    type: 'post',
    data: {
        view_name: 'comments_views', //view name
        view_display_id: 'block_3', //view display
        view_args: nid,   //view contexuall filter
    },
    dataType: 'json',
    success: function(response){
        //alert(response[1].data);
        $('#cm').html(response[1].data);
    }
});
    return false;   
}

for each of the sort functions above it must be exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.