【问题标题】:Generate html table with dynamic rows and column using javascript使用javascript生成具有动态行和列的html表
【发布时间】:2021-12-05 04:08:35
【问题描述】:

我有这些数据集

var questions = [
  { id: 1, 'question': 1, 'section': 1 },
  { id: 2, 'question': 1, 'section': 2 },
  { id: 3, 'question': 2, 'section': 1 },
  { id: 4, 'question': 2, 'section': 2 }
]
var students = {
  'student_1': [
    {id: 1, answer: 1, question_id: 1},
    {id: 1, answer: 1, question_id: 2},
    {id: 1, answer: 2, question_id: 3},
    {id: 1, answer: 1, question_id: 4}
  ],
  'student_2': [
    {id: 1, answer: 2, question_id: 1},
    {id: 1, answer: 1, question_id: 2},
    {id: 1, answer: 2, question_id: 3},
    {id: 1, answer: 1, question_id: 4}
  ],
  'student_3': [
    {id: 1, answer: 1, question_id: 1},
    {id: 1, answer: 1, question_id: 2},
    {id: 1, answer: 1, question_id: 3},
    {id: 1, answer: 1, question_id: 4}
  ]
}

如何使用以上数据创建表格?结果应该是:

Question   |   Section   |   student_1   |   student_2   |   student_3   |
__________________________________________________________________________
    1      |      1      |       1       |       2       |       1       |
__________________________________________________________________________
    1      |      2      |       1       |       1       |       1       |
__________________________________________________________________________
    2      |      1      |       2       |       2       |       1       |
__________________________________________________________________________
    2      |      2      |       1       |       1       |       1       |

我有这个初始的 javascript 代码,但我很困惑如何填写学生的数据

<div class="results"></div>
<script>
var questions = [
  { id: 1, 'question': 1, 'section': 1 },
  { id: 2, 'question': 1, 'section': 2 },
  { id: 3, 'question': 2, 'section': 1 },
  { id: 4, 'question': 2, 'section': 2 }
]
var students = {
  'student_1': [
    {id: 1, answer: 1, question_id: 1},
    {id: 1, answer: 1, question_id: 2},
    {id: 1, answer: 2, question_id: 3},
    {id: 1, answer: 1, question_id: 4}
  ],
  'student_2': [
    {id: 1, answer: 2, question_id: 1},
    {id: 1, answer: 1, question_id: 2},
    {id: 1, answer: 2, question_id: 3},
    {id: 1, answer: 1, question_id: 4}
  ],
  'student_3': [
    {id: 1, answer: 1, question_id: 1},
    {id: 1, answer: 1, question_id: 2},
    {id: 1, answer: 1, question_id: 3},
    {id: 1, answer: 1, question_id: 4}
  ]
}

var table = '<table class="table table-bordered">';
  table += '<thead>';
    table += '<tr style="background-color: #e5e5e5;">';
      table += '<th>Question</th>';
      table += '<th>Section</th>';
      for(var idx in students) {
        table += '<th class="text-center">'+idx+'</th>';
      }
    table += '</tr>';
  table += '</thead>';
  table += '<tbody>';
    Object.entries(questions).map(([key, question]) => {
      table += '<tr class="text-center">';
        table += '<td>'+question.question+'</td>';
        table += '<td>'+question.section+'</td>';
      table += '</tr>';
    })
  table += '</tbody>';
table += '</table>';
$('.results').append(table)
</script>

这是小提琴:https://jsfiddle.net/pLeqd8no/

【问题讨论】:

  • 对下面我的回答有任何疑问吗?

标签: javascript html-table


【解决方案1】:

我想你正在寻找类似的东西?

const questions = [ { id: 1, question: 1, section: 1 } , { id: 2, question: 1, section: 2 } , { id: 3, question: 2, section: 1 } , { id: 4, question: 2, section: 2 } ] 
const students = 
  { student_1: [ { id: 1, answer: 1, question_id: 1 } , { id: 1, answer: 1, question_id: 2 } , { id: 1, answer: 2, question_id: 3 } , { id: 1, answer: 1, question_id: 4 } ] 
  , student_2: [ { id: 1, answer: 2, question_id: 1 } , { id: 1, answer: 1, question_id: 2 } , { id: 1, answer: 2, question_id: 3 } , { id: 1, answer: 1, question_id: 4 } ] 
  , student_3: [ { id: 1, answer: 1, question_id: 1 } , { id: 1, answer: 1, question_id: 2 } , { id: 1, answer: 1, question_id: 3 } , { id: 1, answer: 1, question_id: 4 } ]
  } 

const
  Res   = document.querySelector('div.results')
, tbl   = Res.appendChild( document.createElement('table') ) 
, tBody = tbl.createTBody()
  ;
tbl.createTHead()

questions.forEach(({id, question, section},i) =>
  {
  if (i===0) 
    {
    let row = tbl.tHead.insertRow()
    row.insertCell().textContent =  'Question'
    row.insertCell().textContent =  'Section'
    }
  let row = tBody.insertRow()
  row.question_id              =  id
  row.insertCell().textContent =  question
  row.insertCell().textContent =  section
  })

Object.entries(students).forEach(([student,answers])=>
  {
  tbl.tHead.rows[0].insertCell().textContent = student;
  answers.forEach(({answer,question_id}) =>
    {
    [...tBody.rows].find(r=>r.question_id===question_id)
      .insertCell().textContent = answer
    })
  })
table  {
  border-collapse : collapse;
  margin          : 2em 1em;
  }
thead { 
  background-color : lightsteelblue;
  font-weight      : bold;
  }
td {
  padding    : .2em .8em;
  border     : 1px solid darkblue;
  text-align : center;
  }
&lt;div class="results"&gt;&lt;/div&gt;

【讨论】:

    【解决方案2】:

    以下是将数据转换为行的方法。然后就可以轻松地逐行生成HTML表格了。

    const questions = [
      { id: 1, 'question': 1, 'section': 1 },
      { id: 2, 'question': 1, 'section': 2 },
      { id: 3, 'question': 2, 'section': 1 },
      { id: 4, 'question': 2, 'section': 2 }
    ]
    const students = {
      'student_1': [
        {id: 1, answer: 1, question_id: 1},
        {id: 1, answer: 1, question_id: 2},
        {id: 1, answer: 2, question_id: 3},
        {id: 1, answer: 1, question_id: 4}
      ],
      'student_2': [
        {id: 1, answer: 2, question_id: 1},
        {id: 1, answer: 1, question_id: 2},
        {id: 1, answer: 2, question_id: 3},
        {id: 1, answer: 1, question_id: 4}
      ],
      'student_3': [
        {id: 1, answer: 1, question_id: 1},
        {id: 1, answer: 1, question_id: 2},
        {id: 1, answer: 1, question_id: 3},
        {id: 1, answer: 1, question_id: 4}
      ]
    }
    const studentEntries = Object.entries(students);
    const header = ["Question","Section",...studentEntries.map(s => s[0])];
    const rows = questions.map(question => [question.question,question.section,...studentEntries.map(s => s[1].find(a => a.question_id == question.id).answer)]);
    
    console.log(header);
    console.log(rows);
    猜你喜欢
    • 2019-04-29
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    相关资源
    最近更新 更多