Author Topic: Refresh Grid with new data  (Read 8255 times)

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Refresh Grid with new data
« on: September 21, 2019, 02:39:31 am »
I'm trying to refresh data in grid based on search criteria, but don't have correct syntax. Can you provide the correct syntax based on this jsfiddle example? Also, can you point me to any code examples where this type of refresh done?

https://jsfiddle.net/mikepmcg/vz2a6dc7/6/



        function refresh() {
            var data = [
                            { rank: 4, company: 'BP', revenues: '267600.0', profits: '22341.0' },
                            { rank: 5, company: 'General Motors', revenues: '192604.0', profits: '-10567.0' },
                            { rank: 6, company: 'Chevron', revenues: '189481.0', profits: '14099.0' },
            ];

            objPrj.dataModel = {
                data: data,
            };
            gridPrj.refreshDataAndView()

        }
« Last Edit: September 21, 2019, 02:52:01 am by mikep »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Refresh Grid with new data
« Reply #1 on: September 23, 2019, 10:06:31 am »
Correct syntax is

Code: [Select]
  $gridPrj.pqGrid('option', 'dataModel.data', data).pqGrid('refreshDataAndView')

where $gridPrj is jquery reference to the grid.

https://jsfiddle.net/Lct8hnzs/

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Re: Refresh Grid with new data
« Reply #2 on: September 26, 2019, 05:26:26 pm »
Great, thank you. How does that code line change if the colModel has changed as well?

https://jsfiddle.net/wjzte21b/


paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Refresh Grid with new data
« Reply #3 on: September 26, 2019, 05:52:26 pm »
Code: [Select]
$gridPrj.pqGrid('refreshCM', columns).pqGrid('option', 'dataModel.data', data).pqGrid('refreshDataAndView')

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Re: Refresh Grid with new data
« Reply #4 on: October 01, 2019, 06:34:25 am »
Thank you.
To add additional data to the grid, I am appending the current data model with the additional data, and then calling refreshDataAndView  as you show below. How can I reapply the SortModel so my appended data is sorted properly?


paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Refresh Grid with new data
« Reply #5 on: October 01, 2019, 06:40:07 am »
refreshDataAndView is to be used when whole data is changed.

Please use addRow() method to append or insert new rows to existing rows.

https://paramquery.com/pro/api#method-addRow

and call sort() method wihout any parameters to refresh sorting.

https://paramquery.com/pro/api#method-sort

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Re: Refresh Grid with new data
« Reply #6 on: October 02, 2019, 05:23:15 pm »
Thank you.

Can you tell me why this example isn't adding row to grid and sorting? Also, is using data.push(newRow) an acceptable way to add to grid? It worked in this example, but not in my actual project.

https://jsfiddle.net/mikepmcg/4gnjqhma/11/

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Refresh Grid with new data
« Reply #7 on: October 02, 2019, 05:56:56 pm »
yours' parameters of addRow method call and selector of grid is incorrect.

correct one:

Code: [Select]
$("#grid_json").pqGrid("addRow", {newRow: newrow});

         
$("#grid_json").pqGrid("sort", {
          sorter: [{
              dataIndx: 'rank',
              dir: "up"
          }]
});

https://jsfiddle.net/0k2jhw6n/

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Re: Refresh Grid with new data
« Reply #8 on: November 20, 2019, 03:09:38 am »
Thank you.

In your previous example you used $gridPrj, to do the"refreshDataAndView". Many examples I've seen and used don't use the "$" (jquery reference), and some do.

For example, in this link: https://jsfiddle.net/uqrb7oy6/5/
 
$gridPrj = $("#grid_json").pqGrid(objPrj) -- is used to do the "refreshDataAndView"
gridPrj = pq.grid("#grid_json", objPrj) -- is used to do the "getChanges"

-Can the same grid be reference both ways? I need to do different actions with the grid, but can't seem to use both references on the same grid.
-What's the best way to reference the grid when doing this two actions?

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Refresh Grid with new data
« Reply #9 on: November 20, 2019, 12:53:13 pm »
Grid can be referenced in multiple ways, please check Special variables topic in the tutorial section.

https://paramquery.com/pro/tutorial#topic-special-vars

Any way can be used depending upon preference or convenience.

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Re: Refresh Grid with new data
« Reply #10 on: November 20, 2019, 06:30:34 pm »
Thank you that's good news. Can you tell me, then, why don't the two actions (using the two buttons in my example) work at the same time in my example? https://jsfiddle.net/uqrb7oy6/5/
I have to comment out the one grid declaration so that the other grid will work, and vice-versa. Can you update this so both work?
« Last Edit: November 20, 2019, 06:33:39 pm by mikep »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Refresh Grid with new data
« Reply #11 on: November 20, 2019, 08:29:38 pm »
Please check this: https://jsfiddle.net/w3cL1jdf/ for using grid refrerence

and let me know if still have doubts.

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Re: Refresh Grid with new data
« Reply #12 on: November 21, 2019, 06:12:21 pm »
Thank you. Why did you no longer use the jQuery grid object? Can both objects be used? Is there a benefit of one vs the other, or can they both do the exact same functions, just with different syntax.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Refresh Grid with new data
« Reply #13 on: November 21, 2019, 07:50:07 pm »
Both have same end result. "Any way can be used depending upon preference or convenience" as mentioned earlier.

Methods calls via jQuery grid object are usually preferred by jQuery developers.

Direct calls via grid instance are library / framework agnostic and can be used in jQuery, plain js or frameworks like Angular, Reactjs and Vuejs.

Difference between them is mentioned here: https://paramquery.com/pro/tutorial#topic-methods
« Last Edit: November 21, 2019, 07:52:04 pm by paramvir »