I need to render based on if a user_id field equals the session user_id

I need to render based on if a user_id field equals the session user_id

trf000trf000 Posts: 4Questions: 2Answers: 0
edited August 2023 in Free community support

here is my columnDefs code, but it does not quite work

"columnDefs": [
              { "type": "html", "targets": 0 
          },
 {  targets: 7,         
              render: function (data, type, row, meta) {
                    var menuLinks = [
                        '<a class="menu-link" href="syllabus_view.php?id=' + data + '"><span class="fal fa-search"></span> View Syllabus</a><br>',
                        '<a class="menu-link" href="shell_edit.php?syllabus_id=' + data + '"><i class="fal fa-edit"></i> Edit Syllabus Shell</a><br>',
                        '<a class="menu-link" href="syllabus_edit.php?syllabus_id=' + data + '"><i class="fal fa-edit"></i> Edit Syllabus Contents</a><br>',
                        '<a class="menu-link" href="shell_copy.php?syllabus_id=' + data + '"><i class="fal fa-copy"></i> Copy Syllabus</a><br>'
                    ];

                    if (data.user_id === '<?=$_SESSION['user_id']?>') {
                        menuLinks.push(
                            '<span class="menu-link"><i style="cursor:pointer;" class="fal fa-trash-alt t' + data + ' i_text"> <span style="font-family: arial;"> Delete Syllabus</span></i></span>' +
                            '<script>' +
                            'document.querySelector("i.fal.fa-trash-alt.t' + data + '").onclick = function(){ ' +
                            'swal({ ' +
                            'title: "Are you sure?", ' +
                            'text: "You will not be able to recover this syllabus!", ' +
                            'type: "warning", ' +
                            'showCancelButton: true, ' +
                            'confirmButtonColor: "#DD6B55", ' +
                            'confirmButtonText: "Yes, delete it!", ' +
                            'closeOnConfirm: false, ' +
                            //closeOnCancel: false
                            '}, ' +
                            'function(){ ' +
                            'location.replace("delete_syllabus.php?syllabus_id=' + data + '&deptid=<?= $dept_id;?>"); ' +
                            '}); ' +
                            '}; ' +
                            '<\/script>'
                        );
                    }


                    return menuLinks.join('');
                   
                 }

                }  
                      
           ],

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    if (data.user_id === '<?=$_SESSION['user_id']?>')

    Is the PHP code resolving? i.e. is the Javascript being parsed by PHP. Or is something else going on. You note that it isn't working, but not in what why. An error, unexpected output, or something else?

    If you could link to a test case showing the issue, that would be helpful.

    Allan

  • trf000trf000 Posts: 4Questions: 2Answers: 0

    it's behind a login.

    nothing in the menuLinks.push is rendering. I should have 132 records with one rendering a delete section.

    the idea is instructors can see and copy other instructor's record, but only be able to delete their own.

  • trf000trf000 Posts: 4Questions: 2Answers: 0

    ah...

    if (row.user_id === '<?=$_SESSION['user_id']?>')

    this works. Thanks for pointing me in the right direction.

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    Ah yes, if you need to access the whole row, that would be right.

    Good to hear you've got it working.

    Allan

Sign In or Register to comment.