Customised control buttons - if statement based on column value

Customised control buttons - if statement based on column value

carrarachristophecarrarachristophe Posts: 109Questions: 25Answers: 2

Hello,
I am trying to implement some Customised control buttons but with a conditional statement.
Basically, the action of a button is dependant from the value of a column in the selected row.

My code is not totally wrong as:
* the else condition works fine, and
* if I remove the if condition, the action works fine

My problem is that the conditions do not work, probably because I am not testing properly the value of the colum of the selected row. (I get "Undefined" instead of the status that I am looking for)
Here is my code.

    const iso20022_pain001_PmtInftable = $('#iso20022_pain001_PmtInf').DataTable( {
        ajax: 'php/table.iso20022_pain001_PmtInf.php',
        dom: 'Bfrtip',
        buttons: [
            {
                extend: 'selectedSingle',
                text: 'Valider',
                action: function (e, dt, node, config) {
                    if (iso20022_pain001_PmtInfEditor.get('iso20022_pain001_PmtInf.statut') == 6) {
                        iso20022_pain001_PmtInfEditor
                            .edit(iso20022_pain001_PmtInftable.row({ selected: true }).index(), false)
                            .set('iso20022_pain001_PmtInf.statut', 7)
                            .submit();
                    }
                    else if ('iso20022_pain001_PmtInf.statut' == 7) {
                        iso20022_pain001_PmtInfEditor
                            .edit(iso20022_pain001_PmtInftable.row({ selected: true }).index(), false)
                            .set('iso20022_pain001_PmtInf.statut', 2)
                            .submit();
                    }
                    else alert( 'Ce paiement ne peut être validé' );
                }
            }
        ],
        columns: [
            {data: null, render: function ( data, type, row ) {
                return data.iso20022_pain001_PmtInf.MsgId+ '-' +data.iso20022_pain001_PmtInf.PmtInf_id;
            }},
            {data: "iso20022_pain001_PmtInf.Dbtr_Name"},
            {data: "iso20022_pain001_PmtInf.NbOfTxs"},
            {data: "iso20022_pain001_PmtInf.ReqdExctnDt"},
            {data: "statuts.statut"}
        ],
        columnDefs: [
            {
                targets: 3,
                render: DataTable.render.date()
            }
        ],
        order: [[ 0, 'asc' ]],
        select: {
            style: 'single'
        }
    } );

Would you know how I could amend rows 9 and 15 (I tested various options) to test the value of the 5th column?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,322Questions: 26Answers: 4,948
    edited August 2023 Answer ✓

    You are trying to use the Editor's get() API in lines 9 and 15 but you haven't told the Editor what row of data to use. Instead use the Datatables row().data() API. Something like this:

                action: function (e, dt, node, config) {
                    var statut = iso20022_pain001_PmtInftable.row({ selected: true }).data().iso20022_pain001_PmtInf.statut;
                    if (statut == 6) {
                        iso20022_pain001_PmtInfEditor
                            .edit(iso20022_pain001_PmtInftable.row({ selected: true }).index(), false)
                            .set('iso20022_pain001_PmtInf.statut', 7)
                            .submit();
                    }
                    else if (statut == 7) {
                        iso20022_pain001_PmtInfEditor
                            .edit(iso20022_pain001_PmtInftable.row({ selected: true }).index(), false)
                            .set('iso20022_pain001_PmtInf.statut', 2)
                            .submit();
                    }
                    else alert( 'Ce paiement ne peut être validé' );
                }
    

    Kevin

  • carrarachristophecarrarachristophe Posts: 109Questions: 25Answers: 2
    edited August 2023

    I know that iso20022_pain001_PmtInfEditor.get('iso20022_pain001_PmtInf.statut') can retrieve the data because as per the example, I can for example get the value and add 1 to it:

                    action: function (e, dt, node, config) {
                        iso20022_pain001_PmtInfEditor
                            .edit(iso20022_pain001_PmtInftable.row({ selected: true }).index(), false)
                            .set('iso20022_pain001_PmtInf.statut', iso20022_pain001_PmtInfEditor.get('iso20022_pain001_PmtInf.statut') * 1 + 1)
                            .submit();
                    }
    

    But I don't manage to include it in the conditional statement. I tried that

                    action: function (e, dt, node, config) {
                        iso20022_pain001_PmtInfEditor
                            .edit(iso20022_pain001_PmtInftable.row({ selected: true }).index(), false)
                            .set('iso20022_pain001_PmtInf.statut',
                                if (iso20022_pain001_PmtInfEditor.get('iso20022_pain001_PmtInf.statut') == 6) {
                                 7
                                }
                                else if (iso20022_pain001_PmtInfEditor.get('iso20022_pain001_PmtInf.statut') == 7) {
                                 2
                                }
                                else {
                                iso20022_pain001_PmtInfEditor.get('iso20022_pain001_PmtInf.statut') * 1
                                })
                            .submit();
                    }
    
  • carrarachristophecarrarachristophe Posts: 109Questions: 25Answers: 2

    Hi Kevin, I posted my last answer before seeing yours. Let me try it!

  • carrarachristophecarrarachristophe Posts: 109Questions: 25Answers: 2

    It worked like a charm, thanks Kevin!
    I just have a NS_BINDING_ABORTED warning that I have to fix but getting close to what I am looking for.

Sign In or Register to comment.