Why isn't my checkbox working.

Why isn't my checkbox working.

MickBMickB Posts: 103Questions: 25Answers: 2

Not sure what I am doing wrong here, the checkbox seems to work but is never displaying as checked in the table. Pretty sure this is something simple somewhere.

        //setup the editor

        editor = new $.fn.dataTable.Editor({
                 "ajax": {
                "url": "/locationsAjax",
                "type": "POST",
                data: {table: "locations"}
            },

   //setup the Datatable
        table = $('#generic_table').DataTable({ //took the var off to intentionally make it global

            "ajax": {
                "url": "/locationsAjax",
                headers: {
                    'X-CSRF-TOKEN': 'qbb2Awfm4XI3zUf7Qg8Kj4WyAZjNhPWBAmMw27bJ'
                },
                "type": "POST",

                "data": function(d){
                    d.table = "locations";
                                    },

            },
            "columns": [
                {
                    data: null,

                    defaultContent: '',
                    className: 'select-checkbox',
                    orderable: false
                },

                
      { 'data': 'view_printing_locations.username',editField:'locations.user_id'},
      
      {
                        data:   'locations.auth_required',
                        render: function ( data, type, row ) {
                            if ( type === 'display' ) {
                                return "<input type='checkbox' class='editor-locations.auth_required'>";
                            }
                            return data;
                        },
                        className: 'dt-body-center'
                    }
    


            ],
            order: [1, 'asc'],
            dom: "Bfrtip",
            responsive: false,//true wont let me hide columns
            "scrollY": "600px",
            "scrollCollapse": true,
            
            "paging": false,
            select: {
                style: 'single',
                selector: 'td:first-child',
                blurable: true

            },

            buttons: [{extend:'remove', editor: editor},{extend:'create', editor: editor},{extend:'edit', editor: editor},],
            
    rowCallback: function ( row, data ) {
                    // Set the checked state of the checkbox in the table
                    $('input.editor-locations.auth_required', row).prop( 'checked', data.locations.auth_required == 1 );
                    
                },
    
        });

Here is the PHP:

 public function datatablesAjax()
    {

        $postData = $_POST;
        Editor::inst($db, 'locations')
            ->fields(
                Field::inst("locations.id"),
                Field::inst( "locations.user_id" )
                    ->options('view_printing_locations','id','username'),
                Field::inst("view_printing_locations.username"),
                Field::inst("locations.auth_required")
            )
            ->leftjoin('view_printing_locations','view_printing_locations.id','=',"locations.user_id" )
            ->process($postData)
            ->json();

    }

Does that make sense?

debug.datatables.net/ebinum

Mick

Answers

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Answer ✓

    Change:

    class='editor-locations.auth_required'

    To be class='editor-locations-auth_required' and also update the jQuery selector in the rowCallback to reflect that change. The problem is that jQuery is trying to select an element which two different classes. You could escape the . in the class name, but its easier to just change the class name.

    Allan

  • MickBMickB Posts: 103Questions: 25Answers: 2

    Brilliant! I never would have spotted that.

    Thanks for your help.

    Mick

  • MickBMickB Posts: 103Questions: 25Answers: 2

    I am now stuck because I have a description field in my locations table.

    I want this to reflect the view_printing_locations.username field (so it is in the database which belongs to this application.)

    Does that make sense? I want the user to select a view_printing_locations.username but then save the user_id and username (which are in my user manager database)in my locations table (in this applications database).

  • MickBMickB Posts: 103Questions: 25Answers: 2
    edited August 2016

    ajax.data?

    maybe preEdit, just to copy the value into the other field:

    ->on('preEdit', function ($e, $id, $values) {

                $e->field('locations.description')->setValue($values['view_printing_locations']['username']);
    
            })
    
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    Just to check my understanding - you display the username to the end user, which they select from. Then you want to store both the username and the user id? Why not save just the user id (since the user name can be looked use from the user id - that redundant information isn't being stored)?

    However, if that is what you need, you could listen for the change event on the username field and update the user id as appropriate:

    editor.field('view_printing_locations.username').input().on( 'change', function () {
      editor.field('view_printing_locations.user_id').val( ... );
    } );
    

    I'm not sure where you will get the user_id value from? Does that exist in a look up object somewhere?

    Regards,
    Allan

  • MickBMickB Posts: 103Questions: 25Answers: 2

    View_printing_locations pulls the username and id from a separate database. I am trying to avoid joining to the other db in other parts of this application. (Is that a good idea or not?).

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Answer ✓

    Personally I would say no. Joins are very fast (as long as they are indexed) so there should be no concern about performance. And it means that you have redundant data, so updating can become very difficult. Imagine you have 5 tables that store the user name, you need to update them all if you want to simply change the user name, rather than having a single point of reference.

    Allan

  • MickBMickB Posts: 103Questions: 25Answers: 2

    Thanks for your advice. It just felt a bit odd to keep joining to a different database.

    I have ditched the username from this table and am adding joins in the other parts of my application.

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Answer ✓

    I missed that point - when you say a separate database, do you mean a different server? Or a different db inside the same server? The later is fine, a different server though is a different matter.

    It would be worth profiling your queries to make sure they are acceptable.

    Alla

  • MickBMickB Posts: 103Questions: 25Answers: 2

    Same server. Got it all working now.

    I would be surprised if the locations table ever reached a dozen records.

    Cheers,

    Mick

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Answer ✓

    For a small table like that, it should be like greased lightning then :smile:

    Allan

This discussion has been closed.