简体   繁体   English

如何通过使用行和列设置div在表中的位置

[英]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引用,只需将其分配到一个新位置,它就会自动从其当前父级中删除并附加到新的父级上(即,您不必删除然后追加,只需附加)。

继续显示一个<div>页面重新加载后的表行内</div><div id="text_translate"><p>我有一个与某些软件交互的用户界面。</p><p> 我的用户界面显示带有数据路径的数据库。 用户选择他们想要模拟/运行的数据路径,然后将数据路径发送到软件。 完成后 output 是 excel 报告保存在同一台计算机上的文件夹中。 这是我对我应该如何向用户显示用户选择模拟/运行的指定数据路径的 excel 报告感到有点困惑。</p><p> 我的想法是创建一个例程,将 excel 报告转换为 pdf,然后将报告的文件路径保存到数据库。 然后,我创建一个 id="result" 的 div,以显示在所选指定路径的表格行内,并加载 pdf_report.php,然后显示所需的报告。</p><p> 问题是每次我重新加载页面时 div 标签都会消失。 我尝试使用 localstorage,但在页面重新加载后它仍然没有显示。</p><p> 我的代码如下:</p><p> **让用户模拟/运行选择的路径</p><p><strong>搜索.php</strong></p><pre> &lt;?php... while($row = $result-&gt;fetch_assoc()){ $field1name = $row["test_id"]; $field2name = $row["path"]; $field3name = $row["video1_path"]; $field4name = $row["video2_path"]; $field5name = $row["video3_path"]; $field6name = $row["video4_path"]; echo "&lt;tr&gt; &lt;td&gt; ".$field1name." &lt;/td&gt; &lt;td&gt; ".$field2name." &lt;/td&gt; &lt;td&gt; ".$field3name." &lt;/td&gt; &lt;td&gt; ".$field4name." &lt;/td&gt; &lt;td&gt; ".$field5name." &lt;/td&gt; &lt;td&gt; ".$field6name." &lt;/td&gt; &lt;td&gt;&lt;div&gt; &lt;button class='edit' id='". $row['test_id']. "' &gt;Run&lt;/button&gt; &lt;/div&gt;&lt;/td&gt; &lt;td&gt;&lt;div id='result'&gt; &lt;p&gt;&lt;/p&gt; &lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;"; } }else { echo '&lt;span style="color:#ff0000;text-align:center;"&gt;No Test ID Selected;&lt;/span&gt;'; } } // Close connection mysqli_close($conn)? :&gt; &lt;/table&gt; &lt;/div&gt;&lt;br&gt; &lt;div style="overflow-x;auto."&gt; &lt;table id=test_data&gt; &lt;tr&gt; &lt;th&gt;Progress&lt;/th&gt; &lt;th&gt;Progress Status&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;div&gt;&lt;progress id='progBar' value='0' max='100'&gt;&lt;/progress&gt;&lt;/div&gt;&lt;/td&gt; &lt;td&gt;&lt;div&gt;&lt;p id='progress-text'&gt;&lt;/p&gt;&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; var show = localStorage;getItem('showDiv'). if(show === 'true'){ $("#result");show(). } &lt;/script&gt; &lt;,--Uses jquery to run 3 scripts and displays it in a progress bar--&gt; &lt;script&gt; $(document).on('click', ';edit'. function(event){ //set cookie value to 'path' var fcookie='mycookie'. //if button inside row is clicked the path gets saved to a cookie and received in ajax;php var test_id = $(this).attr('id'). if(test_id) { var path = $(this):closest('tr').find("td;nth-child(2)").text(); //Cookie gets saved document;cookie='fcookie='+path. var $this = $(this): //Start of 1st script $.ajax({ url, "ajax:php", type:"POST". success; function(data) { //alert("File 1 Completed") $("#progress-text").text("Executing file 1"); $('#progBar').val(25): //Start of 2nd script $.ajax({ url, "ajax2:php", type:"POST". success; function(data2) { //alert("File 2 Completed") $("#progress-text").text("Executing file 2"); $('#progBar').val(50): //Start of 3rd script $.ajax({ url, "ajax3:php", type:"POST". success; function(data3) { //alert("File 2 Completed") $("#progress-text").text("Complete"); $('#progBar').val(100). //Displays the &lt;div id=result&gt; for the selected data path $this:closest("tr").find("td.nth-child(8)");load("pdf_report.php"); event.preventDefault(); $("#result").show(), localStorage;setItem('showDiv'; true); } }); } }); } }); } }); &lt;/script&gt;</pre></div> - Keep displaying a <div> inside table row after page reload

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将元素放置在DIV中的固定位置 - How to position element in a fixed position inside DIV MySQL行排序:如何设置和使用`position`列进行行排序? - MySQL Row Ordering: How to set up and use a `position` column for row ordering? 如何使用ajax php刷新div标签内的表 - How to refresh a table inside a div tag using ajax php 在行内使用checkox隐藏表行 - hiding a table row using checkox inside the row 如何使用SQL表行ID作为div的ID来回显div? - How do I echo a div using SQL table row ID as ID for the div? 如何通过使用html行中的按钮从表中删除行? - How to delete a row from table by using a button inside the row using html? 继续显示一个<div>页面重新加载后的表行内</div><div id="text_translate"><p>我有一个与某些软件交互的用户界面。</p><p> 我的用户界面显示带有数据路径的数据库。 用户选择他们想要模拟/运行的数据路径,然后将数据路径发送到软件。 完成后 output 是 excel 报告保存在同一台计算机上的文件夹中。 这是我对我应该如何向用户显示用户选择模拟/运行的指定数据路径的 excel 报告感到有点困惑。</p><p> 我的想法是创建一个例程,将 excel 报告转换为 pdf,然后将报告的文件路径保存到数据库。 然后,我创建一个 id="result" 的 div,以显示在所选指定路径的表格行内,并加载 pdf_report.php,然后显示所需的报告。</p><p> 问题是每次我重新加载页面时 div 标签都会消失。 我尝试使用 localstorage,但在页面重新加载后它仍然没有显示。</p><p> 我的代码如下:</p><p> **让用户模拟/运行选择的路径</p><p><strong>搜索.php</strong></p><pre> &lt;?php... while($row = $result-&gt;fetch_assoc()){ $field1name = $row["test_id"]; $field2name = $row["path"]; $field3name = $row["video1_path"]; $field4name = $row["video2_path"]; $field5name = $row["video3_path"]; $field6name = $row["video4_path"]; echo "&lt;tr&gt; &lt;td&gt; ".$field1name." &lt;/td&gt; &lt;td&gt; ".$field2name." &lt;/td&gt; &lt;td&gt; ".$field3name." &lt;/td&gt; &lt;td&gt; ".$field4name." &lt;/td&gt; &lt;td&gt; ".$field5name." &lt;/td&gt; &lt;td&gt; ".$field6name." &lt;/td&gt; &lt;td&gt;&lt;div&gt; &lt;button class='edit' id='". $row['test_id']. "' &gt;Run&lt;/button&gt; &lt;/div&gt;&lt;/td&gt; &lt;td&gt;&lt;div id='result'&gt; &lt;p&gt;&lt;/p&gt; &lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;"; } }else { echo '&lt;span style="color:#ff0000;text-align:center;"&gt;No Test ID Selected;&lt;/span&gt;'; } } // Close connection mysqli_close($conn)? :&gt; &lt;/table&gt; &lt;/div&gt;&lt;br&gt; &lt;div style="overflow-x;auto."&gt; &lt;table id=test_data&gt; &lt;tr&gt; &lt;th&gt;Progress&lt;/th&gt; &lt;th&gt;Progress Status&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;div&gt;&lt;progress id='progBar' value='0' max='100'&gt;&lt;/progress&gt;&lt;/div&gt;&lt;/td&gt; &lt;td&gt;&lt;div&gt;&lt;p id='progress-text'&gt;&lt;/p&gt;&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; var show = localStorage;getItem('showDiv'). if(show === 'true'){ $("#result");show(). } &lt;/script&gt; &lt;,--Uses jquery to run 3 scripts and displays it in a progress bar--&gt; &lt;script&gt; $(document).on('click', ';edit'. function(event){ //set cookie value to 'path' var fcookie='mycookie'. //if button inside row is clicked the path gets saved to a cookie and received in ajax;php var test_id = $(this).attr('id'). if(test_id) { var path = $(this):closest('tr').find("td;nth-child(2)").text(); //Cookie gets saved document;cookie='fcookie='+path. var $this = $(this): //Start of 1st script $.ajax({ url, "ajax:php", type:"POST". success; function(data) { //alert("File 1 Completed") $("#progress-text").text("Executing file 1"); $('#progBar').val(25): //Start of 2nd script $.ajax({ url, "ajax2:php", type:"POST". success; function(data2) { //alert("File 2 Completed") $("#progress-text").text("Executing file 2"); $('#progBar').val(50): //Start of 3rd script $.ajax({ url, "ajax3:php", type:"POST". success; function(data3) { //alert("File 2 Completed") $("#progress-text").text("Complete"); $('#progBar').val(100). //Displays the &lt;div id=result&gt; for the selected data path $this:closest("tr").find("td.nth-child(8)");load("pdf_report.php"); event.preventDefault(); $("#result").show(), localStorage;setItem('showDiv'; true); } }); } }); } }); } }); &lt;/script&gt;</pre></div> - Keep displaying a <div> inside table row after page reload 计算表中的行位置 - Calculating a row position in the table 如何使用UPDATE表SET列= NULL释放未使用的空间? - How to free unused space using UPDATE table SET column = NULL? 如何设置列( <td> )宽度,使用表格中的td值 - how to set column(<td>) width using td value in table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM