How can I pass variable from stored session or route (nodejs) to use it in ColumnDefs?

How can I pass variable from stored session or route (nodejs) to use it in ColumnDefs?

fosfikfosfik Posts: 5Questions: 2Answers: 0
edited January 2022 in DataTables 1.10

How can I pass variable from stored session or route (nodejs) to use it in ColumnDefs to filter buttons by usergroup permission?

"columnDefs": [
             {"data":null,
             "render":function(data, type, row, meta){
                 return data[0],data[1],data[2],data[3],data[4],data[5];
                 }
             },
             {"targets":4, render:function(data){
                 return moment(data).format('YYYY-MM-DD HH:mm');
                 }
             },
             {"targets":5, render:function(data){
                 return moment(data).format('YYYY-MM-DD HH:mm');
                 }
             },
             {"targets":-1,
             "searchable": false,
             "data":null,
             "render":function(data, type, row, meta){
                 return `<div class="btn-group" role="group"> 
                 <a id="icon" class="text-dark border-0 rounded-0" href="/edit/`+ data[0] +`">
                     <svg xmlns="http://www.w3.org/2000/svg" width="17" height="20" fill="currentColor" class="bi bi-pencil-square" viewBox="0 0 16 16">
                         <path d="M15.502 1.94a.5.5 0 0 1 0 .706L14.459 3.69l-2-2L13.502.646a.5.5 0 0 1 .707 0l1.293 1.293zm-1.75 2.456l-2-2L4.939 9.21a.5.5 0 0 0-.121.196l-.805 2.414a.25.25 0 0 0 .316.316l2.414-.805a.5.5 0 0 0 .196-.12l6.813-6.814z"/>
                         <path fill-rule="evenodd" d="M1 13.5A1.5 1.5 0 0 0 2.5 15h11a1.5 1.5 0 0 0 1.5-1.5v-6a.5.5 0 0 0-1 0v6a.5.5 0 0 1-.5.5h-11a.5.5 0 0 1-.5-.5v-11a.5.5 0 0 1 .5-.5H9a.5.5 0 0 0 0-1H2.5A1.5 1.5 0 0 0 1 2.5v11z"/>
                     </svg>
                 </a>
                 </div>` 
                 ;}
             }
         ]

I want to make more buttons like this on up but only show them to specific usergroup.
I have session store in my DB and on client side it was easy becouse there I coudl use something like this from render result:

<% if (user.usergroup == "superadmin") { %> 
<a href ....

Now I have problem at server side. Not using editor and can't pass this usergroup from session to read it in columnDefs.

This is function to render file to show datatable:

    data: async (req,res) => {
        const ssn = req.session;
        res.render('data.ejs', {
            title: ""
            ,message:''
            ,usergroup: ssn.usergroup
            ,loggeduser: ssn.username
            ,fullname: ssn.fullname
            ,subcat1: ssn.subcat1 
            ,subcat2: ssn.subcat2
            ,usercat: ssn.usercat 
            
        });
        

    },

and another one to send data from database:

dataTable: (req, res) => {
        const requestQuery = req.query;
        const ssn = req.session;
        const usergroup = ssn.usergroup;
        let columnsMap = [
            {
                db: "id",
                dt: 0
            },
            {
                db: "category",
                dt: 1
            },
            {
                db: "topic",
                dt: 2
            },
            {
                db: "rodzaj",
                dt: 3
            },
            {
                db: "send_date",
                dt: 4
            },
            {
                db: "update_date",
                dt: 5
            },
            {  
                db: "author",
                dt: 6
            },
            {  
                db: "author",
                dt: 7
            }


          ];

       
          const primaryKey = "id"
          let query = `my query`;
          
          const nodeTable = new NodeTable(requestQuery, db, query, primaryKey, columnsMap);
         
          nodeTable.output((err, data)=>{
            if (err) {
              console.log(err);
              return;
            }
            //console.log("data: ",data);
            // Directly send this data as output to Datatable
            res.send(data)
          })
    },

Using https://www.npmjs.com/package/nodetable

This question has an accepted answers - jump to answer

Answers

  • fosfikfosfik Posts: 5Questions: 2Answers: 0

    nvm... I will take usergroup from db and add it to dataTable as data[8] with redering

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Answer ✓

    Three options:

    1. Generate your Javascript dynamically (just as you might with HTML, you could have a route in your Node app to do that). That way you can fill in your JS variables.
    2. Make an Ajax request to get the parameters you need from a JSON route. Then with that information build the data needed.
    3. Add a dynamic <script> tag into your dynamically generated HTML which assigns the values you need to Javascript variables. This is similar to 1, but the main JS file wouldn't be dynamically generated - you'd just reference those static variables that you created.

    Allan

Sign In or Register to comment.