Author Topic: Default value in colModel  (Read 2483 times)

hyh888

  • Pro Enterprise
  • Full Member
  • *
  • Posts: 142
    • View Profile
Default value in colModel
« on: May 12, 2022, 03:56:13 am »
Now it seems that default value couldn't be set in colModel for new row of every column. Can it be added?
Then developer may get better experience in programing like following:
var colModel= [
         {title: "ID", dataIndx: "id", width: 200,dataType: "string",  editable: false },
         {title: "Country", dataIndx: "Country",width: 260, dataType: "string",default:"USA"},
         {title: "City", dataIndx: "City",width: 260, dataType: "string",default:"New York"}
                     ]
« Last Edit: May 12, 2022, 03:58:59 am by hyh888 »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Default value in colModel
« Reply #1 on: May 12, 2022, 10:58:37 am »
Ok noted.

hyh888

  • Pro Enterprise
  • Full Member
  • *
  • Posts: 142
    • View Profile
Re: Default value in colModel
« Reply #2 on: June 21, 2022, 01:16:32 pm »
Dear Daramvir:
If this function has been provided in new version?

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Default value in colModel
« Reply #3 on: June 24, 2022, 04:32:04 am »
Your requirement can be met by adding this code in the grid initialization object.

Code: [Select]
rowTemplate: {},
complete: function(){
var rt = this.option('rowTemplate'), cm = this.getColModel();
//debugger;
cm.forEach(function(col){
var def = col.defaultValue, di = col.dataIndx;
if( def !== undefined ){
Object.defineProperty(rt, di, {
enumerable: true,
get(){
var val = this[ "_" + di];
return val == null? def: val;;
},
set(val){
this[ "_" + di ] = val;
}
});
}
});
},

and use defaultValue instead if default since default is a reserved word.
« Last Edit: June 24, 2022, 04:40:40 am by paramvir »

hyh888

  • Pro Enterprise
  • Full Member
  • *
  • Posts: 142
    • View Profile
Re: Default value in colModel
« Reply #4 on: June 28, 2022, 08:51:56 am »
Dear Daramvir:
This code is too complicate, it will be better if pqgrid can add default value property in colModel.

var colModel= [
         {title: "ID", dataIndx: "id", width: 200,dataType: "string",  editable: false },
         {title: "Country", dataIndx: "Country",width: 260, dataType: "string",defaultValue:"USA"},
         {title: "City", dataIndx: "City",width: 260, dataType: "string",defaultValue:"New York"}
                     ]

Long and complicate java-like code really frustrates me, json-like concise code can really save coding time and reduce debug time.
« Last Edit: June 28, 2022, 10:16:27 am by hyh888 »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Default value in colModel
« Reply #5 on: June 28, 2022, 12:30:14 pm »
you don't need to work with the code. That code purpose is to add support for defaultValue property in the columns.

hyh888

  • Pro Enterprise
  • Full Member
  • *
  • Posts: 142
    • View Profile
Re: Default value in colModel
« Reply #6 on: June 29, 2022, 08:14:51 am »
Million thanks!

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: Default value in colModel
« Reply #7 on: November 21, 2022, 11:22:05 am »
Add configurable: true to avoid getting error ( can't redefine property )

Updated code:

Code: [Select]
rowTemplate: {},
complete: function(){
var rt = this.option('rowTemplate'), cm = this.getColModel();
//debugger;
cm.forEach(function(col){
var def = col.defaultValue, di = col.dataIndx;
if( def !== undefined ){
Object.defineProperty(rt, di, {
enumerable: true,
                                configurable: true,
get(){
var val = this[ "_" + di];
return val == null? def: val;;
},
set(val){
this[ "_" + di ] = val;
}
});
}
});
},