CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • What is the correct way to use ajax
Pages: [1]

Author Topic: What is the correct way to use ajax  (Read 686 times)

j.vos

  • I’m new here
  • *
  • Posts: 13
  • Karma: 0
  • CiviCRM version: 4.2
  • CMS version: Drupal
  • MySQL version: 5
  • PHP version: 5
What is the correct way to use ajax
March 25, 2013, 12:50:20 am
What is the correct way to use ajax?

I want to load a table automatically under at a page with javascript.

What is the correct way to do this with javascript? Do you need to create a page and call it with javascript (jQuery), or is there another way to do this?

And are there any examples?

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: What is the correct way to use ajax
March 25, 2013, 02:40:41 am
Hi,

Not sure exactly what you want to do, but I would suggest that you fetch the raw data using the api CRM.api(...) and then use a templating engine on the client side to generate your table.

The newest version of civi 4.3 uses Marionette (backbone) to do advanced stuff on the client side, but it's probably too complex for simply displaying a table unless you know or want to learn it (you should, it's great), something like raw html or mustache is enough.

Another option is to generate the page directly from an extension using smarty, and call it with a snippet=1 param, to get only the table and not all the chrome of your theme around. I personally much prefer the first where you just deal with raw data over the wire and do the templating on the client side.

X+
« Last Edit: March 25, 2013, 04:12:09 am by xavier »
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

cray146

  • I post occasionally
  • **
  • Posts: 31
  • Karma: 1
  • CiviCRM version: 4.2
  • CMS version: Drupal 7
  • MySQL version: 5
  • PHP version: 5.3
Re: What is the correct way to use ajax
March 25, 2013, 08:40:43 am
Hi,

Same question here.

I want to write a function that can be called in javascript on the offline membership page (/civicrm/contact/view/membership) when you add or edit a membership. The function should calculate membership price based on the values of certain custom fields associated with the membership type that is selected.

I'm currently looking into the class that implements the offline membership page, CRM_Member_Page_Tab. The offline membership page uses ajax, e.g. in the buildCustomData function to build the form snippet with custom data but it is not entirely clear to me how the dataUrl (/civicrm/contact/view/membership?snippet=4&type=....) is mapped to a particular function call in CiviCRM.

The CRM/Member/Page folder on the other hand does contain a AJAX.php file, implementing the CRM_Member_Page_AJAX class that defines the getMemberTypeDefaults function. The function CRM_Member_Page_AJAX::getMemberTypeDefaults is mapped to the path civicrm/ajax/memType.

I'm thinking of creating my own custom class CRM/MyProject/Page/Ajax() to implement the functions that can be called using AJAX, similar to CRM_Member_Page_AJAX. Would that be the best approach? Any suggestions?

Thanks,

c.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • What is the correct way to use ajax

This forum was archived on 2017-11-26.