have a table AssetsAssignations, with hundreds of rows. In some cases, the user needs to select many rows with a checkbox, and change the “status” for all of them together.
In my controller, I have this function
public function editMultiple()
{
$assetStatuses = $this->AssetsAssignations->AssetStatuses->find(‘list’);
$this->paginate = [
‘contain’ => [‘Assets’, ‘AssetStatuses’, ‘Clients’, ‘Rooms’],
‘sortWhitelist’ => [
‘Assets.model_number’,
‘Assets.serial_number’,
‘AssetStatuses.name’,
‘Clients.last_name’,
‘Rooms.name’,
]
];
$assetsAssignations = $this->request->data;
$assetsAssignations_ids = array();
foreach($assetsAssignations as $a){
$assetsAssignations_ids[$a['id']] = $a['id'];
$this->AssetsAssignations->updateAll(
array('AssetAssignation.id' => $assetsAssignations_ids)
);
$this->Session->setFlash(__('Statsus is updated for the selcted entries!'));
}
debug($assetsAssignations);
$query = $this->AssetsAssignations->find()
->contain(['Assets', 'AssetStatuses', 'Clients', 'Rooms']);
$filter = $this->Filter->prg($query);
$assetsAssignations = $this->paginate($filter, ['maxLimit' => 10000, 'limit' => 10000]);
$this->set(compact('assetsAssignations', 'assetStatuses'));
$this->set('_serialize', ['assetsAssignations']);
}
In my edit_multiple.ctp, I use a javascript to filter the data. And I put this code:
<thead> <tr> <th>Select</th><th>Model Number</th><th>Serial Number</th><th>Room</th><th>Client</th><th>Status</th> </tr> </thead> </thead> <?php foreach ($assetsAssignations as $assetsAssignation): ?> <tr> <td><input name="data[AssetsAssignations][id][]" value="<?= $assetsAssignation->id ?>" id="AssetsAssignationsId1" type="checkbox"></td> <td><?= $assetsAssignation->has('asset') ? $assetsAssignation->asset->model_number : '' ?></td> <td><?= $assetsAssignation->has('asset') ? $assetsAssignation->asset->serial_number : '' ?></td> <td><?= $assetsAssignation->has('room') ? $assetsAssignation->room->name : '' ?></td> <td><?= $assetsAssignation->has('client') ? $assetsAssignation->client->last_name . ', ' . $assetsAssignation->client->first_name: '' ?></td> <td><?= $assetsAssignation->has('asset_status') ? $assetsAssignation->asset_status->name : '' ?></td> </tr> <?php endforeach; ?> </table> <legend><?= __('') ?></legend> </div> <?= $this->Form->create($assetsAssignation) ?> <fieldset> <div class="row"> <div class="col-xs-3"><?= $this->Form->input('asset_status_id', ['options' => $assetStatuses, 'empty' => true, 'label' => __('Change Status To')]) ?></div> </div> </fieldset> <?= $this->Form->button(__('Submit')) ?> <?= $this->Form->end() ?>
When I debug the result, checking 3 entries, I get this:
[
‘data’ => [
‘AssetsAssignations’ => [
‘id’ => [
(int) 0 => ‘411’,
(int) 1 => ‘413’,
(int) 2 => ‘415’
]
]
],
‘asset_status_id’ => ‘3’
]My question is: How to pass the selected row IDs to the “Submit” button after selecting the checkboxes ?
Thanks in advance.