简体   繁体   中英

Check checkbox based on postgres boolean

I have an page called form.ejs where users can check checkboxes based on the items that are applicable to them.

<div class="container">
        <td class="td-form" class="td-form">
          <div class="tooltipupc">
          <input type="checkbox" id="repoLocationNaming" name="repoLocationNaming" value="<%= model.repoLocationNaming ? 'checked' : '' %>" onclick="checkboxTicked('repoLocationNaming', 1.85)">
          <label for="repoLocationNaming" style="color:red">Repo location and Naming Convention</label><br>
        </td>
</div>

<div class="container">
        <td class="td-form" class="td-form">
          <div class="tooltipupc">
          <input type="checkbox" id="userGroupperMissions" name="userGroupperMissions" value="<%= model.userGroupperMissions ? 'checked' : '' %>"  onclick="checkboxTicked('userGroupperMissions', 1.85)">
          <label for="userGroupperMissions" style="color:red">User Groups (Read, Write, Reviewer)</label><br>
        </td>
</div>

Once the form is completed the users can save these Boolean values based on the checked checkboxes to a postgres database. These are saved to a postgres database. This is done with a function in the route code.

    router.post("/", async (req, res) => {
      const sql = "INSERT INTO Pipelines (slug, gear, app, url, score, rating, immatureCount, poorMaturityCount, maturingCount, matureUpQualifiedCount, chaosCount, rallyGitIntergration, pullRequestBasedEnablement, repoLocationNaming, userGroupperMissions) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)";
      const pipeline = [slugify(req.body.url, { lower: true, strict: true }), req.body.gear, req.body.app, req.body.url, req.body.score, req.body.rating, req.body.immatureAmt, req.body.poorMaturityAmt, req.body.maturingAmt, req.body.matureUpQualifiedAmt, req.body.chaosAmt, req.body.rallyGitIntergration, req.body.pullRequestBasedEnablement, convertCheckbox(req.body.repoLocationNaming), convertCheckbox(req.body.userGroupperMissions)];
      pool.query(sql, pipeline, (err, result) => {
        if (err) {
          return console.error(err.message);
        }
 res.redirect(`/pipelines/${slugify(req.body.url, { lower: true, strict: true })}`);
      });
    
      function convertCheckbox(checkboxID){
        if (checkboxID === "") {
          return true;
        } else {
          return false;
        }
      }
    });

When the user returns to the form, I wish to populate the forms checkboxes with checks when equal to true. For instance, I can populated the inputted url like this.

<input required value="<%= model.url %>" type="text" name="url" id="url" style="width: 70%"><br>

Below is the get route for the form which should be populated by the user inputted information.

router.get("/edit/:id", async (req, res) => {
  const idRating = req.params.id;
  const sqlRating = "SELECT * FROM Pipelines WHERE pipeline_id = $1";
  pool.query(sqlRating, [idRating], (err, result) => {
    if (err) {
      return console.error(err.message);
    }
    res.render("pipelines/edit", { model: result.rows[0] });
  });

}); 

The file form.ejs is included in in edit.ejs. When a user accesses the route edit.js as seen above, I want to check whether the checkbox values are true or false in { model: result.rows[0] } . If they are true then I want the checkbox to be checked. How can I check this boolean value and equate it to a checked checkbox?

<input type="checkbox" <%= model.repoLocationNaming ? 'checked' : '' %>
  id="repoLocationNaming" name="repoLocationNaming"
  onclick="checkboxTicked('repoLocationNaming', 1.85)>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM