简体   繁体   English

无法从映射函数传递数组对象作为参数 javascript

[英]Cannot pass array object from map function as argument javascript

I am trying to figure out why I cannot pass array object from map function as argument to function createModal(student) that is called onclick:我试图弄清楚为什么我不能将数组对象从 map 函数作为参数传递给调用 onclick 的函数createModal(student)
The error is caused by the parameter because when I tried function createModal() it worked fine.该错误是由参数引起的,因为当我尝试函数createModal()它工作正常。

const allWorkshops = await fetch("/api/workshops/detail");
const data = await allWorkshops.json();
result.innerHTML = "";
counter = 0;

data.forEach((workshop) => {
  ...
  ${workshop.students.map(student =>
     `<li class="list-group-item">
        <a data-bs-toggle="modal" data-bs-target="#studentModal" type="button" onclick="createModal(${student})">${student.first_name} ${student.last_name}</a>
      </li>`
  ).join("")}
  ...
});

I have seen this (and this ) question and tried changing the code as following but it did not work either:我已经看到了这个(和这个)问题,并尝试按以下方式更改代码,但它也不起作用:

...
${workshop.students.map(function(student) {
   var studentCopy = JSON.parse(JSON.stringify(student));
   return `<li class="list-group-item">
             <a data-bs-toggle="modal" data-bs-target="#studentModal" type="button" onclick="createModal(${studentCopy})">${student.first_name} ${student.last_name}</a>
           </li>`
}).join("")}
...

Unfortunately I don't know the exact error because the only thing I see in inspect in Chrome is this.不幸的是,我不知道确切的错误,因为我在 Chrome 中的检查中看到的唯一内容就是这个。 Which is at the beggining of index file and does not help me.这是索引文件的开头,对我没有帮助。

Uncaught SyntaxError: Unexpected identifier (at (index):1:21)未捕获的 SyntaxError:意外的标识符(在(索引):1:21)

EDIT: I have updated the code to look as following.编辑:我已将代码更新为如下所示。 It kinda works but the problem is that is have a table for workshops and every row has a list with the students inside and instead of appending students to the right workshop it appends them to the first list other are empty.它有点工作,但问题是有一个工作坊表,每一行都有一个里面有学生的列表,而不是将学生附加到正确的工作坊,而是将他们附加到第一个列表,其他列表是空的。

const workshopList = document.querySelector(".workshop-list");

async function getData(){
  const allWorkshops = await fetch("/api/workshops/detail");
  const data = await allWorkshops.json();
  data.forEach((workshop) => {
      var studentList = document.querySelector(".student-list");
      const tr = document.createElement("tr");
      tr.classList.add("responsive-table__row");
      tr.innerHTML = `
          <td class="responsive-table__body__text responsive-table__body__text--name">
              <ul class="list-group student-list">
                  ${workshop.students.forEach(student => {
                      let li = document.createElement('li');
                      li.classList.add('list-group-item');
                      let anchor = document.createElement('a');
                      anchor.dataset.bsToggle = 'modal';
                      anchor.dataset.bsTarget = '#studentModal';
                      anchor.setAttribute('type', 'button');
                      anchor.addEventListener('click', () => createModal(student));
                      anchor.innerText = student.last_name;
                      li.appendChild(anchor);
                      studentList.appendChild(li);
                  })}
              </ul>
          </td>
      `
      workshopList.appendChild(tr);
  });
};

student is an object. student是一个对象。 When you try to interpolate it into the template literal, it gets turned into the string [Object object] , which isn't a meaningful argument to create_modal() .当您尝试将其插入到模板文字中时,它会变成字符串[Object object] ,这对于create_modal()来说不是一个有意义的参数。

Instead of interpolating, you should create the element using DOM methods, and use addEventListener() with a closure to bind the click listener.您应该使用 DOM 方法创建元素,而不是插值,并使用带有闭包的addEventListener()来绑定单击侦听器。

 data.forEach((workshop) => { //... workshop.students.forEach(student => { let li = document.createElement('li'); li.classList.add('list-group-item'); let anchor = document.createElement('a'); anchor.dataset.bsToggle = 'modal'; anchor.dataset.bsTarget = '#studentModal'; anchor.setAttribute('type', 'button'); anchor.addEventListener('click', () => createModal(student)); anchor.innerText = student.last_name; li.appendChild(anchor); studentList.appendChild(li); }); //... });

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

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