Perform ajax call before remove

Perform ajax call before remove

chris.cavagechris.cavage Posts: 46Questions: 12Answers: 0

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

  • allanallan Posts: 64,798Questions: 1Answers: 10,728 Site admin

    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

  • chris.cavagechris.cavage Posts: 46Questions: 12Answers: 0
    edited August 2016

    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
    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();

    <?php > ``` ?>

    I'm assuming I have to work in this file?

    Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

  • allanallan Posts: 64,798Questions: 1Answers: 10,728 Site admin
    edited August 2016

    Yes that one. At line 20 you could add something like:

    if ( Editor::action( $_POST ) === Editor::ACTION_DELETE ) {
      // ... do check
    }
    

    Allan

  • chris.cavagechris.cavage Posts: 46Questions: 12Answers: 0

    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.

    if ( Editor::action( $_POST ) === Editor::ACTION_DELETE ) {
    
        $delete_id = ID_I'M_TRYING_TO_DELETE
    
       "SELECT * FROM invoice_accessories WHERE id = ' . $delete_id . '"
    
        if (THERE'S A ROW PRESENT FROM QUERY)  {
        $deny = array("error" => "You cannot delete this because it's being used somewhere in the application!");
        echo json_encode($deny);
        exit;
        }
    
    }
    

    Thanks a lot.

  • chris.cavagechris.cavage Posts: 46Questions: 12Answers: 0

    I'm close:

    if ( Editor::action( $_POST ) === Editor::ACTION_DELETE ) {
        
        $RAW_SQL_QUERY= "SELECT * FROM invoice_accessories WHERE types_accessories_id = 'GET_ID'";
     
        $r=$db->sql($RAW_SQL_QUERY)->fetchAll();
        
        if ($r) { 
            
            $deny = array("error" => "You cannot delete this because it's being used somewhere in the application!");
            echo json_encode($deny);
            exit;
            
        };
        
    }
    
    

    How do I get the id of the field to be deleted? Then I'm golden!

  • allanallan Posts: 64,798Questions: 1Answers: 10,728 Site admin
    Answer ✓

    How do I get the id of the field to be deleted?

    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 use foreach (or similar) to loop over it to get the key(s).

    Allan

  • chris.cavagechris.cavage Posts: 46Questions: 12Answers: 0

    Thanks, Allan. I got it to work:

    //get number from row_? key values
        $nums = [];
        foreach ($_POST['data'] as $key => $val) {
            $nums[] = preg_replace('@[^\d]@', '', $key);
        };
    
This discussion has been closed.