Author Topic: Function in listener  (Read 3416 times)

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 341
    • View Profile
Function in listener
« on: October 14, 2016, 07:27:09 pm »
Hi

Is it possible to call the function in a listener?

Ex:
Code: [Select]
{
                        type: 'button',
                        label: 'test',
                        icon: 'icon',
                        listener: function () {
                            alert('test')
                        }

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6263
    • View Profile
Re: Function in listener
« Reply #1 on: October 15, 2016, 10:24:53 am »
yes!

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 341
    • View Profile
Re: Function in listener
« Reply #2 on: October 17, 2016, 12:44:53 pm »
Great, could you please explain what the syntax would be to call it?  8)
Thanks.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6263
    • View Profile
Re: Function in listener
« Reply #3 on: October 17, 2016, 01:14:31 pm »
Please read the documentation on toolbar ( API and examples ). There are couple of examples:

Syntax for calling any method of grid is:

this.method_name( parameters );

Example:
Code: [Select]
type:'button',
label: 'Toggle filter row',
listener: function(){
this.option('filterModel.header', !this.option('filterModel.header'));
this.refresh();
}

Live example:

http://paramquery.com/pro/demos/filter_header_local

API reference:

http://paramquery.com/pro/api#option-toolbar

queensgambit9

  • Pro Ultimate
  • Sr. Member
  • *
  • Posts: 341
    • View Profile
Re: Function in listener
« Reply #4 on: October 17, 2016, 02:21:22 pm »
Thanks for answer, I'm not sure I understand though.
What I would like to do is to reuse the function code I have in the listener somwhere else on the page by calling the function, is it possible even though it doesn't have a name?



paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6263
    • View Profile
Re: Function in listener
« Reply #5 on: October 17, 2016, 04:40:19 pm »
In that case it has to be named function.

Code: [Select]
listener: toggleRow

Code: [Select]
         function toggleRow(){
this.option('filterModel.header', !this.option('filterModel.header'));
this.refresh();
};

Syntax to call that function externally. Note that it requires to set the context of the function:
Code: [Select]
  toggleRow.call( $grid.pqGrid('instance') );

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6263
    • View Profile
Re: Function in listener
« Reply #6 on: November 17, 2016, 07:42:02 pm »