8 – AJAX ReplaceCommand runs as expected only in the first iteration

Expected performance:

  1. User triggers the remove element button.
  2. Update database program.
  3. The form is reconstructed with updated information from the database.
  4. AJAX replaces the old information with new information from buildForm.

Actual output:
The expected output only occurs in the first iteration. If the user activates the Delete element button in another element. The database will be updated. But the AJAX does not run.

I am still learning the Drupal AJAX system, I do not understand why this works for a single iteration.

buildForm:

// get information from the shopping cart table
$ cart = new  Drupal  pps_shopping_cart  Controller  ShoppingCart ();
$ items = $ cart-> getShoppingCartItems ();

// show order of review
yes ($ step === 1)
{
// generate table structure
$ form['table'] = [
                '#type' => 'table',
                '#header' => [
                    $this->t("Image"),
                    $this->t("Manufacturer Part Number"),
                    $this->t("Order QTY"),
                    $this->t("Unit Price"),
                    $this->t("Remove")
                ],
& # 39; # empty & # 39; => $ this-> t ("You do not have any items in your cart.")
];

$ index = 0;
foreach ($ items as $ record)
{
$ imgURL = $ cart-> getImageUrlByPartNumber ($ record-> pno);
$ form['table'][$index]['image']    =

The
& # 39; # type & # 39; => & # 39; container & # 39 ;,
& # 39; # markup & # 39; => "{$ record-> pno} & # 39; width = & # 39; 200px & # 39; height = & # 39; 200px & # 39;> "
];

$ form[ 'label',
                    '#title' => $record->pno
                ];

$ form['table'][$index]['pno']['reference']    = [
                    '#type' => 'hidden',
                    '#value' => $record->pno
                ];

$ form['table'][$index]['qty']    = [
                    '#type' => 'number',
                    '#default_value' => $record->qty,
                    '#min' => 0,
                    '#max' => $cart->getCustomerAvlQty($record->pno)
                ];

$ form['table'][$index]['price']    = [
                    '#type' => 'label',
                    '#title' => '$' . number_format($record->price, 2)
                ];

$ form['table'][$index]['remove']    = [
                    '#type' => 'submit',
                    '#name' => $record->pno,
                    '#value' => $this->t("Remove"),
                    '#validate' => ['::validateRemoveItem'],
& # 39; # submit & # 39; => ['::submitRemoveItem'],
& # 39; # ajax & # 39; => [
                        'callback' => [$this, 'ajaxRemoveItem'],
& # 39; wrapper & # 39; => & # 39; edit-table & # 39;
]];

$ index ++;
}

yes ($ index> 0)
{
$ form['actions'] = ['#type' => 'actions'];
$ form['actions']['review']    = [
                    '#type' => 'submit',
                    '#value' => $this->t("Review Order"),
                    '#validate' => ['::validateReview'],
& # 39; # submit & # 39; => ['::submitReview']
                ];

$ form['actions']['save']    = [
                    '#type' => 'submit',
                    '#value' => $this->t("Save"),
                    '#validate' => ['::validateSaveOrder'],
& # 39; # submit & # 39; => ['::submitSaveOrder'],
& # 39; # ajax & # 39; => ['callback' => [$this, 'ajaxSaveOrder']]];
}
}

submitRemoveItem:

    $ cart = new  Drupal  pps_shopping_cart  Controller  ShoppingCart ();

$ cart-> removeFromCart ($ form_state-> getTriggeringElement ()['#name']);
$ form_state-> setRebuild ();

ajaxRemoveItem:

                                $ answer = new AjaxResponse ();
$ answer-> addCommand (new ReplaceCommand (& # 39; # edit-table & # 39 ;, $ form['table']));
$ response-> addCommand (new OpenModalDialogCommand ($ this-> t ("Removing Item from Cart"), $ this-> t ("@ item: deleted from your shopping cart!",['@item'=>$form_state->getTriggeringElement()['@item'=>$form_state->getTriggeringElement()['@item'=>$form_state->getTriggeringElement()['@item'=>$form_state->getTriggeringElement()['#name']])));
returns $ answer;