Reorder column index

Reorder column index

smassotsmassot Posts: 28Questions: 9Answers: 1
edited October 2015 in ColReorder

Hello,
in my datatable I had to do a custom filter like that :

// définition des filtres datatables sur actions
            ['Total','Major','Partiel','Non','Retiré','Revu','Sans Objet','Indéfini'].forEach(function(action) {
                var actionName = action.replace(' ','');
                actionName = action.replace('é','e');
                $.fn.DataTable.ext.search.push(
                        function( settings, data, dataIndex ) {
                           // console.info('trace:'+ data);
                            var actionValue = (data[6] == action);
                            return (actionValue ? $('#ckActions'+actionName).is(':checked') : $('#ckActions'+actionName).not(':checked'));
                        }
                    );
                });

As you could see I check data[6] but now my customer would be able to reorder columns, I used colRerder plugin to do that
but when I reorder one or more colums, the initial data[6] is now for example col[3] and my custom filter didn't worked.

I'm not sure to have done my filter with the best way, could you tell me how I could know which new index had my data[6] after reorder please ?

Thanks a lot.

Answers

  • allanallan Posts: 61,443Questions: 1Answers: 10,053 Site admin

    Generally speaking, when using ColReorder I would very much recommend using objects rather than arrays as this kind of issue is just side-stepped completely. If you do need to use arrays then you will need to either:

    1. Remove and then add a new and updated search function on each column-reorder event
    2. Make the code inside the function dynamic so it can do a look up. I'd be concerned about performance for this option.

    Objects are the way to go here if you can!

    Allan

  • smassotsmassot Posts: 28Questions: 9Answers: 1

    HI Allan,
    totally agree with you for using object but I'm not totally skill about that,
    Is there possible for you to let me one example on how to transform this code in object mode please ? thanks a lot.

    Chris.

  • allanallan Posts: 61,443Questions: 1Answers: 10,053 Site admin

    The key thing to do is so change your data source to use objects. If you are reading from the DOM then that is as simple as using columns.data - see this example.

    If you are using an Ajax data source, then you need to update whatever code you are using on the server-side to create objects rather than arrays.

    Allan

  • smassotsmassot Posts: 28Questions: 9Answers: 1

    here is data returned by ajax :

    {
        "data": [
            {
                "DT_RowId": "row_1",
                "qualite": {
                    "id": "674",
                    "id_domaine": "157",
                    "etat": "4",
                    "references_precises": null,
                    "recurrent": "1",
                    "date_echeance_initiale": null
                },
                "qualite_full": {
                    "id": "674",
                    "id_domaine": "11",
                    "etat": "Non",
                    "references_precises": null,
                    "recurrent": "Oui",
                    "date_echeance_initiale": null
                },
                "configurationEtats": [
                    {
                        "id": "91",
                        "titre": "Indéfini",
                        "valeur": "0",
                        "class": "label-default"
                    },
                    {
                        "id": "92",
                        "titre": "Total",
                        "valeur": "1",
                        "class": "label-success"
                    }
                ]
            }
        ]
    }
    
    

    so I tried to use :

    var actionValue = (data.qualite_full.etat == action);
    

    seems wrong, could you just give me one line example if you have time
    Thanks a lot Allan

  • allanallan Posts: 61,443Questions: 1Answers: 10,053 Site admin

    Actually that looks correct to me. I'm not sure why that wouldn't work. Can you link to a test case showing the issue please.

    Allan

  • smassotsmassot Posts: 28Questions: 9Answers: 1
    edited October 2015

    Well i's difficult to do a test case, cause there are many function in order to make this work :/

    What I could see with a console.log(data) :

    trace:12 - Comment garantissons-nous la dignité des résidents (en particulier, comment traitons-nous son linge personnel et la question de son habillement) ?,Evaluation interne 2015, Réorganisation du travail des remplacants en lingerie,ASH REF,Dir, Lingéres, ASHREF,31-08-2015,Non,Dédier et former des personnels en remplacement,t ,,Zero plainte ,Protocoles et plannings,,Non,

    So, it seems to be juste one date line without any column name information. Maybe I do it wrong ?

  • allanallan Posts: 61,443Questions: 1Answers: 10,053 Site admin

    I'm afraid I don't know what is wrong without being ale to see the full code. I don't know what data is in your one line above for example. A link to a test case would let me take a look and see what is happening.

    Allan

  • smassotsmassot Posts: 28Questions: 9Answers: 1

    I will try to do a test case if possible. Thanks Allan

This discussion has been closed.