简体   繁体   中英

Add Content to Individual Cells of Dynamically Created Table

I'm working on a Duda website widget (similar to a wordpress plugin) which will allow the user of the website editor to dynamically create a table. So far, I have the ability to create the table down, but I am stuck on allowing the user to add content to the individual cells.

Here is what I have so far:

function tableCreate(){
//get this element ID
var a = $(element).attr('id');
// pass Id to getElement
var e = document.getElementById(a);
//create table element
var b = document.createElement('table');
//user input for table width
var c = data.config.tW1;
//user input for number of rows
var d = parseInt(data.config.rowSelection1);
//user input for number of 
var f = parseInt(data.config.columnSelection1);

//create table dependent on user input
b.style.width  = (c + '%');
//b.style.height = 
b.style.border = '3px solid green';
//i = rows j = columns
for(var i = 0; i < d; i++){
    var tr = b.insertRow();
    for(var j = 0; j < f; j++){
        if(i == d && j == f){
            break;
        } else {
            var td = tr.insertCell();
            td.appendChild(document.createTextNode('Table Cell'));
            td.style.border = '1px solid black';

        }
    }
}
e.appendChild(b);
}
tableCreate();

I can retrieve user input. For example lets say I added a text input, I assign a variable to the input for the user (textInput1), I retrieve that by using data.config.textInput1.

There is an easier way to let the user edit the table (assuming HTML 5 browser): You can just add contentEditable=contentEditable to your table

<table contenteditable="contenteditable">
<tr>
<td>a</td><td>b</td>
</tr>
<tr>
<td>c</td><td>d</td>
</tr>
</table>

I found a solution. First the editor allows for user input of unordered lists. The trick is to take their list and convert it into a table. You can have them designate which columns they want by splitting words with any symbol you want. I used a ,

heres a jsfiddle - https://jsfiddle.net/zqmku0n4/1/

<ul id='my_oddly_formatted_list1' class='userList'>
    <li > Column A,Column B,Column C, Column D</li>
    <li>Value A1,Value B1,Value C1,</li>
    <li>Value A2,Value B1,Value C2,</li>
    <li>Value A3,Value B1,Value C3,</li>
    <li>Value A4,Value B1,Value C4, Value D2</li>
    <li>Value A4,Value B1,Value C4,</li>
    <li>Value A4,Value B1,Anything,</li>
    <li>Value A4,Value B1,Value C4,</li>
    <li>Value A4,Value B1,Value C4,</li>
</ul>




jQuery.fn.tableCreate = function(){
    return this.each(function() {
    //get this element ID (don't need for example)
    //var a = $(element).attr('id');
    // pass Id to getElement
    //var e = document.getElementById(a);

    //create table element
    var b = $('<table>');
    //b.css('background-color', 'orange')
    //b.style.border = '3px solid green'; // this can be an input */
    //user input for table width
    var c = '100%';
    b.css('width', c)
    console.log(c);
    //user input for number of rows
    //var d = parseInt(data.config.rowSelection1);
    var d = $('<tbody>');
    //user input for number of 
    //var f = parseInt(data.config.columnSelection1);  


    //create table dependent on user li input
        $(this).find('li').each(function(i){
        var f = $(this).html().split(',');
      if (i == 0) { 
            var g = $('<thead>')
          var h = 'orange'
         /*  g.style.backgroundColor = (h); */
          var j = $('<tr>')
          $.each(f, function(k) {
                j.append($('<th>').html(f[k]));
          });
          b.append(g.append(j));
              d.append(j);
         } else {
         var j = $('<tr>');
         $.each(f, function(k){
            j.append($('<td style="border:1px solid black; padding:10px;  ">').html(f[k]));
         });
         d.append(j);
         }
      });
      $(this).after(b.append(d)).remove();
    });
 };

$('ul.userList').tableCreate();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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