Search code examples
insertattributesrowfootable

FooTable.js Insert Row(s) with input field... unique name for array


I have a fairly large form that has has a lot of basic form inputs, nothing fancy. But the users are in locations with spotty internet so the form has to be filled out all at once and then submitted.

I'm adding two sections to this form. One is a task manager type of widget. Another is a basic CRUD table without the ability to Delete.

Both the task manager and the CRUD table are using Footable and have rows foreach'd to pre-populate data.

Obviously I need to POST these as arrays. So using the Name attribute I am successfully doing so like this.

  • name="need[ Echo'ing ID ][id]"
  • name="need[ Echo'ing ID ][store_number]"

  • name="task[ Echo'ing ID ][id]"

  • name="task[ Echo'ing ID ][assigned]"}

This is successfully posting the array and working good.

Sorry for my long explintion. I know most like more info then less.

My issue.

When I insert a new row with footable I can not find where I can make a unique name key for each input field, where I have the echo'ing id above. There are roughly 8 rows on each insert. I can make the first insert populate unique but all others fail... Anyone have any help?


Solution

  • I was able to solve this by adding an onlick number gen to the insert function like so.

    $('.add-row').click(function(e) {
                e.preventDefault();
                var t = Math.floor(100000 + Math.random() * 900000);
                //get the footable object
                var footable = $('table').data('footable');
    
                //build up the row we are wanting to add
                var newRow = `<tr><td><input type="text" type="" name="need[`+t+`][id]" value=""></td>...
    

    bmartin solved helped on this question. Javascript random number in function onclick