简体   繁体   English

基于 postgres 布尔值检查复选框

[英]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.我有一个名为 form.ejs 的页面,用户可以在其中根据适用于他们的项目检查复选框。

<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.表单完成后,用户可以根据选中的复选框将这些布尔值保存到 postgres 数据库。 These are saved to a postgres database.这些被保存到 postgres 数据库中。 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.当用户返回表单时,我希望在等于 true 时使用检查填充表单复选框。 For instance, I can populated the inputted url like this.例如,我可以像这样填充输入的 url。

<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.文件 form.ejs 包含在 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] } .当用户访问如上所示的路由 edit.js 时,我想检查{ 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)>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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