简体   繁体   English

是否可以仅使用CSS和单元格的td值将边框动态分配给html表?

[英]Is it possible to assign borders to an html table dynamically using CSS and the td value of the cell only?

I have data coming in from a database that is loaded into an html table. 我有来自加载到html表的数据库的数据。 Because of this, I can't easily add different classes and id's to td's. 因此,我无法轻松地将不同的类和id添加到td中。

So I have a table of values in a table, call it table 1. Then I have another set of values in a separate query that have the values of each "group". 因此,我在一个表中有一个值表,称为表1。然后在单独的查询中有另一组值,每个值具有每个“组”的值。 This is loaded into an html variable which is loaded into a non-displaying table, call this table 2. The group values in table 2 are what I want to use to assign the new borders in table 1 with. 这被加载到html变量中,该html变量被加载到非显示表中,称为表2。表2中的组值是我要用来在表1中分配新边框的对象。 The values in table 1 don't matter. 表1中的值无关紧要。

So essentially, I want to use table 2 values to assign the borders on table 1. The values in table 2 are like this (I'd prefer not to change these if possible): 因此,从本质上讲,我想使用表2的值来分配表1上的边框。表2中的值是这样的(如果可能,我不希望更改这些值):

0   1.1   1.2   1.3   2.1   2.2   2.3   3.1

I would assign each group number (1.1 for example) with a certain border style which would apply to the other table. 我将为每个组号(例如1.1)分配一定的边框样式,该样式将应用于其他表。

I'm not too savvy with all of this stuff yet so often times I don't know if what I want to do is possible or not. 我不太了解所有这些东西,但是很多时候我不知道我想做的事是否可行。 So please let me know if you have any ideas or if you think it's just not going to happen. 因此,请让我知道您是否有任何想法,或者您认为这将不会发生。

I'm able to incorporate javascript, jquery, sql, css and html elements, but this runs on a local machine so no php, etc. The data comes from a database so if there is a trick to applying something through sql keep that idea in mind. 我能够合并javascript,jquery,sql,css和html元素,但这在本地计算机上运行,​​因此没有php等。数据来自数据库,因此,如果存在通过sql进行应用的技巧,请保持该想法心里。

Here is an old fiddle showing the "table 1" loaded using html table strings as variables (instead of originating from sql). 这是一个古老的小提琴,显示了使用html表字符串作为变量(而不是源自sql)加载的“表1”。 This is not how it currently works, but it shows what it looks like. 这不是当前的工作方式,但显示出它的外观。

Sample fiddle Slide the sliders and the data will appear. 样本小提琴滑动滑块,将出现数据。

The table 1 has a bunch of values in it. 表1中有一堆值。 I will load another table, table 2, that will have the group values in it and I want to apply this to the table 1. 我将加载另一个表,表2,该表中将具有组值,并且我想将此应用于表1。

Thanks for the assist! 感谢您的协助!

since html doesn't use variables, I believe you mean javascript variables. 由于html不使用变量,因此我相信您的意思是javascript变量。 let us call the variable you will store your "group" in group. 让我们调用变量,您将“组”存储在组中。

var group = document.getElementById('table2').value
if you want to base your border color for table1 on the value stored in a field of table2, try something like the following switch(case) statement. 如果要基于表2的字段中存储的值为表1设置边框颜色,请尝试以下类似switch(case)语句的操作。 Obviously, you will substitute the actual div id you are referencing in table2 and the actual div id you assign to your table1. 显然,您将替换在table2中引用的实际div id和分配给table1的实际div id。 This is just an overview you can use as a template. 这只是可以用作模板的概述。 For instance, if you want to change the border width, instead of color, your code blocks would be document.getElementById('table1').style.borderWidth = "thick"; 例如,如果要更改边框宽度而不是颜色,则代码块将为document.getElementById('table1')。style.borderWidth =“ thick”; etc. 等等
  switch(group){ case 0: document.getElementById('table1').style.borderColor = "red"; break; case 1.1: document.getElementById('table1').style.borderColor = "blue"; break; case 1.2: document.getElementById('table1').style.borderColor = "yellow"; default: document.getElementById('table1').style.borderColor = "black"; 

\n\n

}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM