Table rows as react components aren't getting rendered - html

I'm trying to return multiple table rows from PlayerTableRow inside PlayerTable, but I can't get it to work. Console shows no errors. Only the table headers get rendered. If I substitute all in PLayerTableRow with static content, it still doesn't render. I guess there is something wrong with the use of childreen, but I can't figure it out.
function App() {
const PlayerRoster = new Map([
"1",
{
position: "None",
name: " ",
skill1: " ",
skill2: " ",
},
],
[
"2",
{
position: "None",
name: "",
skill1: "",
skill2: "",
},
]);
return (
<div className="">
<PlayerTable>
{playerRoster.forEach((player, key) => {
<PlayerTableRow />;
})}
</PlayerTable>
</div>
);
}
function PlayerTable({ children }) {
return (
<table>
<thead>
<tr>
<th>No.1</th>
<th>Player Name</th>
<th>Position</th>
<th>MA</th>
<th>ST</th>
<th>AG</th>
<th>PA</th>
<th>AV</th>
<th>Skills</th>
<th>Skill1</th>
<th>Skill2</th>
<th>Price</th>
</tr>
</thead>
<tbody>{children}</tbody>
</table>
);
}
function PlayerTableRow( ) {
return (
<>
<tr>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
</tr>
</>
);
}

Related

jQuery UI sortable dynamic target not working

I have two tables in my html. The first is loaded on page load, the second is generated on button click.
My sortable function does not apply to the generated table.
Can you explain why?
Here is my Javascript some.js:
// sort all kinds of tables
$(function(){
$('table').sortable();
});
// generate new table
$(document).on('click','#test',function(){
let str = "<table>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
</table>"
let content = $.parseHTML(str)
$('#main').html(content);
})
And here the actual html file index.html:
<!DOCTYPE html>
<html>
<head>
<script defer src="jquery/3.5.1/jquery-3.5.1.min.js"></script>
<script defer src="jquery/jquery-ui-1.13.1.js"></script>
<script defer src="some.js"></script>
</head>
<body>
<div id="main">
<table>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
</table>
<input type="button" id="test" value="Generate Table" onclick="">
</div>
</body>
</html>
The generated table does not exist in the DOM in the ready event when you call $('table').sortable(); The table is added to the DOM later as response to the click event. You should call $('table').sortable(); after you add the table to the dom
This should work:
$(document).on('click','#test',function(){
let str = "<table>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
</table>"
let content = $.parseHTML(str)
$('#main').html(content);
$('#main table').sortable();
})

how to detect multiple clicks on a cell of same row and prevent it in jquery?

I have a table
<table>
<tr>
<th>Heading 1
</th>
<th>Heading 2
</th>
<th>Heading 3
</th>
<th>Heading 4
</th>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
</tr>
</table>
When I click a particular cell I get its cell value.
$('td').click(function () {
var col_index = $(this).text();
console.log("Text: " + col_index);
});
What I want is, when I click the different cell of same row I want to alert a message that 'You cannot click another value once clicked' or something like that.
For example, if you look in to the table, for the first time I click Value 1, but then if I go click Value 2 it should prevent me to do click.
How do I do that?
You could store a map of clicked cells in a specific row and if something has been already clicked then display an alert:
var td_map = {};
$('td').click(function () {
var $this = $(this);
var $td = $this.parent();
var col_index = $(this).text();
if (td_map[$td.index()] === undefined || td_map[$td.index()] === col_index) {
td_map[$td.index()] = col_index;
console.log("Text: " + col_index);
} else {
alert('You cannot select two cells in one row');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<th>Heading 1
</th>
<th>Heading 2
</th>
<th>Heading 3
</th>
<th>Heading 4
</th>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
</tr>
</table>

Prints in console, but not in on page?

Can anyone spot the issue here?
import React from 'react'
export default function CoinTable(props){
props.data.map(row=> {
console.log(row.first_name)
})
return (
<table>
<thead>
<tr>
<th>Name</th>
<th>Phone</th>
<th>Email</th>
<th>Subscription</th>
</tr>
</thead>
<tbody>
{
props.data.map(row => {
<tr>
<td>{row.first_name} {row.last_name}</td>
<td>{row.phone}</td>
<td>{row.email}</td>
</tr>
})
}
</tbody>
</table>
)
}
For some reason the code appears on console but not on the screen. I am also printing from a JSON file if that helps.
You don't return the DOM in map.
props.data.map(row => {
return (
<tr>
<td>{row.first_name} {row.last_name}</td>
<td>{row.phone}</td>
<td>{row.email}</td>
</tr>
)
})
Or precisely:
props.data.map(row => (
<tr>
<td>{row.first_name} {row.last_name}</td>
<td>{row.phone}</td>
<td>{row.email}</td>
</tr>
))

Displaying AMCharts inside an HTML table cell

I am trying to display AMCharts inside an HTML table cell. I'm looping through data and creating a chart for each data item. Here is my HTML code:
<table id="theTable" class="display table table-bordered">
<thead>
<tr>
<th>Department</th>
<th>Number of Employees</th>
<th>Number of Volunteers</th>
<th>Chart</th>
</tr>
</thead>
<tbody>
<tr>
<td>IT</td>
<td>30</td>
<td>5</td>
<td><div id="chart_1"></div></td>
</tr>
<tr>
<td>HR</td>
<td>35</td>
<td>14</td>
<td><div id="chart_2"></div></td>
</tr>
<tr>
<td>LG</td>
<td>100</td>
<td>65</td>
<td><div id="chart_3"></div></td>
</tr>
</tbody>
</table>
And here is my Javascript code:
var emp = [30, 35, 100];
var vol = [5, 14, 65];
for (var j = 0; j < 3; j++) {
var str = "chart_" + j.toString();
var chart = AmCharts.makeChart(str, {
"type": "pie",
"dataProvider": [{
"type": "Employees",
"per": parseFloat(emp[j]),
"color": "#54e83a"
}, {
"type": "Volunteers",
"per": parseFloat(vol[j]),
"color": "#41ba2c"
}],
"valueField": "per",
"titleField": "type",
"colorField": "color",
"balloon": {
"fixedPosition": true
},
"export": {
"enabled": true
},
"startDuration": 0
});
}
What am I doing wrong? Any help is appreciated! Here is a JSFiddle
(Please ignore this text the Editor is saying my post is mostly code so I'm adding non-code text hoping to trick it...This is unprofessional I know.)
AmChart divs must have dimensions specified through CSS, otherwise they won't display. Set the sizes and you're all set, for example:
#chart_1, #chart_2, #chart_3 {
width: 300px;
height: 100px;
}
You also need to adjust your loop logic as you're starting from chart_0, when your divs are starting from chart_1. Either update your html or fix your string creation, e.g. var str = "chart_" + (j + 1); (note you don't need toString())
Updated fiddle

AngularJS nested ng-repat in ng-repeat (key, value) pair data structure in HTML table element with sort filter

I want to print table rows from the fallowing data structure.
$scope.myData = {}
myData['Item1']['Period1']= {"Value":1}
myData['Item1']['Period2']= {"Value":2}
myData['Item1']['Period3']= {"Value":3}
myData['Item1']['Period4']= {"Value":4}
myData['Item2']['Period1']= {"Value":11}
myData['Item2']['Period2']= {"Value":12}
myData['Item3']['Period3']= {"Value":13}
myData['Item4']['Period4']= {"Value":14}
I want to print it somehow like this and sort by Period name DESC:
<tbody >
<tr ng-repeat="(key, value) in myData">
<td>
{{ key }}
</td>
<td ng-repeat="PeriodItem in value | <!-- here the filtering -->">
{{ PeriodItem.Value }}
</td>
</tr>
</tbody>
The key is printed out, but the PeriodItem.Value is not. What is the proper way to do that, because i tried a couple of things and none of them seem to work.
Example of desired output:
<tbody>
<tr>
<td>Item1</td>
<td>4</td>
<td>3</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>Item2</td>
<td>14</td>
<td>13</td>
<td>12</td>
<td>11</td>
</tr>
</tbody>
try this fiddle
<tbody >
<tr ng-repeat="(key, value) in myData">
<td>
{{ key }}
</td>
<td ng-repeat="(key, value) in value">
{{ value.Value }}
</td>
</tr>
</tbody>
when using ng-repeat, "(key, value) in blah" syntax is used to iterate hashes.
"value in blah" is used to iterate arrays
You've defined your data as
myData['Item1']['Period1']= {"Value":1}
myData['Item1']['Period2']= {"Value":2}
...
which is a bit confusing. If you restructure it, it should be clear whether the second dimension is an array or hash.
$scope.myData = {
Item1: {
Period1: {Value: 1},
Period2: {Value: 2},
Period3: {Value: 3},
Period4: {Value: 4},
},
Item2: {
Period1: {Value: 11},
Period2: {Value: 22},
...