scroll, orden y mas cosas
este post viene de uno abierto en programacion servidor, pero el tema se desvio y continua por aqui.
Se trata de codigo para ordenar una tabla mediante dom, en el cliente:
1) me hago un fichero ordenar.js que contiene lo sigueinte
<code>
function createRowsArray (table) {
var rows = new Array();
var r = 0;
if (table.tHead == null && table.tFoot == null)
for (var r1 = 0; r1 < table.rows.length; r1++, r++)
rows[r] = table.rows[r1];
else
for (var t = 0; t < table.tBodies.length; t++)
for (var r1 = 0; r1 < table.tBodies[t].rows.length; r1++, r++)
rows[r] = table.tBodies[t].rows[r1];
return rows;
}
function insertSortedRows(table, rows) {
if (document.all) var rowsCopy = new Array(rows.length)
for (var r = 0; r < rows.length; r++) {
if (document.all) rowsCopy[r] = rows[r].cloneNode(true);
table.deleteRow(rows[r].rowIndex);
}
var tableSection = table.tBodies[table.tBodies.length - 1];
for (var r = 0; r < rows.length; r++) {
var row = document.all ? rowsCopy[r] : rows[r];
tableSection.appendChild(row);
}
}
function sortTable (table, sortFun) {
var rows = createRowsArray(table);
if (rows.length > 0) {
rows.sort(sortFun);
insertSortedRows(table, rows);
}
}
//para invertir el orden hay que invertir row1 y row2
function sortRowsAlpha (row1 , row2) {
var column = sortRowsAlpha.col;
var cell1 = row1.cells[column].firstChild.nodeValue;
var cell2 = row2.cells[column].firstChild.nodeValue;
return cell1 < cell2 ? - 1 : (cell1 == cell2 ? 0 : 1);
}
function sortRowsNumber (row1 , row2) {
var column = sortRowsNumber.col;
var cell1 = parseFloat(row1.cells[column].firstChild.nodeValue);
var cell2 = parseFloat(row2.cells[column].firstChild.nodeValue);
return cell1 < cell2 ? - 1 : (cell1 == cell2 ? 0 : 1);
}
function sortRowsLink (row1 , row2) {
var column = sortRowsLink.col;
var cell1 = findFirstLinkChild(row1.cells[column]).href;
var cell2 = findFirstLinkChild(row2.cells[column]).href;
return cell1 < cell2 ? - 1 : (cell1 == cell2 ? 0 : 1);
}
function findFirstLinkChild (el) {
var child = el.firstChild;
while (child.tagName != 'A')
child = child.nextSibling;
return child;
}
function testSortTable(table, col) {
sortRowsAlpha.col = col;
sortTable(table, sortRowsAlpha);
}
function testSortTableNumerical (table, col) {
sortRowsNumber.col = col;
sortTable(table, sortRowsNumber);
}
function testSortTableLinks (table, col) {
sortRowsLink.col = col;
sortTable(table, sortRowsLink);
}
function findTableParent (node) {
while (node.tagName.toUpperCase() != 'TABLE')
node = node.parentNode;
return node;
}</code>
2)luego a la hora de usarlo:
//primero importo ordenar.js
<script type="text/javascript" src="ordenar.js"></script>
declaro mi tabla con su parte <thead> y <tbody>
y en cada celda de la cabecera pongo:
<code>ONCLICK="testSortTable(document.getElementById('fab'), 0);"</code>
donde fab es el id de la tabla y par colomo funciona jejejejejeje
lo de ponerle el overflow a la tabla directamente lo voy a probar ahora mismo........................
nota:si se quiere hacer scroll en la tabla es recomendable hacer 2 tablas una con la cabecera que no entre dentro del scroll y otra con el contenido que sera lo que scrolleemos.
meddle
este ordenar.js me parece bastante sucio por diversas razones (namespace, inicializacion en html, etc)
habria que mirar bien lo del scroll en la tabla, creo que deberia poderse hacer con un height fijo en px y un overflow:auto o bien scroll:auto
hartum
Lo siento pero lo de la tabla no ha funcionado, o al menos yo no he conseguido que funcione y hubiese sido un puntazo que funcionase asi la verdad, se que en mozilla puedes hacer scroll hasta del tbody solamente pero en explorer no va, sinembargo lo he metido en una capa y funciona perfect, ahora estoy tratando de que la capa me scrolle sola cuando pulso un boton, he probado con esto:
<code>onClick="document.getElementById("fab").scrollTop = 700;"</code>
pero ha pasado de mi olimpicamente, ¿alguna idea?
pd:exactamente como en tu portafolio jejejejejejeejeje.