2017-08-02 82 views
0

我想從我的數據庫下傳值的下降在我看來: 這是我的項目一個js文件:通價值下拉菜單

newproject.js:

var express = require('express'); 
var router = express.Router(); 
var sql = require ('mssql'); 


router.get('/', function(req, res, next) { 
    res.render('newProject'); 
}); 


const config = { 
    user: 'sa', 
    password: 'password', 
    server: 'localhost\\SQLEXPRESS', // You can use 'localhost\\instance' to connect to named instance 
    database: 'pcgdb', 

    options: { 
     encrypt: false // Use this if you're on Windows Azure 
    } 
}; 


sql.connect(config).then(() => { 
    return sql.query`select Project_Type_Desc from Project_Type` 
}).then(result => { 
    console.log(result) 
}).catch(err => { 
    console.log(err) 
}) 



module.exports = router; 

這是相應的視圖:

index.ejs

<!DOCTYPE html> 
<html lang="en"> 

<head> 
    <meta charset="UTF-8"> 
    <title>PCG WEB APP</title> 
</head> 

<body> 
<h1>Please input a new project</h1> 
    <form class="form-horizontal" role="form" style="width: 50%;"> 
      <label for="sel1">Region : </label> 
      <select class="form-control" id="sel1"> 
       <option>EMEA</option> 
       <option>APAC</option> 
       <option>AMER</option> 
      </select> 
    Country : 
    <input type="text" name="coutry"><br> 
    City: 
    <input type="text" name="city"><br> 
    Requested By : 
    <input type="text" name="request_by"><br> 
    Project Responsibility: 
    <input type="text" name="project_responsibility"><br> 
    Facility Classification: 
    <input type="radio" name="facilityclassification" value="new" checked> New 
    <input type="radio" name="facilityclassification" value="existing"> Existing<br> 
      <label for="sel1">Project Type : </label> 
      <select class="form-control" id="sel1"> 
       <option>Densification</option> 
       <option>Renovation</option> 
       <option>Expansion/Contraction of Office</option> 
       <option>Infrastructure Upgrades</option> 
       <option>Existing Office Relocation</option> 
       <option>New Location</option> 
      </select> 
    Expected Start Date : 
    <input type="date" name="start_date"><br> 
    Expected End Date : 
    <input type="date" name="end_date"><br> 
    Brief description of scope of work: 
    <input type="text" name="description"><br> 
    Project manager : 
    <input type="text" name="manager"><br> 
    Project Owner : 
    <input type="text" name="owner"><br> 
    Project Sponser : 
    <input type="text" name="sponser"><br> 
    Functional Currency : 
    <input type="text" name="currency"><br> 
    <input type="submit" value="Submit" class = "btn btn-primary"> 
</form> 
</div> 
</body> 
</html> 

我想要傳遞我查詢的值來查看用作項目類型選擇的淹沒,請讓我知道你們是否知道解決方案。我對es5或es6完全陌生,並且在語法上很難。我也想查詢額外的表來填充更多的下拉菜單。

數據庫是MS SQL Server和我使用MSSQL模塊與提前DB

感謝連接。

+1

看一看這個答案如何變量傳遞給EJS模板:https://stackoverflow.com/a/16098699/2027146 – Milk

+0

然後這個答案是如何在ejs中使用循環來輸出你的'select''選項':https://stackoverflow.com/a/22952940/ 2027146 – Milk

+0

所以我應該創建一個JSON,然後將其傳遞給我的觀點? – SSS

回答

2

傳遞DB結果模板:

router.get('/', function(req, res, next) { 
    sql.connect(config).then(() => { 
     return sql.query`select Project_Type_Desc from Project_Type` 
    }).then(result => { 
     console.log(result) 
     // Pass the DB result to the template 
     res.render('newProject', {dropdownVals: result}) 
    }).catch(err => { 
     console.log(err) 
    }) 
}); 

然後在模板中,使用經過的價值:

<select class="form-control" id="sel1"> 
<% for(var i=0; i < dropdownVals.length; i++) { %> 
    <option><%= dropdownVals[i] %></option> 
<% } %> 
</select> 
+0

你知道如何查詢2個表並傳遞這些值嗎?已經爲此掙扎了2天 – SSS

+0

順便說一句,這工作完美的我謝謝 – SSS

+0

@ SSS請接受答案,如果它爲你工作。如果您想要通過多個列表,則可以進行多個數據庫調用並將結果作爲傳遞給'res.render'的對象中的不同字段傳遞 – Milk