使用 Ag-grid 访问状态
Accessing state with Ag-grid
可能不是 Ag-Grid 问题,但我在这个问题上停留了一段时间。
有一个组件像这样反应:
export default class Recrutement extends React.Component {
constructor(props) {
super(props);
this.state = {
candidats: [], // We suppose here that values are filled
values: [] // We suppose here that values are filled
};
this.getContacts();
this.getValues();
}
columnDefs = [{
cellRenderer: function(params) {
return '<span><i class="material-icons"></i></span>';
},
suppressMovable: true,
width: 100,
colId: 1,
//width: (self.columnDefinitions.find(function (v) { return v.colId === 1 }) || {}).size || 50,
pinned: 'left',
suppressFilter: true
},
{
width: 100,
headerName: "Formation",
editable: true,
colId: 7,
suppressMovable: true,
//width : (self.columnDefinitions.find(function (v) { return v.colId === 7 }) || {}).size || null,
autoHeight: true,
cellEditor: 'agSelectCellEditor',
valueGetter: function(params) {
if (params.data.candidat.formationId === null)
return "";
return this.state.values.formations.find(function(val) {
return val.id === params.data.candidat.formationId;
}).name;
},
valueSetter: function(params) {
return selectValueSetter(this.state.values.formations, true, 'formationId', params);
},
cellEditorParams: function() {
return {
values: this.state.values.formations
.sort(function(a, b) {
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
})
.map(function(v) {
return v.name;
})
};
},
filter: "anyOfWordsFilter"
},
];
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
我想要的是 valueGetter 可以访问 this.state
。但是在这里,我每次都得到:
Uncaught TypeError: Cannot read property 'state' of undefined
我尝试了绑定系统(或者也许我做错了,可能是 :D),但其中 none 个箭头正在工作。
在这种情况下如何访问状态?要使用 Ag-grid,我需要我的阵列保持这样(功能可以改变)
替换
valueSetter: function(params) {
return selectValueSetter(this.state.values.formations, true, 'formationId', params);
},
来自
valueSetter: (params) => {
return selectValueSetter(this.state.values.formations, true, 'formationId', params);
},
请准备好this post and
可能不是 Ag-Grid 问题,但我在这个问题上停留了一段时间。
有一个组件像这样反应:
export default class Recrutement extends React.Component {
constructor(props) {
super(props);
this.state = {
candidats: [], // We suppose here that values are filled
values: [] // We suppose here that values are filled
};
this.getContacts();
this.getValues();
}
columnDefs = [{
cellRenderer: function(params) {
return '<span><i class="material-icons"></i></span>';
},
suppressMovable: true,
width: 100,
colId: 1,
//width: (self.columnDefinitions.find(function (v) { return v.colId === 1 }) || {}).size || 50,
pinned: 'left',
suppressFilter: true
},
{
width: 100,
headerName: "Formation",
editable: true,
colId: 7,
suppressMovable: true,
//width : (self.columnDefinitions.find(function (v) { return v.colId === 7 }) || {}).size || null,
autoHeight: true,
cellEditor: 'agSelectCellEditor',
valueGetter: function(params) {
if (params.data.candidat.formationId === null)
return "";
return this.state.values.formations.find(function(val) {
return val.id === params.data.candidat.formationId;
}).name;
},
valueSetter: function(params) {
return selectValueSetter(this.state.values.formations, true, 'formationId', params);
},
cellEditorParams: function() {
return {
values: this.state.values.formations
.sort(function(a, b) {
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
})
.map(function(v) {
return v.name;
})
};
},
filter: "anyOfWordsFilter"
},
];
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
我想要的是 valueGetter 可以访问 this.state
。但是在这里,我每次都得到:
Uncaught TypeError: Cannot read property 'state' of undefined
我尝试了绑定系统(或者也许我做错了,可能是 :D),但其中 none 个箭头正在工作。
在这种情况下如何访问状态?要使用 Ag-grid,我需要我的阵列保持这样(功能可以改变)
替换
valueSetter: function(params) {
return selectValueSetter(this.state.values.formations, true, 'formationId', params);
},
来自
valueSetter: (params) => {
return selectValueSetter(this.state.values.formations, true, 'formationId', params);
},
请准备好this post and