[英]How to set the position of a div inside a table by using row and column
I have a table with a div in each row on the first column, I want to set its position on page load depending on what table row or column I want it to be. 我在第一列的每一行中都有一个带有div的表,我想根据它要成为的表行或列来设置其在页面加载中的位置。
This is my output table: 这是我的输出表:
col1 | col2 | col3 | col4 | col5 |
""""""""""""""""""""""""""""""""""
div1 | | | | |
""""""""""""""""""""""""""""""""""
div2 | | | | |
""""""""""""""""""""""""""""""""""
div3 | | | | |
I want on page load set their position to different column depending on the query of my database. 我想在页面加载时根据数据库查询将其位置设置为不同的列。 example: 例:
col1 | col2 | col3 | col4 | col5 |
""""""""""""""""""""""""""""""""""
| div1 | | | |
""""""""""""""""""""""""""""""""""
| | | div2 | |
""""""""""""""""""""""""""""""""""
div3 | | | | |
Is it possible..? 可能吗..? i can get the position in javascript using 我可以使用javascript获取位置
var pos = rd.get_position();
console.log( pos[0][0] );
Any method or advice is ok. 任何方法或建议都可以。 Thanks! 谢谢!
| col1 | col2 | col3 |
"""""""""""""""""""""""""""
row1 | x | | |
"""""""""""""""""""""""""""
row2 | | | y |
"""""""""""""""""""""""""""
row3 | | z | |
jQuery jQuery的
$('table tr:eq(0) td:eq(0)').text('x');
$('table tr:eq(1) td:eq(2)').text('y');
$('table tr:eq(2) td:eq(1)').text('z');
or... 要么...
$('td', $('table tr').eq(0)).eq(0).text('x');
$('td', $('table tr').eq(1)).eq(2).text('y');
$('td', $('table tr').eq(2)).eq(1).text('z');
Proof: http://jsfiddle.net/iambriansreed/mfTec/ 证明: http : //jsfiddle.net/iambriansreed/mfTec/
I'm assuming that you want to move each div so that it is a child of the specified column (as opposed to just positioning it visually on the column eg position: absolute). 我假设您要移动每个div,以使其成为指定列的子级(而不是仅在视觉上将其定位在列上,例如position:absolute)。
This solution uses an array with an item for each div in the table (so posList[0] holds the value for div1, postList[2] holds the value for div3, etc.). 此解决方案对表中的每个div使用一个带有项的数组(因此posList [0]保存div1的值,postList [2]保存div3的值,等等)。 The value of each array entry is a number specifying the column for each div. 每个数组条目的值是一个数字,用于指定每个div的列。
/*
posList is an array specifying, for each div, the zero-based column where you want to move each div.
Example: The array [1, 3, 0] means the first div will be in column 2, the second div in column 4 and the third div in column 1 (remember that they are zero-indexed).
In this code snippet it has been hard-coded for simplicity but it should be generated from the "query of [your] database" in whatever way is appropriate to your application.
*/
var posList = [1, 3, 0]; // Hard-coded for simplicity: div1 is in col2, div2 is in col4, div3 is in col1.
var rowList = $("table tr"); // Get all the rows in the table.
var divList = rowList.find("div"); // Find all the divs in the rows. This assumes there aren't any other divs in the table at all - make this selector more specific as required).
var i = 0
,colIndex = 0
,cell = null
,div = null
,row = null;
for (i = 0; i < posList.length; i++){
colIndex = posList[i]; // Get the column index for each <div>.
if (colIndex == 0){
continue; // Minor optimisation - if the index is 0 then we don't need to move the <div>.
}
div = divList.eq(i); // The <div> that needs to be moved.
row = rowList.eq(i); // This is the <tr> element that is the ancestor of the current <div>.
cell = row.find("td").eq(colIndex); // We want to move the <div> to the <td> element specified by the column index from posList.
div.detach().appendTo(cell); // Move the <div> to the new column.
}
You can use: 您可以使用:
// table is a reference to the table
// div is a reference to the div to move
table.rows(y).cells(x).appendChild(div);
where y is the row number (noting that they are indexed from zero from the top) and x is the column number (indexed from zero from the left). 其中y是行号(注意它们从顶部开始从零开始索引),而x是列号(从左侧开始从零开始索引)。 Also note that if you have a reference to the div, you just assign it to a new position, it will automatically be removed from it's current parent and appended to the new one (ie you don't have to do remove then append, just append). 还要注意,如果您有一个div引用,只需将其分配到一个新位置,它就会自动从其当前父级中删除并附加到新的父级上(即,您不必删除然后追加,只需附加)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.