construct Datatable ajax response programmatically

construct Datatable ajax response programmatically

malpautimalpauti Posts: 8Questions: 4Answers: 1
edited October 2017 in Free community support

I'm attempting to recreate the following in PHP dynamically:

   Editor::inst( $db, 'user_to_groups')
       ->field(
            Field::inst( 'user_to_groups.user' ),
            Field::inst( 'user_to_groups.group' )
            ->options( Options::inst()
              ->table('groups')
              ->value('ID')
              ->label('groupname')
        )
        ->validator( 'Validate::dbValues' ),
        Field::inst( 'groups.groupname' )  
       )
       ->where('user',25)
      ->leftJoin( 'groups', 'groups.id', '=', 'user_to_groups.group' )
        ->process($_POST)
        ->json();

So far, my attempt has yielded:

   $editor = new Editor( $db, $tablename, $tablekey );
   $editor->fields(new Field("user_to_groups.user")); 
   $editor->fields(new Field("user_to_groups.group")); 
   $editor->fields(Field("user_to_groups.group"))->options( 'groups','ID','groupname' ); 
   $editor->fields(Field("user_to_groups.group"))->validator( 'Validate::dbValues' ); 
   $editor->fields(new Field("groups.groupname")); 
   $editor->where('user','25');
   $editor->leftJoin( 'groups', 'groups.id', '=', 'user_to_groups.group' ); 
   $editor->process( $_POST )     ->json();

I think the first 3 lines are correct. Can you please provide guidance on correct syntax for setting options, validator, and joins?

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

Answers

  • Tester2017Tester2017 Posts: 145Questions: 23Answers: 17
    edited October 2017

    My answer is based on using DataTables and Editor client side. Server-side requires a bit more work.

    You are free to make your own server part. You only have to respect 2 things:

    1. the requests from client side (JSON normally) and
    2. the response you are giving from the server back to the client.

    Receiving request from the clients can be separated in 2 kinds:

    1. CRUD requests
    2. Uploads

    Regarding the first, there are 4 options:

    1. action = nothing -> read request - return all fields of all rows requested
    2. action = delete -> delete request - return an empty JSON when success or a JSON response if something was not going as prospected.
    3. action = update -> update request - return the values of the updated records
    4. action = create -> create request - return the value of the record

    It may look a little bit complex, but believe me, as soon as you understand how the things are working it is easy going and giving a lot more flexibility in validating input, using multiple languages etc...

    I can give you some examples if you need, but you have to understand that I am using the CodeIgniter Framework and all my work was done inside this framework. I can not give examples of other frameworks.

    Here you can find more information about the data used between client and server: https://editor.datatables.net/manual/server

    Here an example of how to process AJAX requests:

        /*  This function is called by an AJAX Call from JavaScript groups.js. */
        function ajax()
        {
            /*  Call the appropriate CRUD / imageUpload function as requested by the user */
            if ( ! isset($_POST['action']))
            {
                /*  Read the records. */
                $this->Group_model->read();
            }
            else
            {
                switch ($_POST['action'])
                {
                    case 'create':
                        /*
                            Prepare data, so we can use the same array through all our functions
                            and the model.
                        */
                        $this->prepare_data('create');
                        /*
                            Validate the inputFields.
                        */
                        if( ! $this->fields_valid())
                        {
                            /*
                                One or more fields did not pass the validation rules.
                                We will send a notification of this back to the user.
                            */
                            echo json_encode($this->_field_errors);
                        }
                        else
                        {
                            /*  InputFields correctly validated, lets create the record now. */
                            $this->Group_model->create($this->_post_data);
                        }
                        break;
    
                    case 'edit':
                        /*
                            Prepare data, so we can use the same array through all our functions
                            and the model.
                        */
                        $this->prepare_data('edit');
                        /*
                            Validate the inputFields.
                        */
                        if( ! $this->fields_valid())
                        {
                            /*
                                One or more fields did not pass the validation rules.
                                We will send a notification of this back to the user.
                            */
                            echo json_encode($this->_field_errors);
                        }
                        else
                        {
                            /*
                                InputFields correctly validated, now we can process the
                                updates.
                            */
                            $this->Group_model->update($this->_post_data);
                        }
                        break;
    
                    case 'remove':
                        /*
                            Prepare data, so we can use the same array through all our functions
                            and the model.
                        */
                        $this->prepare_data('remove');
    
                        /*  Process the record or records to be deleted. */
                        $this->Group_model->delete($this->_post_data);
                        break;
    
                    case 'upload':
                        /*
                            Process the upload of an Image.
                         */
                        $this->Group_model->uploadImages();
                        break;
                }
            }
        }
    
  • malpautimalpauti Posts: 8Questions: 4Answers: 1

    My specific challenge is the creation of the DataTables Editor object server-side in php. Appreciate your prompt response but it has not helped.

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin

    The nesting is slightly wrong - for example:

    $editor->fields(Field("user_to_groups.group"))->validator( 'Validate::dbValues' );
    

    Should be:

    $editor->fields(new Field("user_to_groups.group")->validator( 'Validate::dbValues' ));
    

    i.e. the validator belongs to the Field. Also you need to use new for the field like in the other ones.

    Its perhaps a little easier to see with newlines:

    $editor->fields(
      new Field("user_to_groups.group")
        ->validator( 'Validate::dbValues' )
    );
    

    Allan

This discussion has been closed.