Author Topic: How to handle events in Grid+Sub Grid?  (Read 5127 times)

nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
How to handle events in Grid+Sub Grid?
« on: March 06, 2014, 08:05:58 pm »
I have two grids in selector #tabela:

  • $objOrcamento is the main grid
    • $grid is the details grid

    Now I want to handle events like cellBeforeSave, load, etc for both of them but I keep receiving the error:
    Code: [Select]
    Uncaught TypeError: Object #<Object> has no method 'pqGrid'
    For example, to handle cellBeforeSave in $grid, I tried:

    Code: [Select]
    var $grid={
       //colModel, dataModel, etc...

      cellBeforeSave: function (evt, ui) {
                var isValid = $grid.pqGrid("isValid", ui);
                if (!isValid.valid) {
                    evt.preventDefault();
                    return false;
                }

       },
    }

    This isn't working, so I've tried outside var $grid:

    Code: [Select]
        $("#tabela").on("pqgridcellbeforesave", function (event, ui) {
            var isValid = $grid.pqGrid("isValid", ui);
            if (!isValid.valid) {
                evt.preventDefault();
                return false;
            }
        });

    But this is generating the same error.
    What is the correct way to handle events in multiple grids?

    Thanks in advance for your help!

    paramvir

    • Administrator
    • Hero Member
    • *****
    • Posts: 6314
      • View Profile
    Re: How to handle events in Grid+Sub Grid?
    « Reply #1 on: March 06, 2014, 08:48:40 pm »
    When you have multiple grids, especially nested ones, you can't rely upon global instance variable i.e $grid

    you can get instance of the grid which generated the event from within the event itself by using $(this)

    Code: [Select]
    cellBeforeSave: function (evt, ui) {
                var isValid = $(this).pqGrid("isValid", ui);
                if (!isValid.valid) {
                    evt.preventDefault();
                    return false;
                }

       },



    « Last Edit: March 06, 2014, 08:55:42 pm by paramquery »

    nuno.nogueira

    • Pro Economy
    • Jr. Member
    • *
    • Posts: 95
      • View Profile
    Re: How to handle events in Grid+Sub Grid?
    « Reply #2 on: March 06, 2014, 08:55:41 pm »
    Hi,

    Thanks a lot, I was thinking (this) would help :-D

    So this means that I will have to repeat the same code for the two grids. I thought i could have one event handler to validate the two grids simultaneously.

    paramvir

    • Administrator
    • Hero Member
    • *****
    • Posts: 6314
      • View Profile
    Re: How to handle events in Grid+Sub Grid?
    « Reply #3 on: March 06, 2014, 08:58:41 pm »
    You can use the same event handler for multiple grids as long as the logic in handler is same.

    nuno.nogueira

    • Pro Economy
    • Jr. Member
    • *
    • Posts: 95
      • View Profile
    Re: How to handle events in Grid+Sub Grid?
    « Reply #4 on: March 06, 2014, 10:32:04 pm »
    Exactly. The problem is how to put that into practice.

    Say you want to delete a row of the subgrid ($grid) which is initialized in $('#tabela'). You can call function acceptChanges, like:

    Code: [Select]
    function acceptChanges() {
            //more code goes here
           
               var changes = $grid.pqGrid("getChanges", {
                    format: "byVal"
                }),
    }

    It doesn't help to use $(this), $grid or $objOrcamento.

    You have to identify which grid is calling acceptChanges, but how?
    « Last Edit: March 06, 2014, 11:36:31 pm by nuno.nogueira »

    paramvir

    • Administrator
    • Hero Member
    • *****
    • Posts: 6314
      • View Profile
    Re: How to handle events in Grid+Sub Grid?
    « Reply #5 on: March 07, 2014, 02:21:37 pm »
    Good question!

    You have to pass the reference of the grid obtained through $(this) in an event handler to acceptChanges ( $grid )

    For example in this demo
    http://paramquery.com/pro/demos/editing_batch

    acceptChanges is called from an button in a toolbar (line 108). here you can obtain reference to the grid using

    Code: [Select]
      //this points to button instance in the toolbar
      //so we get the parent pqGrid instance.
      var $grid = $(this).closest( ".pq-grid" );
      acceptChanges($grid);


    nuno.nogueira

    • Pro Economy
    • Jr. Member
    • *
    • Posts: 95
      • View Profile
    Re: How to handle events in Grid+Sub Grid?
    « Reply #6 on: March 07, 2014, 02:53:53 pm »
    I found a similar solution: get the instance of the grid from within the event handler and pass it as an argument to the acceptChanges function. It seems to be working OK.