【问题标题】:Displaying data in row in column 1, then rows in column 2, etc在第 1 列的行中显示数据,然后在第 2 列的行中显示数据,依此类推
【发布时间】:2017-05-09 02:42:11
【问题描述】:

首先,我找到了这个解决方案 (ColdFusion - Displaying rows as columns),如果我可以使用 table,它将解决我的问题,但我不能,因为这需要响应。但是,客户不喜欢响应式表格。他们真的,真的更喜欢显示列,这样用户只需要上下滚动,而不是上下左右滚动边。

我需要一些帮助将表格转换为引导程序<div>'s。我知道基础知识:跳过表格,tr 等于 div class="row"td 等于 div class="col-sm-*" 的东西。但是,更具体地说,我不确定用于转换上述 SO 解决方案的逻辑以及如何将其应用于我的需求。

将 Lucee 与 Bootstrap v3 一起使用,我正在尝试填充“网格”,但我需要在填充 column2 之前填充 column1 中的行。

1|10|20|30|40  
2|11|21|31|41  
3|12|22|32|42  
4|13|23|33|43

下面的代码 sn-p 来自 @travis,我不相信它

<cfset cols = 5>
<!--- get the number of rows so you know what record to display at the top of the next row. for example if our query contains "a,b,c,d,e,f,g,h,i,j,k,l,m" (13 elements) it will produce 3 totalrows--->
<cfset totalRows = ceiling(qMyQuery.RecordCount / cols)>
<!--- set inital record to 1 "output" is the actual cell of the query --->
<cfset output = 1>
<!--- Create table --->
<table width = "100%" border="0" align="center" cellpadding="2" cellspacing = "2">  
<!--- loop through the rows.  This loop will run 3 times in this example --->
    <cfloop from = "1" to = "#totalRows#" index = "thisRow">
    <tr>
        <!--- this loop will run 5 times in times in this example --->
        <cfloop from = "1" to = "#cols#" index = "thisCol">
        <!--- the width in the table cell will dynamicaly calculated to evenly distribute the cells. in this example if cols = 5 100/5 will make the cells 20% of the table --->
        <td width = "<cfoutput>#numberformat((100/cols), 99)#</cfoutput>%" align="center" nowrap style = "border: 1px solid #ccc;">
            <!--- Check current record with the record count, this will be used to display data or an empty cell --->
            <cfif output lte qMyQuery.recordCount>
                <cfoutput>#qMyQuery.Mon[output]#</cfoutput>
            <cfelse>
            <!--- use <br> to display an empty cell --->
                <br>
            </cfif>
            <!--- increment counter to the next record in this example if we started on the first cell of the first row it would be 1(a), then 4(d), then 7(g) and so on if this was the firs cell on the second row it would be 2(b), 5(e), 8(h), continue... --->
            <cfset output = output + totalRows>
        </td>
        </cfloop>
        <!--- this little bit tells where to start the next row. if we just finished the first row output would be 2(b) --->
        <cfset output = thisRow + 1>
    </tr>
    </cfloop>
</table>

这是我尝试使用引导程序完成同样的事情:

<!-- what I'm working with -->
<cfset cols = 2>
<!-- get the number of rows so you know what record to display at the top of the next row. for example if our query contains "a,b,c,d,e,f,g,h,i,j,k,l,m" (13 elements) it will produce 3 totalrows-->
<cfset totalRows = ceiling(qMyQuery.RecordCount / cols)>
<!--- set inital record to 1 "output" is the actual cell of the query --->
<cfset output = 1>
<!--- Create table --->
<div class="row">
    <div class="col-sm-12">
    <!--- loop through the rows.  This loop will run 3 times in this example --->
        <cfloop from = "1" to = "#totalRows#" index = "thisRow">
            <div class="row">
                <!--- this loop will run 5 times in this example --->
                <cfloop from = "1" to = "#cols#" index = "thisCol">
                    <div class="col-xs-3" style="border:1px solid red">
                        <cfif output lte qMyQuery.recordCount>
                            <cfoutput>#qMyQuery.mon[output]#</cfoutput>
                        <cfelse>
                            <br>
                        </cfif>
                        <cfset output = output + totalRows>
                    </div>
                </cfloop>
                <cfset output = thisRow + 1>
            </div>
        </cfloop>
    </div>
</div>

但是,我的工作输出只是在一个全宽页面中显示所有内容(border:1px 纯红色的内联样式用于查看代码生成的内容)。虽然这是针对 ColdFusion,但我认为循环的逻辑与 PHP 相同。

对此有任何提示、技巧、建议或最好的解决方案吗?

【问题讨论】:

  • Bootstrap 3.x 的网格系统使用 12 为基数。如果上面代码中的注释是正确的; “循环将运行 5 次”,这将创建五个 col-sx-3 div,总计 15 个。另一个潜在问题是 col-sx-3(在循环中)可能是一个错字。标准网格类是 col-xs-、col-sm-、cols-md- 和 col-lg-。当然,col-sx-3 可以是用户自定义的类...
  • 所以在我修复了一些缩进和其他东西之前不会让我发布我的代码,我忘记了它是什么。我熟悉网格(喜欢它)。 sx 是一个错字,我会在代码中修正它。至于列数,当前设置为 2 需要根据用于查看页面的设备是动态的。我将其留在代码中以帮助解释变量和值的位置,但事后认为它可能反而增加了混乱。
  • 我想过做一些快速的数学计算来获得返回的记录数(应该远低于 100)并将其除以我需要的列数,然后执行查询查询,针对每一列数据。
  • 我不喜欢 &lt;div class="row"&gt; 然后 &lt;div class="col-sm-12"&gt; 然后 &lt;div class="row"&gt; 行模式。 Bootstrap 真的不能那样工作。
  • @JamesAMohler 你确定吗? getbootstrap.com/css/#grid-nesting 提供了一个嵌套示例 - 或者这不是您的意思?如果没有,请您详细说明一下吗?

标签: twitter-bootstrap twitter-bootstrap-3 coldfusion lucee


【解决方案1】:

如果我有更多时间,我可能会做不同的事情,因为这并不漂亮,但它确实满足了要求,我已经在这方面花费了太多时间,但最重要的是,客户对这种方法很满意预算和时间。

对于此实施,列表将永远不会超过 75 条记录,99% 的时间将在平板电脑或更大的设备上使用,并且用户将使用它来选择复选框。还有一个“全选”选项,但未包含在此答案中 - 这是以后的用户故事。

我正在运行三个查询(运行一个查询,然后运行一个查询查询),一个收集前 XX 条记录,第二条 XX 条记录,XX 用于第三组记录。我正在为 QoQ 传递一个变量,该变量将确定将显示多少行,以便每列中都会出现一些数据。

然后输出看起来像这样(对不起,我永远无法在 SO 中得到完全正确的格式):

`    <div class="container">
    <div class="row">
        <div class="col-xs-3 col-xs-offset-1">
            <cfoutput query="colum1">
                <label><input type="checkbox"> #id# #mon#</label><br/>
            </cfoutput>
        </div>
        <div class="col-xs-3">
            <cfoutput query="colum2">
                <label><input type="checkbox"> #id# #mon#</label><br/>
            </cfoutput>
        </div>
        <div class="col-xs-3">
            <cfoutput query="colum3">
                <label><input type="checkbox"> #id# #mon#</label><br/>
            </cfoutput>
        </div>
    </div>
</div>`

【讨论】:

  • 出于好奇,QoQ 比 cfoutput start/endRow 给你带来了什么?
  • 好问题。我的理解是我可以缓存原始查询(少于 100 条记录),然后从缓存的内存中提取 25 条左右的记录(缓存的缓存?)。我在使用和不使用缓存以及使用和不使用 QoQ 的情况下运行它(我知道这些可能很昂贵)。性能差异可以忽略不计,QoQ 稍快一些。也许同样重要的是,代码是可读的。哈哈。
  • 如果数据不经常更改,那么缓存不是一个坏主意,但不遵循 QoQ 如何帮助解决以特定顺序输出数据的原始问题,即向下和交叉而不是上下。
  • @Leigh MAIN 查询按 id 排序,然后是 mon。 QoQA 将从 MAIN 中提取前 25 条记录,QoQB 将从 MAIN 中提取接下来的 25 条记录,然后 QoQC 将从 MAIN 中提取剩余的 25 条记录。每个“集合”都是按顺序排列的,这样我就可以在每列中按字母顺序列出。 QoQA 传入一个值来检索 25 条记录,QoQB 和 QoQC 做同样的事情。然后我将 QoQA 输出到由 cfoutput query="colum1"、cfoutput query="colum2"、cfoutput query="colum3" 定义的 ColumnA 中。这有帮助吗?
  • QoQ 的帮助可能没有我最初想象的那么大。我想我可以将我需要的记录数量作为附加参数传递给 CFC……重新考虑这种方法。
【解决方案2】:

我不确定您为什么说您的代码将所有内容都输出到一个列中。我试过你的例子,它似乎对我有用。您确定在您的页面中包含引导类吗?

我根据您的示例创建了一个 ColdFusion 要点:https://trycf.com/gist/cec79b0028728c9ec370e3bb8a05991f/lucee5?theme=monokai

我所做的唯一更改是使用您的示例数据创建一个查询对象,并将列数 (cols) 更改为 4,这与 Bootstraps 12 网格布局系统配合得很好。 您需要对5 列进行一些调整。

当您运行该代码时,它会显示您所说的单列数据。但那是因为尚未加载 Bootstrap 类。当我将该代码生成的输出复制到 Bootply 时,它会在四列中正确显示:http://www.bootply.com/a0dF7NKw9V

【讨论】:

  • 首先,感谢您的努力。我发布的代码确实可以工作,但我的代码可以工作,A)你必须硬编码列数 B)如果浏览器被调整大小,列不会堆叠和C)列表不会是数字,它将是不同文本长度的文本,并带有一个伴随文本的复选框。 HTH。在台式机上,我们可以有 4 列,平板电脑,3 列,在较小的设备上可以有 1 列(这就是为什么我不能提前硬编码 col 变量。
  • 这就是 Bootstrap 为您所做的。基本上,您需要为您想要的每个设备指定类。因此,您需要指定&lt;div class="col-xs-12 col-sm-4 col-md-3"&gt; 之类的东西,而不是仅仅指定&lt;div class="col-xs-3"&gt;。这意味着“xs”设备显示“1 列”,“sm”设备显示“3 列”,“md”设备显示“4 列”。这应该让你朝着正确的方向前进。棘手的部分是每次都尝试显示偶数列。
  • “每次的列数” 并且有问题 - 大声笑。我想我有一个可行的解决方案,我将在完成将伪代码转换为实际代码后不久发布。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
  • 2020-03-03
  • 2017-05-04
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多