I’m using react to render the gantt. I’m also using redux to pass new data to the gantt.
If I set my initial data to be
this.props.data = [];
it renders fine. But then the user uses a filter to get data from the server. After I get this data, how can I update the gantt with the new data?
Here is my component
class Home extends Component {
constructor() {
super();
this.state = {
data: [],
links: []
};
}
render() {
console.log("props: ");
console.log(this.props);
return (
<div className="fill">
<Gantt data={this.props.data} links={this.state.links} />
</div>
);
}
}
Home.PropTypes = {
data: PropTypes.IsRequired
};
function mapStateToProps(state) {
return {
data: state.gantt.ganttData
};
}
export default connect(mapStateToProps)(Home);
Here is my gantt component
export default class GanttComponent extends Component {
constructor() {
super();
//"Gantt" in getting an instance goes with the capital letter
this.gcService = null;
this.gChart = Gantt.getGanttInstance();
this.plService = new PortfolioService(this.gChart);
}
componentDidMount() {
this.gcService = new GanttChart(this.gChart);
this.gcService.initGantt(this.ganttContainer, this.props.data, this.props.links);
}
componentDidUpdate() {
//let d = this.gcService.toGanttData(this.props.data);
//this.gcService.initGantt(this.ganttContainer, d, this.props.links);
this.gChart.render();
}
render() {
return (
<div className="fill">
<GanttToolBar
createPortfolio={this.plService.create.bind(this)} />
<div className="fill" ref={(input) => { this.ganttContainer = input; }} />
<PortfolioLineUpdateModal />
<GanttFilter />
</div>);
}
}
here is the gantt service doing all the work
class GanttChart {
constructor(ganttChart) {
this.gantt = ganttChart;
this.lightBox = new LightBox(ganttChart);
}
initGantt(ganttContainer, _data, _links) {
this.initCustomTaskTypes();
this.initColumns();
this.gantt.init(ganttContainer);
this.gantt.parse({
data: _data,
links: _links
});
}
initCustomTaskTypes() {
this.gantt.config.types.portfolio = "portfolio";
this.gantt.config.types.program = "program";
}
initColumns() {
var nameEditor = { type: "text", map_to: "text" };
var sdateEditor = {
type: "date", map_to: "start_date", min: new Date(2000, 0, 1),
max: new Date(2050, 0, 1)
};
var edateEditor = {
type: "date", map_to: "end_date", min: new Date(2000, 0, 1),
max: new Date(2050, 0, 1)
};
var durationEditor = { type: "number", map_to: "duration", min: 0, max: 100 };
this.gantt.config.columns = [
{ resize: true, name: "text", id: "text", label: "Name", tree: true, width: "350", editor: nameEditor },
{ resize: true, name: "start_date", id: "start_date", label: "Start", align: "center", width: "100", editor: sdateEditor },
{ resize: true, name: "end_date", id: "end_date", label: "End", align: "center", width: "100", editor: edateEditor},
{ resize: true, name: "duration", id: "duration", label: "Duration", align: "center", width: "100", editor: durationEditor }
];
this.gantt.config.grid_resize = true;
}
toGanttData(portfolios) {
for (let i = 0; i < portfolios.length; i++) {
portfolios[i].id = i;
portfolios[i].type = this.gantt.config.types.portfolio;
portfolios[i].unscheduled = true;
portfolios[i].text = portfolios[i].name;
}
return portfolios;
}
}
/*
{
id: ganttId,
type: gantt.config.types.productline,
productLineId: data.productLineId,
sortOrder: data.sortOrder,
text: data.name,
unscheduled: true
};
*/
module.exports = GanttChart;