0

I have a simple view with one exposed filter that displays a table view of contents.(for example nodes are some addresses with the exposed filter of the city). And I made a map for cities;I want that whenever user clicks on a city in the map, the selected city become exposed filter of view and view become refreshed without refreshing the whole page.is it possible? I thought that I can use something like:

$.get(
'/myviewpage',
{myfield=14},
function(data){  $('#SomeOtherDiv').html(data) }
);

I tried that but I got unauthorized error. if it's correct; how can I make a page view-able for everyone? thanks

1

The way I would do it (and it works):

Instead of directly calling the page with JQuery $.get, you should use the Drupal ajax framework and call a custom path you created with hook_menu with a callback function which calls the view and return it using ajax_deliver. Then you create your own js function for the response.

There is some nice documentatione/example about this, particularly this one.

  • An example of how adding code would be useful... The site no longer exists.... – Chris Happy Oct 6 '17 at 1:29
3

First, enable Ajax on the view. On window load, bind the click event to a custom function, let's call it getInfo(arg) and pass the arguments you need (nid, tid, or whatever you need)

Then, on getInfo() add the following code.

function getInfo(args) {
  $.ajax({
    url: '/views/ajax',
    type: 'post',
    data: {
      view_name: 'your_view_name',
      view_display_id: 'block_1', //your display id
      view_args: args,
    },
    dataType: 'json',
    success: function (response) {
      if (response[1] !== undefined) {
        var viewHtml = response[1].data;
        $('#myDiv').html(viewHtml);
        Drupal.attachBehaviors(); //check if you need this.
      }
    }
  });
}

You pass the arguments in the data array, and you get the #myDiv replaced with the view content.

1

You can not use hook_menu. Exposed filter of view send request to /views/ajax, you can add param for this request view_name=[view_name]&view_display_id=[view_display_id], so you only get the content that you want. If you want to use JSON, install views_datasource

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.