Please forgive my ignorance. I am just getting into using react and I am sure I am missing something really simple here. So depending on the amount of data I get back from loadData I need to display the data in 1 2 or 3 columns. I am using ul and li. I calculate based on screen size how many can fit in a column. I tried having them all in one ul but I need a header row at the top, so I really need up to 3 uls with the first li having the header info. How can I loop and create the number of uls that I need? I tried putting a loop around the return but I can't do that. Any help is greatly appreciated.
var appointmentBoardSection = React.createClass({
displayName: "appointmentBoardSection",
getInitialState: function() {
return {
key: null,
results: [],
parameters: this.props.parameters,
type: "",
};
},
shouldComponentUpdate: function(nextProps, nextState) {
console.info("this.state.key: " + this.state.key);
console.info("nextState.key: " + nextState.key);
if (this.state.key !== nextState.key) {
console.info("NO MATCH!!!!");
//this.forceUpdate();
return true;
}
return false;
},
componentDidMount: function() {
console.info("MOUNTED!!!");
var _this = this;
_this.loadData();
setInterval(function() {
_this.loadData();
}, 10000);
},
loadData: function() {
console.info("LOADING DATA....");
var _this = this;
$.when(_api.callPromise('apiRouter.ashx', 'getAppointmentsBoard', JSON.stringify(masterPage.reporting.parameters), this.props.type)).then(function(data) {
var totalData = data.args[0];
if (totalData.length <= 0) {
$("div#" + _this.props.type).hide();
} else {
$("#no_data").hide();
$("div#" + _this.props.type).show();
}
_this.setState({
results: totalData,
key: Math.random()
});
masterPage.hidePleaseWait(true);
});
},
render: function() {
var contHeight = $(window).height();
var offsetTop = $("div.appt_bd_container").offset().top;
var realEstate = contHeight - 400;
var perColumn = Math.round(realEstate / 70);
console.info("height: " + $(window).height());
console.info("offsetTop: " + offsetTop);
console.info("perColumn: " + perColumn);
var maxICanShow = perColumn * 3;
var limitResults = false;
var colCount = 3;
var colDef = "";
var colWidth = 100;
var numberOfColsNeeded = Math.round(this.state.results.length / perColumn);
if (numberOfColsNeeded > 3) {
colCount = 3;
} else {
colCount = numberOfColsNeeded;
}
switch (colCount) {
case 2:
colWidth = 50;
break;
case 3:
colWidth = 33.333;
break;
default:
colDef = "";
colWidth = 100;
}
if (this.state.results.length > maxICanShow) {
var i = 1;
console.info("this.state.results.length: " + this.state.results.length);
console.info("maxICanShow: " + maxICanShow);
while (i < this.state.results.length - (maxICanShow - 1)) {
this.state.results.pop();
i++;
}
}
var curColData = [];
var overallCount = this.state.results.length;
var testArr = [];
for (var i = 0; i < colCount; i++) {
testArr.push({});
}
if (this.state.results.length > perColumn) {
for (var i = 0; i < perColumn; i++) {
if (overallCount > 0) {
curColData.push(this.state.results.shift());
overallCount--;
}
}
}
var myTest = [];
return ( <
ul className = "appt_bd" >
<
li className = "appt_bd_header"
style = {
{
width: colWidth + '%'
}
} >
<
span className = "appt_bd_header_main" > {
this.props.title
} < /span> <
span className = "appt_bd_header_sub" > Appts Set < /span> <
span className = "appt_bd_header_sub" > Appts Show < /span> <
/li> {
curColData.map(function(row) {
return <AppointmentBoardRow colWidth = {
colWidth
}
key = {
row.userId
}
row = {
row
}
/>;
})
} <
/ul>
);
}
});
var AppointmentBoardRow = React.createClass({
render: function() {
var row = this.props.row;
return ( <
li className = "appt_bd_row"
style = {
{
width: this.props.colWidth + '%'
}
}
key = {
row.userId
} >
<
span className = "appt_bd_desc" >
<
span className = "appt_bd_rank" > {
row.rank
} < /span> <
span className = "appt_bd_avatar_container" > < div className = {
row.className
} > < span className = "initials" > {
row.initials
} < /span></div > < /span> <
span className = "appt_bd_user" >
<
span className = "appt_bd_description_main" > {
row.userName
} < /span> <
span className = "appt_bd_description_sub" > {
row.role
} < /span> <
/span> <
/span> <
span className = "appt_bd_data" >
<
span className = "appt_bd_data_main" > {
row.apptsSetCount
} < /span> <
span className = "appt_bd_data_sub" > /{row.apptsSetGoal}</span >
<
/span> <
span className = "appt_bd_data" >
<
span className = "appt_bd_data_main" > {
row.apptsShowCount
} < /span> <
span className = "appt_bd_data_sub" > /{row.apptsShowGoal}</span >
<
/span> <
/li>
);
}
});
****EDITED in response to Josh...one issue I am having now is nothing is displaying though I can verify uls is correct. Here is my nee render:
render: function () {
var contHeight = $(window).height();
var offsetTop = $("div.appt_bd_container").offset().top;
var realEstate = contHeight - 400;
var perColumn = Math.round(realEstate / 70);
var maxICanShow = perColumn * 3;
var colWidth = 100;
var uls = _.chunk(_.take(this.state.results, maxICanShow), perColumn);
let li = "";
return (
<div>
{uls.map((lis) => {
<ul className="appt_bd">
<li className="appt_bd_header" style={{ width: colWidth + '%' }}>
<span className="appt_bd_header_main">{this.props.title}</span>
<span className="appt_bd_header_sub">Appts Set</span>
<span className="appt_bd_header_sub">Appts Show</span>
</li>
lis.map(li => {<li className="appt_bd_row" style={{ width: this.props.colWidth + '%' }} key={li.userId}>
<span className="appt_bd_desc">
<span className="appt_bd_rank">{li.rank}</span>
<span className="appt_bd_avatar_container"><div className={li.className}><span className="initials">{li.initials}</span></div></span>
<span className="appt_bd_user">
<span className="appt_bd_description_main">{li.userName}</span>
<span className="appt_bd_description_sub">{li.role}</span>
</span>
</span>
<span className="appt_bd_data">
<span className="appt_bd_data_main">{li.apptsSetCount}</span>
<span className="appt_bd_data_sub">/{li.apptsSetGoal}</span>
</span>
<span className="appt_bd_data">
<span className="appt_bd_data_main">{li.apptsShowCount}</span>
<span className="appt_bd_data_sub">/{li.apptsShowGoal}</span>
</span>
</li>})
</ul>
})}
</div>
);
}
columnsproperty?