I created a table dynamically, being populated from database. What want to do is to created a fixed header and a scrollable body. I tried css and javascript but didn't manage to do anything, I think because table is created automatically.
Help.
Here is my code:
private void GenerateTable();
{
DataTable dt = CreateDataTable();//in CreateDataTable I just take all my reocrds from database
Table table = new Table();
TableRow row = null;
row = new TableRow();
for (int j = 0; j < dt.Columns.Count; j++)
{
TableHeaderCell headerCell = new TableHeaderCell();
headerCell.Text = dt.Columns[j].ColumnName;
row.Cells.Add(headerCell);
}
table.Rows.Add(row);
//Add the Column values
for (int i = 0; i < dt.Rows.Count; i++)
{
row = new TableRow();
for (int j = 0; j < dt.Columns.Count; j++)
{
TableCell cell = new TableCell();
cell.Text = dt.Rows[i][j].ToString();
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
tablediv.Controls.Add(table);
}
Div where I add the table:
<div class="mainEntry" style="width:90%" id="tablediv" runat="server" />
And my CSS for table:
table {
border-collapse: collapse;
width: 100%;
color: white;
}
th, td {
text-align: left;
padding: 8px;
}
th{
font-size:20px;
}
tbody{
height:500px;
overflow:auto;
display:block;
}
tr:nth-child(even){background-color: rgba(255, 255, 255, .15)}
th {
background-color: rgba(174, 183, 212, .15);
color: white;
}
I hope this css classes demo can help you. Visit fiddlehttp://jsfiddle.net/TweNm/
Related
Im trying to apply some conditional formatting on a html script. I currently have it 4 different tables, with the first 2 stacked on top of each other, and the bottom two side by side at the bottom. I have a color conditional formatting to where for the first two tables, it is only applied to the last 2 columns. For the last table, I have it applied for all but the first column. I am trying to apply the conditional formatting on the last table (all but first column), but it keeps applying the formatting of the first 2 columns.
<html>
<head>
<script>
setTimeout(function(){
location.reload();
}, 10000);
function changeColor() {
var tables = document.querySelectorAll("table");
for (var i = 0; i < tables.length - 2; i++) {
var rows = tables[i].querySelectorAll("tr");
for (var j = 1; j < rows.length; j++) { // start from the second row (index 1)
var cells = rows[j].querySelectorAll("td:nth-child(n+5)");
for (var k = 0; k < cells.length; k++) {
if (cells[k].innerHTML !== "") {
if (cells[k].innerHTML > 0) {
cells[k].style.backgroundColor = "rgba(144, 238, 144, 0.5)";
} else {
cells[k].style.backgroundColor = "rgba(240, 128, 128, 0.5)";
}
}
}
}
}
var rows = tables[tables.length - 2].querySelectorAll("tr");
for (var j = 1; j < rows.length; j++) { // start from the second row (index 1)
var cells = rows[j].querySelectorAll("td");
for (var k = 0; k < cells.length; k++) {
if (k === 0) continue; // skip the first column
if (cells[k].innerHTML !== "") {
if (cells[k].innerHTML > 0) {
cells[k].style.backgroundColor = "rgba(144, 238, 144, 0.5)";
} else {
cells[k].style.backgroundColor = "rgba(240, 128, 128, 0.5)";
}
}
}
}
var rows = tables[tables.length - 1].querySelectorAll("tr");
for (var j = 1; j < rows.length; j++) { // start from the second row (index 1)
var cells = rows[j].querySelectorAll("td");
for (var k = 0; k < cells.length; k++) {
if (k === 0) continue; // skip the first column
if (cells[k].innerHTML !== "") {
if (cells[k].innerHTML > 0) {
cells[k].style.backgroundColor = "rgba(144, 238, 144, 0.5)";
} else {
cells[k].style.backgroundColor = "rgba(240, 128, 128, 0.5)";
}
}
}
}
}
window.onload = changeColor;
</script>
<style>
body {
background-color: #333;
color: white;
}
table {
display: block;
border-collapse: collapse;
}
td, th {
border: 1px solid white;
padding: 8px;
}
th {
background-color: #666
font-size: 12px;
font-weight: bold;
}
td {
text-align: center;
font-size: 10px;
}
</style>
</head>
</head>
<body>
<table>
{{ df1_html }}
</table>
<br><br>
<table>
{{ df2_html }}
</table>
<br><br>
<div style="display: flex;">
<div style="flex: 1; padding-right: 10px;">
<div class="table-title">ISONE HDD Delta</div>
<table>
{{ df3_html }}
</table>
</div>
<div style="flex: 1; padding-left: 10px;">
<div class="table-title">NYISO HDD Delta</div>
<table>
{{ df4_html }}
</table>
</div>
</div>
</body>
</html>```
I have tried adjusting the boundaries on the formatting, but nothing seems to change it. I am expecting table 3 and 4 to have the color conditional formatting applied to all but the first column.
I have the below design to be created dynamically
I have this layout & wanted to convert this using HTML CSS. My requirement is that, when any column is deleted another column should increase the space in place of a deleted column.
Also, the same is needed when someone removes any row, the previous row should take place of that row.
To build the grid: You can archive this by using a loop inside a loop. For Styling i use flex box system.
const c = document.querySelector('div');
for (let i = 0; i < 4; i++) {
const row = document.createElement('div');
row.setAttribute('class','row')
for (let j = 0; j < 4; j++) {
const cell = document.createElement('div');
cell.setAttribute('class','cell')
cell.innerHTML = i + ' - ' +j
row.append(cell);
}
c.append(row)
}
.container {
background: yellow;
height:100vh;
}
.row {
display: flex;
}
.cell {
border: 1px solid black;
padding: 5px;
width:25%;
height: 25vh;
}
<div class="container"></div>
var rows = 55;
var cols = 140;
var rowsInput = document.getElementById("rowsId");
var colsInput = document.getElementById("colsId");
//initialize 2dim arrays
var arr;// current generation array
var nextArr; // next generation array
function drawGrid() {
let grid = document.getElementById("container");
let table = document.createElement("table");
table.setAttribute("class", "center");
for (let i = 0; i < rows; i++) {
let tr = document.createElement("tr");
for (let j = 0; j < cols; j++) {
let cell = document.createElement("td");
cell.setAttribute("id", i + "_" + j);
cell.setAttribute("class", "dead");
tr.appendChild(cell);
cell.addEventListener("click", function () {
if (cell.classList.contains("live")) {
cell.setAttribute("class", "dead");
arr[i][j] = 0;
}
else
cell.setAttribute("class", "live");
arr[i][j] = 1;
});
}
table.appendChild(tr);
}
grid.appendChild(table);
}
function make2DArr() {
arr = new Array(rows);
nextArr = new Array(rows);
for (let i = 0; i < rows; i++) {
arr[i] = new Array(cols);
nextArr[i] = new Array(cols);
}
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
arr[i][j] = 0;
nextArr[i][j] = 0;
}
}
}
var generateBtn = document.getElementById("generateBtnId");
generateBtn.addEventListener("click", function () {
rows = rowsInput.value;
cols = colsInput.value;
remove();
make2DArr();
drawGrid();
});
function remove() {
var tb = document.querySelector("table");
tb.outerHTML = "";
}
function init() {
make2DArr();
drawGrid();
}
init();
body {
background-color: rgba(76, 77, 62, 0.514);
}
.center {
margin: auto;
width: 70rem;
height: 30rem;
padding: 0.5rem;
}
#container {
position: relative;
border:1px rgb(26, 51, 192) solid;
margin: 0;
overflow: auto;
display: flex;
}
table {
border:1px rgb(241, 241, 241) solid;
border-spacing: 0;
flex:1;
}
.live {
background-color:rgba(0, 0, 0, 0.685);
}
.dead {
background-color:rgba(228, 228, 241, 0.829);
}
td {
flex:1;
position: relative;
border:1px rgb(29, 182, 29) solid;
width: 0.5rem;
height: 0.5rem;
}
button {
background-color: #4CAF50; /* Green */
border: none;
color: white;
padding: 1rem 2rem;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 0.5rem 0.5rem;
transition-duration: 0.4s;
cursor: pointer;
}
button:hover {
background-color: rgba(144, 180, 145, 0.596);
color: rgb(54, 59, 54)
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel=stylesheet type="text/css" href="game.css">
</head>
<body>
<div class="center">
<div id="container">
</div>
<button id="startBtnId"><span>Start</span></button>
<button id="clearBtnId"><span>Clear</span></button>
<button id="randomBtnId"><span>Random</span></button>
<button id="generateBtnId"><span>Generate</span></button>
<input type="text" id="rowsId" value="rows">
<input type="text" id="colsId" value="cols">
<select id="sizeId">
<option value="Big">Big</option>
<option value="Medium">Medium</option>
<option value="Small">Small</option>
</select>
</div>
<script type="text/javascript" src="game.js"></script>
</body>
</html>
I would like to enter a number of rows and columns manually and the new table will be contained in the fixed container size.
For example, when I weigh 20 rows and 20 columns the container will be the same size as the one where 50 lines and 50 columns are balanced.
Enter the number of rows and columns, and then click generate
Since you have specified container as flex so if you add as many colum it will fit in to container, remove the flex and give width manual and check.
I've dynamic table with fixed width and height. Using random function I get numbers of cells (3x3 - 7x7). Table is rendering well. On each cell I've click event which write one letter to pointed cell. There a problem, because row is expanding on height. How to stop it?
https://codepen.io/anon/pen/PdrNVM
let table = document.getElementById('table');
let tableSize = Math.floor((Math.random() * 5) + 3);
console.log(tableSize);
for (i = 0; i < tableSize; i++) {
let tr = document.createElement('tr');
for (j = 0; j < tableSize; j++) {
let td = document.createElement('td');
let content = document.createTextNode('');
td.appendChild(content);
tr.appendChild(td);
}
table.appendChild(tr);
}
let tds = document.querySelectorAll("td");
tds.forEach((td) => {
td.addEventListener("click", function() {
td.innerHTML = 'p';
});
});
table {
width: 200 px;
height: 200 px;
table-layout: fixed;
}
td {
background-color: green;
}
<h2>Click in cell</h2>
<table id='table'></table>
Just set a proper height and max-height like:
td {
background-color: green;
height: 50px;
max-height: 50px;
}
Set a fixed height for the tds. You don't need use the height for the table, let the child td make up the height.
table {
width: 200px;
table-layout: fixed;
}
td {
height: 60px;
background-color: green;
}
I want to create a table which can be scrolled horizontally. The number of columns is dynamic. I want to achieve when we scroll the table horizontally the first column needs to be fixed/frozen. I have tried the following css. The first column is fixed alright but, second, third columns are hidden under the fixed column.
.mydiv {
overflow-y: scroll;
}.headcol {
position:absolute;
width: 100px;
text-align:center;
background-color: #4CAF50;
color: white;
font-family:arial;
font-size:13px;
padding-left:15px;
padding-right:15px;
padding-top:10px;
padding-bottom:10px;
border: 1px solid #c4c0c0;
}
.tablemy {
border-collapse: collapse;
border: 1px solid #c4c0c0;
}
.thmy {
text-align:center;
background-color: #4CAF50;
color: white;
font-family:arial;
font-size:13px;
padding-left:15px;
padding-right:15px;
padding-top:10px;
padding-bottom:10px;
border: 1px solid #c4c0c0;
}
.tdmy {
white-space: nowrap;
padding:8px; border-left:1px solid #ccc; border-top:1px solid #ccc;
padding-left:15px;
padding-right:15px;
padding-top:10px;
padding-bottom:10px;
font-size:12px;
font-family:arial;
border: 1px solid #c4c0c0;
color:#585858;
}
.trmy:nth-child(odd) {
background-color:#F5F5F5;
}
tr:nth-child(even) {
background-color:#ffffff;
}
And my table is
I'm creating my table in javascript.
$(window).load(function(){
var customers = new Array();
customers.push(["TN04 AH 0253", "55"]);
customers.push(["TN04 AF 8830", "67"]);
customers.push(["TN37 CM 1249", "33"]);
customers.push(["TN19 F 9175", "47"]);
customers.push(["TN37CM 1932", "0"]);
var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
var dateArray = getDates(firstDay, lastDay);
// Create a HTML Table element.
var table = document.createElement("TABLE");
table.setAttribute('class', 'tablemy');
table.border = "1";
// Get the count of columns.
var columnCount = customers[0].length;
var row = table.insertRow(-1);
for (var i = 0; i < dateArray.length; i++) {
var headerCell = document.createElement("TH");
if (i === 0) {
headerCell.setAttribute('class', 'headcol');
} else {
headerCell.setAttribute('class', 'thmy');
}
//headerCell.setAttribute('class', 'thmy');
headerCell.innerHTML = dateArray[i];
row.appendChild(headerCell);
}
// Add the data rows.
for (var i = 0; i < customers.length; i++) {
row = table.insertRow(-1);
row.setAttribute('class', 'trmy');
for (var j = 0; j < columnCount; j++) {
var cell = row.insertCell(-1);
if (j === 0) {
cell.setAttribute('class', 'headcol');
} else {
cell.setAttribute('class', 'tdmy');
}
//cell.setAttribute('class', 'tdmy');
cell.innerHTML = customers[i][j];
}
}
var dvTable = document.getElementById("dvTable");
dvTable.innerHTML = "";
dvTable.appendChild(table);
})
function getDates(date1, date2) {
var day = 1000 * 60 * 60 * 24;
var diff = (date2.getTime() - date1.getTime())/day;
var dateArray = new Array();
dateArray.push("Unit/Count");
for(var i=0; i<=diff; i++)
{
var xx = date1.getTime() + day * i;
var yy = new Date(xx);
var strDate = yy.getDate() + "/" + (yy.getMonth() + 1);
dateArray.push(strDate);
}
return dateArray;
}
I think you need to set position: fixed for 1st column and to make all other columns visible you need to set padding-left: 130px; (which is equal to width of first column) for .tablemy :
function getDates(date1, date2) {
var day = 1000 * 60 * 60 * 24;
var diff = (date2.getTime() - date1.getTime())/day;
var dateArray = new Array();
dateArray.push("Unit/Count");
for(var i=0; i<=diff; i++)
{
var xx = date1.getTime() + day * i;
var yy = new Date(xx);
var strDate = yy.getDate() + "/" + (yy.getMonth() + 1);
dateArray.push(strDate);
}
return dateArray;
}
var customers = new Array();
customers.push(["TN04 AH 0253", "55"]);
customers.push(["TN04 AF 8830", "67"]);
customers.push(["TN37 CM 1249", "33"]);
customers.push(["TN19 F 9175", "47"]);
customers.push(["TN37CM 1932", "0"]);
var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
var dateArray = getDates(firstDay, lastDay);
// Create a HTML Table element.
var table = document.createElement("TABLE");
table.setAttribute('class', 'tablemy');
table.border = "1";
// Get the count of columns.
var columnCount = customers[0].length;
var row = table.insertRow(-1);
for (var i = 0; i < dateArray.length; i++) {
var headerCell = document.createElement("TH");
if (i === 0) {
headerCell.setAttribute('class', 'headcol');
} else {
headerCell.setAttribute('class', 'thmy');
}
//headerCell.setAttribute('class', 'thmy');
headerCell.innerHTML = dateArray[i];
row.appendChild(headerCell);
}
// Add the data rows.
for (var i = 0; i < customers.length; i++) {
row = table.insertRow(-1);
row.setAttribute('class', 'trmy');
for (var j = 0; j < columnCount; j++) {
var cell = row.insertCell(-1);
if (j === 0) {
cell.setAttribute('class', 'headcol');
} else {
cell.setAttribute('class', 'tdmy');
}
//cell.setAttribute('class', 'tdmy');
cell.innerHTML = customers[i][j];
}
}
var dvTable = document.getElementById("dvTable");
dvTable.innerHTML = "";
dvTable.appendChild(table);
html, body {
margin: 0;
padding: 0;
}
.mydiv {
overflow-y: scroll;
}
.headcol {
position: fixed;
left: 0;
width: 100px;
text-align:center;
background-color: #4CAF50;
color: white;
font-family:arial;
font-size:13px;
padding-left:15px;
padding-right:15px;
padding-top:10px;
padding-bottom:10px;
border: 1px solid #c4c0c0;
}
.tablemy {
border-collapse: collapse;
border: 1px solid #c4c0c0;
margin-left: 130px;
}
.thmy {
text-align:center;
background-color: #4CAF50;
color: white;
font-family:arial;
font-size:13px;
padding-left:15px;
padding-right:15px;
padding-top:10px;
padding-bottom:10px;
border: 1px solid #c4c0c0;
}
.tdmy {
white-space: nowrap;
padding:8px; border-left:1px solid #ccc; border-top:1px solid #ccc;
padding-left:15px;
padding-right:15px;
padding-top:10px;
padding-bottom:10px;
font-size:12px;
font-family:arial;
border: 1px solid #c4c0c0;
color:#585858;
}
.trmy:nth-child(odd) {
background-color:#F5F5F5;
}
tr:nth-child(even) {
background-color:#ffffff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="dvTable"></table>