Author Topic: Call a function in a second grid  (Read 4341 times)

Robert

  • Pro Economy
  • Newbie
  • *
  • Posts: 26
    • View Profile
Call a function in a second grid
« on: April 23, 2014, 01:45:12 am »
I have 2 grids in separate <div>'s but in one php page.

Both grids are working perfectly HOWEVER, I would like to call a function in one of the grids from the other one (and pass in a var). Because they are wrapped in self executing functions, I cannot access the internal function of the opposite grid.

Is it possible to call an internal function of one grid from another grid?

Thanks in advance!

Robert

  • Pro Economy
  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Call a function in a second grid
« Reply #1 on: April 23, 2014, 02:18:07 am »
After miles of searching before asking the question here ... I didn't realize how close I was to the answer. My apologies. I found an example that allows this to happen and it works beautifully.

 (function($) {

     var namespace;
         namespace = {
                     something : function() {
                                             alert('hello there!');
                                            },
                      bodyInfo : function() {
                                             alert($('body').attr('id'));
                                            }
                     };
         window.ns = namespace;
    })(this.jQuery);

   $(function() {
              ns.something();
              ns.bodyInfo();
   });

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6263
    • View Profile
Re: Call a function in a second grid
« Reply #2 on: April 23, 2014, 01:50:38 pm »
Common solution to this is:

If you want to call API of a grid for which you don't have its instance variable, you can always get reference to it using the selector.

$(function(){
  //grid A constructed in this function.
  var obj = {....};
  var $gridA = $("#A").pqGrid( obj );
});

$(function(){
  var $gridA = $("#A");
  //call methods
  $gridA.pqGrid( 'some method' );
});

Robert

  • Pro Economy
  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Call a function in a second grid
« Reply #3 on: April 25, 2014, 03:49:49 am »
Fantastic suggestion! THANK YOU!!! I will work with this and see if it solves what appears to be a scope issue I am having. Thank you again.

Robert

  • Pro Economy
  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Call a function in a second grid
« Reply #4 on: April 25, 2014, 06:04:06 am »
This seems so simple but I am having issues getting this to work. Is there and example I can see where this is implemented?

Robert

  • Pro Economy
  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Call a function in a second grid
« Reply #5 on: April 25, 2014, 06:28:51 am »
OH MY ... I got it to work!!! You are a life saver!!!