columns.render with condition

columns.render with condition

louis_Elesselouis_Elesse Posts: 3Questions: 3Answers: 0
edited November 2018 in Free community support
"services": [{
            "id": "1",
            "nom": "Garde-Malade"
        }, {
            "id": "2",
            "nom": "Soins infirmiers",
            "actif": true
        }, {
            "id": "3",
            "nom": "P\u00e9dicure M\u00e9dicale"
        }, {
            "id": "4",
            "nom": "je ne sais plus"
        }],
i have this in my colums initialisation table  
           {
                "data": "services",
                "render": "[, ].nom"
            },

when i execuse my application it create a comma separated list like this:
Garde-Malade, Soins infirmiers, Pédicure Médicale, je ne sais plus

how can iapply condition on "render": "[, ].nom" to obtain only the one with "actif": true?. i mean to obtain only Soins infirmiers

thank for your help!

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @louis_Elesse ,

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • rf1234rf1234 Posts: 2,985Questions: 87Answers: 421
    edited November 2018

    You can use render with a function as in my column filtr.field_type. Your comma separated list should get passed in as parameter "data". Then you can do whatever you like with it using Javascript or jQuery.

    var fieldTypeOptions = 
            [
                { label: "Number, 0 decimals, sign opt.", value: 1 },
                { label: "Number, 5 decimals, sign opt.", value: 2 },
                { label: "Percent, 0 decimals", value: 4 },
                { label: "Percent, 3 decimals", value: 5 },
                { label: "Date (DD/MM/YYYY)", value: 7 },
                { label: "Text", value: 9 }
            ];
    
    var filtrTable = $('#tblFiltr').DataTable({
        dom: "Bfrtip",
        ajax: {
            url: 'actions.php?action=tblFiltr'
        },
        columns: [
            {   data: "govdept", render: "[, ].name"    },
            {   data: "filtr.label" },
            {   data: "filtr.field_type",
                render: function (data, type, row) {
                    return $.grep(fieldTypeOptions, function(obj){return obj.value == data;})[0].label;
                }
            },
            {   data: "filtr.update_time"   }
        ],
        columnDefs: [
            // targets may be classes
            {   targets: "hiddenCols", visible: false   },
            {   targets: "filtrs", className: "filtrsInline"    }
        ],
        select: {
            style:    'os',
            selector: 'td:first-child'
        },     
        buttons: [
            {   extend: "create", editor: filtrEditor   },
            {   extend: "edit", editor: filtrEditor },
            {   extend: "remove", editor: filtrEditor   },
                "colvis"
        ]
    });
    

    Please see this too: https://datatables.net/reference/option/columns.render
    There's also one example with render as a function.

    I would just use a debugger to see what gets passed in as parameter "data" when using the function.

This discussion has been closed.