Search code examples
javascriptnode.jsmobile-websitepugexpress

NodeJS, Assigning Name/getting value of Jade Checkboxes


I populate a table within a form from a Mongo Database. I want the user to be able to select one or many checkboxes and then determine which items were selected from the table. One part of the problem is that I am not sure how to name the checkboxes as they are dynamically created. I also do not know how to read the checkbox property.

I am using ExpressJS and Jade. I can populate the table no problem, but have no idea how to then communicate which items were selected.

Here is my jade

    h1 index view

    form  (method='post', action='/customers/check')
      fieldset
        legend Add a Customer
    div.clearfix
      -if(docs.length)
      table(style="border-left:1px solid black")
        tr
          th First Name
          th Last Name
          th "Hidden" 
            each first in docs
              tr
               td #{first.first} 
               td #{first.surname}
               td #{first.group}
               td
                 div.input
                    input(type="checkbox", name=(#{first.box}), unchecked=     (true===true ? "checked" : "")).checkbox
        div.actions
          input(type='submit', value='Save', class='btn primary')
          button(type='reset', class='btn') Cancel

My Mongo Database has four properties (first, surname, group, and i added box in attempt to solve this problem In the big picture what i am doing is taking a String input, then rendering a view with this table and I want to store the String input and whatever elements from the table that are selected into another mongoDB with two properties ( string and complex object) please saveee meeee!!! thanks


Solution

  • Ah got it, was dancing around the solution the whole time

      h1 index view
    
     form(method='post', action='/customers/check')
    fieldset
        legend Add a Customer
        div.clearfix
            -if(docs.length)
                table(style="border-left:1px solid black")
                    tr
                        th First Name
                        th Last Name
                        th "Hidden" 
                            each first in docs
                                tr
                                    td #{first.first} 
                                    td #{first.surname}
                                    td #{first.group}
                                    td
                                        div(data-role='fieldcontain')   
                                            fieldset(data-type='vertical', data-role='controlgroup')                                           
                                                label(for='showpass') show password
                                                input(id='showpass',type='checkbox', name='#{first.id}')
        div.actions
            input(type='submit', value='Save', class='btn primary')
            button(type='reset', class='btn') Cancel
    

    and my server side I put the checked values into an array called "arr", here that is

         app.post('/customers/check', function(req, res) {
            console.log(req.body);
            var locks = req.body;
            var arr = Object.keys(locks);
            console.log(arr);   
            res.redirect('/customers')
    });