I have a local JSON file containing JSON objects and I want it so that if an obj contains a specific string, then that object is rendered to a table. If not, then it's not rendered. For the time being I'm getting one error in DevTools: Uncaught TypeError: Cannot read property 'DT_RowId' of undefined
and I'm working with DataTables---while it is useful, using functions with it has been a headache.
function loadPH() {
let admissText = admissData.d.results.map(function(val) {
if (val.p_h_v !== "") { // If this is not empty, then return
return {
"PHV": val.p_h_v,
"Part C": val.partc
}
}
})
$('#prohac-table').DataTable({
columns: [
{ data: "PHV" },
{ data: "Part C" },
... // ---------- the rest contains irrelevant data
{
"d": {
"results": [
{
...
"p_h_v": "" // ------------ this doesn't meet conditions, isn't rendered
...
},
{
"p_h_v": "Yes" // ---------- meets conditions---this obj rendered
...
Converted your example to a stack-snippet
and all it took was a filter
, the values that map wasn't returning were undefined
objects in your dataset and were causing the issue.
var admissData = {
"d": {
"results": [{
"p_h_v": "Maybe", // ---------- meets conditions---this obj rendered
"partc": "show this too"
},
{
"p_h_v": "", // ------------ this doesn't meet conditions, isn't rendered
"partc": "test - no show"
},
{
"p_h_v": "Yes", // ---------- meets conditions---this obj rendered
"partc": "test - show"
}
]
}
};
function loadProHac() {
let admissText = admissData.d.results
.filter(x => x.p_h_v !== "") //added your filter here.
.map(function(val) {
return {
"PHV": val.p_h_v,
"Part C": val.partc
}
});
$('#prohac-table').DataTable({
data: admissText,
columns: [{
data: "PHV"
},
{
data: "Part C"
}
]
});
}
loadProHac();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<table id='prohac-table'></table>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments