Highlight row based on count of field in other table.

Highlight row based on count of field in other table.

MickBMickB Posts: 103Questions: 25Answers: 2

Hi,

What is the simplest way to highlight a row, only if it doesn't have related rows in another table?

Eg,

Losses Datatable for losses db table.
Rows highlighted if there is not a record in the recorded_faults table with a matching loss id.

I have the highlighting bit:


"createdRow": function( row, data, dataIndex ) { if ( data.recorded_faults.loss_id == null ) { $(row).addClass( 'important' ); } }

I am using Editor and the PHP Library.

Mick

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    Hi Mick,

    That you have looks fairly good to me - does that not work for you? The one thing I can see that wouldn't work with the above is that it triggers on createdRow only, which will only fire once in the lifetime of a row. You would need to use rowCallback if you wanted it to run once the data has been updated (for example, you might use Editor to assign a related row, thus the highlight should be removed, and the inverse).

    Regards,
    Allan

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

    At the moment, it is working but pulls back multiple rows from the losses table, if there are multiple records in the recorded_faults table.

    I guess the problem is my join:

     Editor::inst($db, 'losses')->where("losses.deleted_at", null)
                ->fields(
                    Field::inst("losses.id"),//always needed
                    Field::inst('products.part_no')->validator('Validate::notEmpty'),
                    Field::inst('products.description')->validator('Validate::notEmpty'),
                    Field::inst('losses.date_selected')->validator('Validate::unique')->getFormatter( 'Format::date_sql_to_format', 'd/m/Y' ),
                    Field::inst('recorded_faults.id')
                )
                ->leftjoin('products', 'products.id', '=', "losses.product_id")
                ->leftjoin('recorded_faults', 'recorded_faults.loss_id', '=', "losses.id")
                ->process($postData)
                ->json();
    

    Mick

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    Hi Mick,

    Can there be multiple recorded_faults for a single losses record? If so, the one-to-many join is going to be a better way to structure the query.

    Regards,
    Allan

  • MickBMickB Posts: 103Questions: 25Answers: 2

    Yes, nearly got this now.

    I was getting this error:

    "FatalErrorException in Bootstrap.php line 53:
    App\Libraries\DataTables{closure}(): Failed opening required '/var/www/html/selection/app/Libraries/DataTables/Editor/MJoin.php' (include_path='.:/usr/share/pear:/usr/share/php')"

    (from Laravel)

    I had to rename Mjoin.php to MJoin.php to resolve this. Is there a reason why it had a lower case J?

    Mick

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    Yes, the auto loader uses the capitalisation of the letters to reflect the path name. Editor\MJoin would be found in the Editor/M/Join.php path, while Editor/Mjoin (which the documentation should always refer to) will be in Editor/Mjoin.php.

    Allan

  • MickBMickB Posts: 103Questions: 25Answers: 2

    Not sure I understand that.

    In my Editor directory I have:
    Editor.php
    Join.php
    Mjoin.php

    In Mjoin.php.

    I now have this:


    Editor::inst($db, 'losses')->where("losses.deleted_at", null) ->fields( Field::inst("losses.id"),//always needed Field::inst('products.part_no')->validator('Validate::notEmpty'), Field::inst('products.description')->validator('Validate::notEmpty'), Field::inst('losses.date_selected')->validator('Validate::unique')->getFormatter( 'Format::date_sql_to_format', 'd/m/Y' ) ) ->leftjoin('products', 'products.id', '=', "losses.product_id") ->join( MJoin::inst( 'recorded_faults' ) ->link( 'recorded_faults.loss_id', 'losses.id' ) ->fields( Field::inst('id')->set(false) ) ) //->leftjoin('recorded_faults', 'recorded_faults.loss_id', '=', "losses.id") ->process($postData) ->json(); }

    I get the error:
    Datatables warning: table_id = generic_table SQLSTATE[42522]: Column not found: 1054 Unknown column 'losses.loss_id' in 'field list'.

    I can't see where it is getting the 'losses.loss_id' from?

        editor = new $.fn.dataTable.Editor({
    
     table: "#generic_table",
    
        idSrc: 'losses.id',
    
        fields: [{
                        label: 'Product:',
                        name: 'products.part_no'
                        },{
                        label: 'Description:',
                        name: 'products.description'
                        },{
                        label: 'Date Selected:',
                        name: 'losses.date_selected'
                        }
                         ]
    
            });
    
    
    
    
    
    //setup the Datatable
            table = $('#generic_table').DataTable({ //took the var off to intentionally make it global
    
                "ajax": {
                    "url": "/lossesAjax",
                    headers: {
                        'X-CSRF-TOKEN': 'vdmUAYd5S3A3V2xbLSSnJtUeKtyeqKTSRCuadhEh'
                    },
                    "type": "POST",
                    data: {table: "losses"}
                },
                "columns": [
                    {
                        data: null,
                        defaultContent: '',
                        className: 'select-checkbox',
                        orderable: false
                    },
    
                    
          { 'data': 'products.part_no'},
          { 'data': 'products.description'},
          { 'data': 'losses.date_selected'},
    
    
    
    
  • MickBMickB Posts: 103Questions: 25Answers: 2

    Just trying to debug.

    Error comes from line 465 Join.php.
    Looking at $stmt
    table is losses as losses
    _field(0) is losses.loss_id as dteditor_pkey

  • MickBMickB Posts: 103Questions: 25Answers: 2

    This is where It comes from;

    ->get( $dteTableLocal.'.'.$joinField.' as dteditor_pkey' )

    dteTableLocal = losses
    joinField = loss_id

    So, maybe I have the joinField wrong somewhere.

  • MickBMickB Posts: 103Questions: 25Answers: 2

    Swapped the link round:

    previously:
    ->link( 'recorded_faults.loss_id', 'losses.id' )
    now:
    ->link( 'losses.id', 'recorded_faults.loss_id' )

    Looks like it is working.

    I guess that my loss_id field is named incorrectly anyway, my primary keys should me tablename_id.

  • MickBMickB Posts: 103Questions: 25Answers: 2
    Answer ✓

    Yes, works now!

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    Good to hear you've got it working now :-)

    Allan

This discussion has been closed.