Perform ajax call before remove
Perform ajax call before remove

Hi,
In my dataTables Editor, I'd like to perform a check on my db. If a separate record is present based on a query which I will write, I want to NOT allow the delete/remove of the chosen record from the Editor.
I basically want to call an ajax function once the user clicks the 'delete' button. Then proceed with the delete action or cancel it based on the response of that ajax.
I am just wondering how I should go about this?
Thanks.
This question has an accepted answers - jump to answer
Answers
Rather than an Ajax call, can you just integrate the check straight into the existing server-side script? You could check to see if Editor is submitting a delete action (
action=remove
) and if so perhaps whatever check is required. If the user isn't allowed to delete the row return JSON such as{"error": "Can't delete this row"}
.There is the
preRemove
server-side event, but that isn't yet cancellable. That will be coming in 1.6.Allan
Thanks Allan. I will work on your suggestion. Which page though catches the action?
Here's one of the pages.php that I made for a table:
```php
<?php /*
* Editor server script for DB table users
* Created by http://editor.datatables.net/generator
*/
// DataTables PHP library and database connection
include( "../added-assets/plugins/editor/php/DataTables.php" );
// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Upload,
DataTables\Editor\Validate;
$company_id = $_GET['company_id'];
// Build our Editor instance and process the data coming from _POST
<?php > ``` ?>Editor::inst( $db, 'appt_reasons', 'id' )
->fields(
Field::inst( 'reason' )
->validator( 'Validate::notEmpty' ),
Field::inst( 'description' ),
Field::inst( 'color' )
->validator( 'Validate::notEmpty' ),
Field::inst( 'textColor' )
->validator( 'Validate::notEmpty' ),
Field::inst( 'company_id' )
->validator( 'Validate::notEmpty' )
)
->where( $key = "company_id", $value = $company_id, $op = "=" )
->process( $_POST )
->json();
I'm assuming I have to work in this file?
Yes that one. At line 20 you could add something like:
Allan
Got it, thanks. Working on it now. With your classes, how can I perform this query: "SELECT * FROM invoice_accessories WHERE id = ID_I'M_TRYING_TO_DELETE"
if there is at least one row present, then don't delete it.
Thanks a lot.
I'm close:
How do I get the id of the field to be deleted? Then I'm golden!
The client / server communication that Editor uses is documented here. Basically the id is the key in the
data
array that is submitted, so you'll need to useforeach
(or similar) to loop over it to get the key(s).Allan
Thanks, Allan. I got it to work: