How to get the row added by ajax in the right place? I have a table form created from a custom entity:
  public function buildForm(array $form, FormStateInterface $form_state, $contract = NULL)
  {

    $form['subscriptions'] = [
      '#type'       =&gt; 'table',
    ];
    $form['subscriptions']['#header'] = [some stuff here];
//  here is the query to retrieve data from entity...  
    $results = db_query($query);
    foreach ($results as $key =&gt; $value) {
      $myKey = $key * 10;
      $form['subscriptions'][$myKey]['somefield'] = ['#markup' =&gt; $value-&gt;designation];
      $form['subscriptions'][$myKey]['anotherfield'] = [
        '#type'          =&gt; 'number',
        '#default_value' =&gt; $value-&gt;$sTemp,
      ];
      $form['subscriptions'][$myKey]['addRow'] = [
        '#type'  =&gt; 'submit',
        '#name'  =&gt; $myKey,
        '#value' =&gt; '+',
        '#ajax'  =&gt; [
          'callback' =&gt; '::ajaxAddRow',
          'wrapper'  =&gt; 'my_form_wrapper',
        ],
      ];
    }

    $form['#prefix'] = '&lt;div id=&quot;my_form_wrapper&quot;&gt;';
    $form['#suffix'] = '&lt;/div&gt;';

    $form['submit'] = [
     '#type'  =&gt; 'submit',
     '#name'  =&gt; 'submit',
     '#value' =&gt; $this-&gt;t('Submit'),
    ];
  return $form;
  }

The ajax callback:
  public function ajaxAddRow(array &amp;$form, FormStateInterface $form_state)
  {
    $myKey = $form_state-&gt;getTriggeringElement()['#name'];
    $form['subscriptions'][$myKey + 5] = $form['subscriptions'][$myKey];
    return $form;
  }

The submitForm:
  public function submitForm(array &amp;$form, FormStateInterface $form_state)
  {
    if ($form_state-&gt;getTriggeringElement()['#name'] == 'submit') {
        do some stuff then exit the form
    }
    else {
      $form_state-&gt;setRebuild();
    }
  }

When I click on '+' in some row, the form is displayed again with a copy of the row added to the table.
But the copy is added at the end of the table.
How can I have the copy added just after the original one?
I finally made it work this way:
  public function ajaxAddRow(array &amp;$form, FormStateInterface $form_state)
  {
    $myKey = $form_state-&gt;getTriggeringElement()['#name'];
    $formBis = [];
    foreach ($form as $key =&gt; $value) {
      switch ($key) {
        case 'subscriptions':
          foreach ($form['subscriptions'] as $key2 =&gt; $value2) {
            $formBis['subscriptions'][$key2] = $form['subscriptions'][$key2];
            if (is_numeric($key2)) {
              if ($key2 == $myKey) {           &lt;&lt; Here is the row to be copied
                $keyfornewrow = $key2 + 5;
                $formBis['subscriptions'][$keyfornewrow] = $form['subscriptions'][$key2];
                foreach ($formBis['subscriptions'][$keyfornewrow] as $key3 =&gt; $value3) {
                    &lt;&lt; some code here to initialize content of copied row &gt;&gt;
                }
              }
            }
          }
          break;
        default:
          $formBis[$key] = $form[$key];
      }
    }
    return $formBis;
  }