Trouble with checkbox.

Trouble with checkbox.

jquijadojquijado Posts: 24Questions: 6Answers: 0

Hi, everyone.
Im trying to use a checkbox set to indicate in the editor form a set of optios which can be individually check or unchecked. Everything is ok if I define the options in the checkbox field definition. However, if I try to create the options dinamically, by using the preOpen event, all the options are shown unchecked.... just the first time I open the editor. The second, third etc times, the options are shown properly cheked or unchecked, depending on the data base contents. It only fails the first time I open the editor.

The field is defined like this:

{
    label: 'Especialidades:', 
    name: 'id_especialidades[]', 
    type: 'checkbox', 
    fieldInfo: 'Selecciona las especialidades'
},

The options are loaded dinamically by the preOpen event, like this:

objetoEditor.on('preOpen', function(e, mode, action){
    if (action == "remove") return;
    $.ajax({
        url:"leer_especialidades_editor_08.php", 
        async:false,
        dataType: "JSON",
        complete:function(datosRecibidos) {
            listaDeEspecialidades = datosRecibidos.responseText;
        }
    });
    var matrizDeEspecialidades = JSON.parse(listaDeEspecialidades);
    objetoEditor.field('id_especialidades[]').update(matrizDeEspecialidades);
});

Ant the field in the Datatables is defined like this:

{"data": 'id_especialidades[]'}

When I open the editor, the preOpen event gives the right JSON response from the first time, like this:

0:{label: "Bootstrap", value: "9"}
1:{label: "CSS 3", value: "3"}
2:{label: "HTML 5", value: "1"}
3:{label: "JavaScript 6", value: "2"}
4:{label: "jQuery", value: "7"}
5:{label: "jQuery Mobile", value: "10"}
6:{label: "jQueryUI", value: "8"}
7:{label: "MySQL", value: "6"}
8:{label: "PHP", value: "5"}
9:{label: "SCSS", value: "4"}

The first time I try to edit a user profiles, they appear all the checkboxes unchecked. If I close the form and reopen it, the checkboxes appear properly checked or unchecked, on the real sate in the MySQL table.

Why does it not work just the first time?

Thanks everyone.

Replies

  • rf1234rf1234 Posts: 2,944Questions: 87Answers: 416

    Interesting approach! To be honest I don't have a solution using your approach ... but maybe you can use an alternative approach like in this example:
    https://editor.datatables.net/examples/advanced/joinArray.html
    Here the options are selected immediately through an mjoin and not in a separate step on 'preOpen'.
    I attached the data model of the example to make it easier to understand.

  • rf1234rf1234 Posts: 2,944Questions: 87Answers: 416

    Found another example of options selection in my own code. This example doesn't even need an mjoin. However: If your options are more complex, e.g. coming from multiple tables as in this example, it is to be noted that you cannot do a ->leftJoin in ->options. It is simply not available there. In this case you would need to do an old fashioned 1990's style join ... (i.e. target multiple tables and join through the where clause of your statement).

    Editor::inst( $db, 'rfa_has_rfp' )
            ->field(
                Field::inst( 'rfa_has_rfp.rfa_id' )->set(Field::SET_CREATE),
                Field::inst( 'rfa_has_rfp.rfp_id' )->set(Field::SET_CREATE)
                                                   ->setValue( $_POST['rfp_id'] ),
                Field::inst( 'rfa_has_rfp.approver_id' )
                    ->options( Options::inst()
                        ->table('user, govdept_has_user')
                        ->value('user.id')
                        ->label( array('user.title', 'user.acad',
                          'user.firstname', 'user.lastname', 'govdept_has_user.role') )
                        ->render( function ( $row ) {
                            return $row['user.title'].' '.$row['user.acad'].' '
                                   .$row['user.firstname'].' '.$row['user.lastname']
                                   .' ('.$row['govdept_has_user.role'].')';
                        } )
                        //where clause MUST be a closure function in Options!!!
                        //the current user may not choose himself as approver
                        ->where( function($q) {                            
                            $q ->where('user.id',  $_SESSION['id'], '!=' );
                            $q ->where('govdept_has_user.govDept_id', $_POST['rfp_govDept_id']);
                            //the fourth parameter "false" makes sure that user.id is not
                            //escaped as a string; left join does NOT work with options
                            //hence we do the 1990's style join with where clause
                            //and two tables targeted
                            $q ->where('govdept_has_user.user_id', 'user.id', '=', false); //join govdept_has_user
                            $q ->where( function ( $r ) {
                                $r  ->where('govdept_has_user.role', 'Principal');
                                $r  ->or_where('govdept_has_user.role', 'Administrator');
                                $r  ->or_where('govdept_has_user.role', 'Editor');
                                $r  ->or_where('govdept_has_user.role', 'Approver');
                            } );
                        } )
                    ),                
                Field::inst( 'rfa_has_rfp.status' )->set(Field::SET_CREATE)
                                                   ->setValue( 'W' ),
    
  • jquijadojquijado Posts: 24Questions: 6Answers: 0

    Thanks a lot, friend:
    Your examples are really interesting. However there's a reason for using preOpen when I use two tables (a main one and a secondary one) and an intermediate table: If another user modifies the secondary table when my datatables is already loaded, by loading the options with preOpen I get the actual data in real time. Sincerely, I find the preOpen event really useful when several concurrents users can modify a table.

    From your code, I see you have got a real high level of knowledge on DataTables. If you are not a worker of the company yet, they should contract you.

    Thanks a lot.

  • jquijadojquijado Posts: 24Questions: 6Answers: 0

    However, the trouble is still the same. If I use preOpen, in order to load an updated copy of the secodary table, the checkboxes are not properly checked the first time I open the form, but they are properly checked if I open it a second time. I've read a lot of pages in this site, but I can't find the reason of this, or a btter event than preOpen, or a better method than update. It seems to be no alternatives.

  • allanallan Posts: 63,204Questions: 1Answers: 10,415 Site admin

    If another user modifies the secondary table when my datatables is already loaded, by loading the options with preOpen I get the actual data in real time

    I would take a slightly different approach to solve this problem. What I would suggest is that instead of using the edit button (which I guess you currently are?) use a custom button which will make a request to the server to update the data for the selected row (use row().data() to update it) and then use the Editor API to trigger editing (edit()). You could show a little "updating" animation or similar while the new data is being loaded, and that will update the data for the whole row.

    This is actually something I plan to offer as a feature in future versions of Editor.

    Allan

  • jquijadojquijado Posts: 24Questions: 6Answers: 0

    Thanks a lot, Allan.
    Once else your approach is, of course, better than mine.
    Regards.

This discussion has been closed.