Author Topic: load data on button click  (Read 9941 times)

forwheeler

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 26
    • View Profile
load data on button click
« on: January 10, 2014, 04:08:49 am »
I want to load data in the grid on button click.
I am using MVC so on button click I get the data from the controller. I need to also refresh the grid so I can load the data. When I click the button which does a GET to the controller, it just sends the JSON to the browser and doesn't load in the grid.
I tried $("#jsongrid").pqGrid('refresh') but it didn't load the data. I also tried refreshDataAndView.

I can probably use jQuery's load method but I must be missing something simple.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: load data on button click
« Reply #1 on: January 10, 2014, 08:00:40 pm »
Please refer this topic in the tutorial

http://paramquery.com/pro/tutorial#topic-refreshView

refreshDataAndView requests the data from server on its own and  loads the data in the grid when dataModel.location is 'remote'

However when dataModel.location is local, you have to assign data to grid manually even if you fetch the data from remote location.

Assuming dataModel.location to be local in your case and format of response.data to be array of objects :

Code: [Select]
  $.ajax( url, success: function ( response ){
    $grid.pqGrid( 'option', 'dataModel.data', response.data );     
    $grid.pqGrid( 'refreshDataAndView' );     
  }


« Last Edit: January 10, 2014, 08:08:59 pm by paramquery »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: load data on button click
« Reply #2 on: January 11, 2014, 12:53:04 am »
Please also read this topic in tutorial:

http://paramquery.com/pro/tutorial#topic-loaddata

forwheeler

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: load data on button click
« Reply #3 on: January 13, 2014, 07:35:35 pm »
This is the datamodel for the main grid and shows I am using remote location.
Code: [Select]
  dataModel: {
                dataType: "JSON",
                location: "remote",
                recIndx: "ID",
                url: "/PCA/PCAData",
                getData: function (response) {
                    return { data: response };
                }
            },

I want the grid to not load data when the page loads. After I enter a parameter in an input, I want to click a button and have the parameter sent to the server and get the JSON back and load the grid with data.

I assume I need to remove the data model from the grid declaration and call the data model in the button click like below.

Code: [Select]
       
$('#search').click(function()
        {
            $( "#jsongrid" ).pqGrid("option","dataModel.getData",function( response, textStatus, jqXHR ){});
        });

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: load data on button click
« Reply #4 on: January 13, 2014, 10:34:35 pm »
dataModel.location could be kept local intially to prevent remote call on page load
Code: [Select]
dataModel: {
                dataType: "JSON",
                location: "local",
                data: [],
                recIndx: "ID",
                url: "/PCA/PCAData",
                getData: function (response) {
                    return { data: response };
                }
            },


Code for button
Code: [Select]
$('#search').click(function()
{
      $( "#jsongrid" ).pqGrid("option","dataModel.location","remote");
      $( "#jsongrid" ).pqGrid("option","dataModel.postData", { param: $inp.val() } );
      $( "#jsongrid" ).pqGrid( "refreshDataAndView" );
});


forwheeler

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: load data on button click
« Reply #5 on: January 14, 2014, 12:53:55 am »
That works great. Thanks!