[英]Nodejs/Express noob update page view no reload
I am using Node v.0.10.29 on Win XP SP3. 我在Win XP SP3上使用Node v.0.10.29。
I am new to Node/Express. 我是Node / Express的新手。 I am trying to learn Node/Express by duplicating a existing PHP project.
我正在尝试通过复制现有的PHP项目来学习Node / Express。 http://stevenjsteele.com/database/
http://stevenjsteele.com/database/
The PHP project allows the user to build a list of items from either the materials, tools or equipment tables needed for a project, without a page reload. PHP项目允许用户从项目所需的材料,工具或设备表中构建项目列表,而无需重新加载页面。
What I am having a hard time with (don't understand) is: With the PHP project I can change tables using the select table drop down without a page reload. 我遇到的麻烦(不了解)是:在PHP项目中,我可以使用select table下拉列表来更改表,而无需重新加载页面。 This is done with a PHP echo and:
这是通过PHP回显完成的:
xmlHttp.open("POST","getpage.php?tablename="+str,true);
I initialize the app with: 我用以下方法初始化应用程序:
var express = require('express'),
request = require('request'),
requirejs = require('requirejs'),
mysql = require('mysql'), // node-mysql module
path = require('path'),
bodyParser = require('body-parser'),
app = express();
var mh_connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password: ''
});
mh_connection.connect();
var materialhandler = router.route('/materialhandler');
materialhandler.get(function(req,res){
selecttools = 'tools';
selectelectrical = 'electrical';
selectequipment = 'equipment';
mh_connection.query('use materialhandler');
var strQuery = 'SELECT * FROM materialhandler.tools ORDER BY item_id';
mh_connection.query( strQuery, function(err, rows){
if(err) {
throw err;
}else{
tablename='Tools';
res.render('materialhandler',{title:"page title",data:rows});
res.end("");
}
});
});
In my node/express template (which is html) I use: 在我的节点/表达模板(HTML)中,我使用:
For the select option dropdown inside of a form, I use: onchange="getData()"
对于表单内的选择选项下拉菜单,我使用:
onchange="getData()"
<form id="theForm" action="/materialhandler">
<div class="tablepicker two-thirds column">
<select id="selectTable" onchange="getData()" class="selectpicker" name="selectpicker">
<optgroup>
<option name="" value="">Select Table</option>
<option name="tools" value="tools"><%=selecttools%></option>
<option name="electrical" value="electrical"><%=selectelectrical</option>
<option name="equipment" value="equipment"><%=selectequipment%</option>
</optgroup>
</select>
</div>
</form>
function getData() {
//console.log('begin');
var http = new XMLHttpRequest();
var selectedTable = selectTable.options[selectTable.selectedIndex].value;
http.open("POST", 'http://localhost:3000/materialhandler?selectpicker='+selectedTable, true);
http.setRequestHeader("Content-type", "application/x-www-form urlencoded");
http.onreadystatechange = function() {
console.log('onreadystatechange');
if (http.readyState == 4 && http.status == 200) {
//alert(http.responseText);
}
else {
console.log('readyState=' + http.readyState + ', status: ' + http.status);
}
}
//console.log('sending...')
http.send(selectedTable);
console.log(selectedTable)
//console.log('end');
$('#theForm').submit();
}
In terminal window console.log shows the selected table. 在终端窗口中,console.log显示选定的表。
I am trying to change the tables without a page reload. 我试图在不重新加载页面的情况下更改表。
As I said at beginning I am just starting to learn this paradigm. 正如我在一开始所说的那样,我才刚刚开始学习这种范例。 Any help or pointing in the right direction is appreciated.
任何帮助或指向正确的方向表示赞赏。
Thanks. 谢谢。
The $('#theForm').submit();
$('#theForm').submit();
is what's causing your page reload, but instead of doing it that way you can make your post request directly with javascript. 是导致您的页面重新加载的原因,但是您可以直接使用javascript发出发帖请求,而不必那样做。 jQuery would be a better alternative to XMLHttpRequest.
jQuery将是XMLHttpRequest的更好替代方案。
Instead of your getData()
function, you can hook up some javascript to listen for select
changes, and post them to your server like so: 除了使用
getData()
函数,您还可以连接一些JavaScript来侦听select
更改,然后将其发布到服务器,如下所示:
$('#selectTable').on('change', function() {
$.post("materialhandler?selectpicker=" + $(this).val());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.