ParamQuery grid support forum
General Category => Suggest new features => Topic started by: hyh888 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"}
]
-
Ok noted.
-
Dear Daramvir:
If this function has been provided in new version?
-
Your requirement can be met by adding this code in the grid initialization object.
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.
-
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.
-
you don't need to work with the code. That code purpose is to add support for defaultValue property in the columns.
-
Million thanks!
-
Add configurable: true to avoid getting error ( can't redefine property )
Updated code:
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;
}
});
}
});
},