Author Topic: Is that possible to add/remove column for grid  (Read 5766 times)

noctrona

  • Pro Enterprise
  • Jr. Member
  • *
  • Posts: 82
    • View Profile
Is that possible to add/remove column for grid
« on: February 13, 2014, 01:25:03 pm »
Hi,

I know we can add/remove row dynamically. But I have a requirement to add/remove column in grid,  so I want to know whether we can do it?


Thank you!

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6265
    • View Profile
Re: Is that possible to add/remove column for grid
« Reply #1 on: February 13, 2014, 06:47:03 pm »
Add a new column object in colModel using push (if at end) or splice (if in middle)

colModel.push( { title: "yyy", dataIndx: "xxx", width : n } );

and call

Code: [Select]
$grid.pqGrid( "refreshDataFromDataModel" );
 $grid.pqGrid ( "refresh" );
« Last Edit: February 13, 2014, 11:55:37 pm by paramquery »

noctrona

  • Pro Enterprise
  • Jr. Member
  • *
  • Posts: 82
    • View Profile
Re: Is that possible to add/remove column for grid
« Reply #2 on: February 14, 2014, 08:41:58 am »
Add a new column object in colModel using push (if at end) or splice (if in middle)

colModel.push( { title: "yyy", dataIndx: "xxx", width : n } );

and call

Code: [Select]
$grid.pqGrid( "refreshDataFromDataModel" );
 $grid.pqGrid ( "refresh" );

Hi,

Thanks for your response.
I try it on my page. But still have problem.
As I understanding, I need to get the all colModel array then I can push new column in it. Is that right?

I use the following code:
var colModel = $( ".selector" ).pqGrid( "option", "colModel" );
var title=colModel[0].title;

But when I get the "colModel" and try to get some attributes about this object, I will see the following error:
Cannot read property 'title' of undefined.

I'm sure I import "pqgrid.min.js" on my page.
So do you know why this happened? Do I make any mistake for these codes?


Thank you!

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6265
    • View Profile
Re: Is that possible to add/remove column for grid
« Reply #3 on: February 14, 2014, 09:04:51 am »
From the error it seems that your colModel is empty; you haven't added any column in it yet.

Just check the length of colModel before trying to access colModel[0]

http://jsfiddle.net/LAgZx/217/

noctrona

  • Pro Enterprise
  • Jr. Member
  • *
  • Posts: 82
    • View Profile
Re: Is that possible to add/remove column for grid
« Reply #4 on: February 14, 2014, 10:21:23 am »
From the error it seems that your colModel is empty; you haven't added any column in it yet.

Just check the length of colModel before trying to access colModel[0]

http://jsfiddle.net/LAgZx/217/

You are right. I check my code and found a stupid mistake. I didn't get colModel about my target, that's why colModel is empty.

Thank you!