Showing duplicate rows using custom button?

Showing duplicate rows using custom button?

map@odenterprise.orgmap@odenterprise.org Posts: 14Questions: 4Answers: 0

Hi,

I am a beginner in PHP and Javascript coding. I would like to update my editor table to show only duplicate records. I want to show all rows having duplicate entries in the org_profiles.org_name column. I have been trying to use search but was unsuccessful in developing the logic. The below code is working to search a particular column value on click of a button. Kindly direct me any way you can. Thanks!

var editor;

$(document).ready(function() {
    editor = new $.fn.dataTable.Editor( {
        ajax: "../php/staff.php",
        table: "#example",
        fields: [ {
                label: "profile_id:",
                name: "org_profiles.profile_id"
            },  {
                label: "org_name:",
                name: "org_profiles.org_name"
            }, {
                label: "org_description:",
                name: "org_profiles.org_description"
            }, {
                label: "org_size:",
                name: "org_profiles.org_size"
            }, {
                label: "org_profile_status:",
                name: "org_profiles.org_profile_status"
            }, 
            {
                label: "org_hq_city:",
                name: "org_profiles.location_id",
                type: "select",
                placeholder: "Select a location"
            },
            {
                label: "org_hq_st_prov:",
                name: "org_locations.org_hq_st_prov"

            },
            {
                label: "survey_contact_email:",
                name: "org_contacts.survey_contact_email"

            },
                        {
                label: "survey_contact_first:",
                name: "org_contacts.survey_contact_first"

            },
                        {
                label: "survey_contact_last:",
                name: "org_contacts.survey_contact_last"

            },
                        {
                label: "survey_contact_phone:",
                name: "org_contacts.survey_contact_phone"

            },
                        {
                label: "survey_contact_title:",
                name: "org_contacts.survey_contact_title"

            },
                        {
                label: "org_hq_country:",
                name: "org_profiles.country_id",
                type: "select",
                placeholder: "Select a country"
            },

                        {
                label: "org_hq_country_income:",
                name: "org_country_info.org_hq_country_income"

            },
                        {
                label: "org_hq_country_income_code:",
                name: "org_country_info.org_hq_country_income_code"

            },
                        {
                label: "org_hq_country_locode:",
                name: "org_country_info.org_hq_country_locode"

            },
                        {
                label: "org_hq_country_region:",
                name: "org_country_info.org_hq_country_region"

            },
                        {
                label: "org_hq_country_region_code:",
                name: "org_country_info.org_hq_country_region_code"

            },
                        {
                label: "ISO2:",
                name: "org_country_info.ISO2"

            }
        ]
    } );

    // Activate an inline edit on click of a table cell
    $('#example').on( 'click', 'tbody td:not(:first-child)', function (e) {
        editor.inline( this , {
            submit: 'allIfChanged'  //Added by Vinayak
        }
            );
    } );

    var table = $('#example').DataTable( {
        dom: "Bfrtip",
        ajax: "../php/staff.php",
        order: [[ 1, 'asc' ]],
        "scrollY": 400,
        "scrollX": true,
        "scrollCollapse": true,
        columns: [
            {
                data: null,
                defaultContent: '',
                className: 'select-checkbox',
                orderable: false
            },
            { data: "org_profiles.profile_id" },
            { data: "org_profiles.org_name" },
            { data: "org_profiles.org_description" },
            { data: "org_profiles.org_size" },
            { data: "org_profiles.org_profile_status"},
            { data: "org_locations.org_hq_city", editField: "org_profiles.location_id"},
            { data: "org_locations.org_hq_st_prov", editField: "org_locations.org_hq_st_prov"},
            { data: "org_contacts.survey_contact_email", editField: "org_contacts.survey_contact_email"},
            { data: "org_contacts.survey_contact_first", editField: "org_contacts.survey_contact_first"},
            { data: "org_contacts.survey_contact_last", editField: "org_contacts.survey_contact_last"},
            { data: "org_contacts.survey_contact_phone", editField: "org_contacts.survey_contact_phone"},
            { data: "org_contacts.survey_contact_title", editField: "org_contacts.survey_contact_title"},
            { data: "org_country_info.org_hq_country", editField: "org_profiles.country_id"},

            { data: "org_country_info.org_hq_country_income", editField: "org_country_info.org_hq_country_income"},
            { data: "org_country_info.org_hq_country_income_code", editField: "org_country_info.org_hq_country_income_code"},
            { data: "org_country_info.org_hq_country_locode", editField: "org_country_info.org_hq_country_locode"},
            { data: "org_country_info.org_hq_country_region", editField: "org_country_info.org_hq_country_region"},
            { data: "org_country_info.org_hq_country_region_code", editField: "org_country_info.org_hq_country_region_code"},
            { data: "org_country_info.ISO2", editField: "org_country_info.ISO2"},


        ],
        select: {
            style:    'os',
            selector: 'td:first-child'
        },
        buttons: [
            { extend: "create", editor: editor },
            { extend: "edit",   editor: editor },
            { extend: "remove", editor: editor },
            {
                text: 'Find Duplicates',
                action: function ( e, dt, node, config ) {
                    //alert( 'Button activated' );
                    table
                        .columns( 2 )
                        .search( 'Vividin' )
                        .draw();            
                }
            }
        ],

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,872Questions: 1Answers: 10,527 Site admin

    The only way I can think of doing this is to use a custom search plug-in. When the function is called (search counter === 0) get the data from the table and bin it (i.e. find the duplicates by having a counter for each data point). Then as the filter runs for the records you can consult your bins and check to see if the row is part of a duplicate or not.

    Allan

  • map@odenterprise.orgmap@odenterprise.org Posts: 14Questions: 4Answers: 0

    Hi Allan,

    Thanks a lot for your input. It was extremely helpful. I was able to implement it using the following code:

    buttons: [
                { extend: "create", editor: editor },
                { extend: "edit",   editor: editor },
                { extend: "remove", editor: editor },
                {
                    text: 'Find Duplicates',
                    action: function ( e, dt, node, config ) {
                        //alert( 'Button activated' );
    /*                    table
                            .columns( 2 )
                            .search( 'Vividin' )
                            .draw();    */      
    
        var org_name = [];
    
        table
        .column( 2 )
        .data()
        .each( function ( value, index ) {
            org_name.push(value);
    
        } );
    
        $.fn.dataTable.ext.search.push(
        function( settings, data, dataIndex ) {
    
            var count = org_name.reduce(function(n, val) {
                 return n + (val === data[2]);
            }, 0);
    
                if(count >= 2)
                {
                    return true
                }
                return false
            }
            );
                // Event listener to the two range filtering inputs to redraw on input
                table
                .column( 2 )
                .order( 'asc' )
                .draw();
    
               }
            }
            ],
    

    Again, thanks a lot for the prompt reply!

  • allanallan Posts: 63,872Questions: 1Answers: 10,527 Site admin
    Answer ✓

    Nice one! Thanks for posting back with your answer.

    Allan

This discussion has been closed.