简体   繁体   English

添加 <select>代替<td>

[英]Adding <select> inside of <td>

I have this code that creates a table with 7 columns. 我有这段代码创建了一个包含7列的表。

The table returns like this: 该表返回如下:
[1][2][3][4][5][6][7] << headers [1] [2] [3] [4] [5] [6] [7] <<标头
[1][2][3][4][5][6][7] << values [1] [2] [3] [4] [5] [6] [7] <<值

Where the 7th column is an <a> button inside of a <td> . 其中第七列是<td>内的<a>按钮。

I'm trying to figure out how to add a <select> with <option>'s inside of the 6th columns <td> . 我试图弄清楚如何在第6列<td>内的<option>'s内添加<select> <td>

  //create table
  var artistFileValues = artistFile.getDataRange().getValues();
  var isHeader = true;
  var table = "<table id='table-noValues'>\n";
  var caption = "<caption>" + name + "</caption>\n";

  table += caption;

  for (var i = 0; i < artistFileValues.length; i++)
  {
    table += "<tr>\n";

    for (var j = 0; j < artistFileValues[i].length; j++)
    {
      if (isHeader)
      {
        table += "<th>" + artistFileValues[i][j] + "</th>\n"; //headers
        if (j == artistFileValues[i].length - 1) //if last header in row, add another cell with 'add' button
        {
          table += "<th style='text-align: center;'>" + "<a id='addButton' onclick=\x22addRow(this)\x22>" + "&plus;" + "</a>" + "</th>\n"; //'+' button
        }
      }
      else
      {
        table += "<td contenteditable='false'>" + artistFileValues[i][j] + "</td>\n"; //values
        if (j == artistFileValues[i].length - 1) //if last data in row, add another cell with 'delete' button
        {
          table += "<td style='text-align: center;'>" + "<a id='deleteButton' onclick=\x22deleteRow(this)\x22>" + "&times;" + "</a>" + "</td>\n"; //'x' button
        }
      }
    }
    isHeader = false;
    table += "</tr>\n";
  }
  table += "</table>";
  table += "<button id='calculateButton' onclick='calculate()'>" + "Calculate" + "</button>"; //add calculate button to bottom of table
  return table;

I'm assuming I'd have to implement an if statement or a for loop that counts to 5 and then adds the inside of the 6th . 我假设我必须实现一个if语句或一个计数为5的for循环,然后在6th的内部添加。 I'm not really sure what the best way to do this is, or if this is even the right way to do it. 我不太确定执行此操作的最佳方法是什么,或者这是否正确。

But any help would be appreciated. 但任何帮助将不胜感激。

Just figure out where you want to put it and given the below function your optionscsv string and valuescsv string and it will generate all of the tags for the select and options opening and closing tags. 只需弄清楚要将其放置在何处,并为以下功能指定optionscsv字符串和valuescsv字符串,它将为select和options的打开和关闭标签生成所有标签。

function mySelect(optionscsv,valuescsv)
{
  var optionscsv = (typeof(optionscsv) !== 'undefined')?optionscsv : null;
  var valuescsv = (typeof(valuescsv) !== 'undefined')?valuescsv : null;
  var s='<select>';
  if(valuescsv && optionscsv)
  {
    var optionsA = optionscsv.split(',');
    var valuesA = valuescsv.split(',');
    if(optionsA.length == valuesA.length)
    {
      for(var i = 0; i < optionsA.length; i++)
      {
        s += '<option value="' + valuesA[i] + '">' + optionsA[i] + '</option>';
      }
      s += '</select>';
    }
    else
    {
      s ='';
    }

  }
  else
  {
    s = '';
  }
  return s;
}

And below I took a guess at where you might wish to put it and how you could do it. 在下面,我猜测了您可能希望将其放置在何处以及如何做。 But I might be wrong so come back for more help if needed. 但是我可能是错的,因此如有需要,请回来寻求更多帮助。

for (var j = 0; j < artistFileValues[i].length; j++)
    {
      if (isHeader)
      {
        table += "<th>" + artistFileValues[i][j] + "</th>\n"; //headers
        if (j == artistFileValues[i].length - 1) //if last header in row, add another cell with 'add' button
        {
          table += "<th style='text-align: center;'>" + "<a id='addButton' onclick=\x22addRow(this)\x22>" + "&plus;" + "</a>" + "</th>\n"; //'+' button
        }
      }
      else
      {
        if(j!==5)//I'm guessing this is the sixth column
        {
            table += "<td contenteditable='false'>" + artistFileValues[i][j] + "</td>\n"; //values
        }
        else
        {
            var s = mySelect(optionscsv,valuescsv);
            table += "<td contenteditable='false'>" + artistFileValues[i][j] + s + "</td>\n"; //values
        }
        if (j == artistFileValues[i].length - 1) //if last data in row, add another cell with 'delete' button
        {
          table += "<td style='text-align: center;'>" + "<a id='deleteButton' onclick=\x22deleteRow(this)\x22>" + "&times;" + "</a>" + "</td>\n"; //'x' button
        }
      }
    }

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

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