从 ssjs 读取 csjs 中的 json 个对象
Read json object in csjs from ssjs
我想知道是否可以在 sessionScope 变量中将 json 对象数组从 ssjs 发送到 csjs。当我尝试在脚本块中使用 sesionScope.get 时。不是 运行?
SSJS scriptsChartDojo.jss:
function createChartData()
{
var resultArray = new Array();
resultArray = [
{y: 1978, text: "Blue Widget"},
{y: 1669, text: "Brown Widget"},
{y: 2017, text: "Green Widget"},
{y: 334, text: "Orange Widget"},
{y: 1051, text: "Pink Widget"},
{y: 1545, text: "Purple Widget"},
{y: 339, text: "Red Widget"},
{y: 1067, text: "Yellow Widget"}];
sessionScope.chartData = resultArray;
}
在 Scriptblock 中使用 CSJS 的 Xpage 源代码:
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" dojoParseOnLoad="true"
dojoTheme="true" xmlns:xe="http://www.ibm.com/xsp/coreex">
<xp:this.beforePageLoad><![CDATA[#{javascript:createChartData();}]]></xp:this.beforePageLoad>
<xp:this.resources>
<xp:dojoModule name="dojox.charting.Chart2D"></xp:dojoModule>
<xp:script src="/scriptsChartDojo.jss" clientSide="false"></xp:script>
</xp:this.resources>
<xp:scriptBlock id="scriptBlock1">
<xp:this.value><![CDATA[makeCharts = function()
{
//Create a new 2D Chart
var pieChart = new dojox.charting.Chart2D("#{id:panel1}");
alert(sessionScope.get("chartData")); // Does not alert anything.
// Add the only/default plot for the pie chart
pieChart.addPlot("default", {type: "Pie", radius: 150, fontColor: "black", labels: false});
// Add the data series
pieChart.addSeries("Number of Orders", sessionScope.get("chartData")); // Causes blank screen to load
//Render the chart!
pieChart.render();
};
XSP.addOnLoad(makeCharts);]]></xp:this.value>
</xp:scriptBlock>
<xp:panel style="height:450px;width:450px" id="panel1"></xp:panel>
</xp:view>
在 xp:scriptBlock 中,您将客户端 JS 与服务器端 JS 组合在一起。服务器端 JS 代码必须在 #{javascript:<code>}
中才能被解析。所以这样做:
<xp:scriptBlock id="scriptBlock1">
<xp:this.value><![CDATA[
makeCharts = function() {
//Create a new 2D Chart
var pieChart = new dojox.charting.Chart2D("#{id:panel1}");
alert(sessionScope.get("chartData")); // Does not alert anything.
// Add the only/default plot for the pie chart
pieChart.addPlot("default", {type: "Pie", radius: 150, fontColor: "black", labels: false});
// Add the data series
pieChart.addSeries("Number of Orders", #{javascript:sessionScope.get("chartData")});
//Render the chart!
pieChart.render();
};
XSP.addOnLoad(makeCharts);
]]></xp:this.value>
</xp:scriptBlock>
更新: 您的 SSJS Array() 未按预期返回。如果您改为使用简单的字符串,您会看到代码可以正常工作并将数据从 SSJS 发送到 CSJS。这是一个简单的例子:
<xp:this.beforePageLoad><![CDATA[#{javascript:
var resultArray = "[{y: 1978, text: \"Blue Widget\"},{y: 1669, text: \"Brown Widget\"},{y: 2017, text: \"Green Widget\"},{y: 334, text: \"Orange Widget\"},{y: 1051, text: \"Pink Widget\"},{y: 1545, text: \"Purple Widget\"},{y: 339, text: \"Red Widget\"},{y: 1067, text: \"Yellow Widget\"}]";
sessionScope.chartData = resultArray;
}]]></xp:this.beforePageLoad>
<xp:scriptBlock id="scriptBlock1">
<xp:this.value><![CDATA[
makeCharts = function() {
console.log(#{javascript:sessionScope.get("chartData")});
};
XSP.addOnLoad(makeCharts);
]]></xp:this.value>
</xp:scriptBlock>
您可以将作用域变量绑定到隐藏的文本字段中。确保它的 readOnly
属性设置为 false
。参见
然后在您的 CSJS 中通过以下方式获取其值:
var field = document.getElementById("#{id:hiddenInput1}");
myJson = field.value;
我想知道是否可以在 sessionScope 变量中将 json 对象数组从 ssjs 发送到 csjs。当我尝试在脚本块中使用 sesionScope.get 时。不是 运行?
SSJS scriptsChartDojo.jss:
function createChartData()
{
var resultArray = new Array();
resultArray = [
{y: 1978, text: "Blue Widget"},
{y: 1669, text: "Brown Widget"},
{y: 2017, text: "Green Widget"},
{y: 334, text: "Orange Widget"},
{y: 1051, text: "Pink Widget"},
{y: 1545, text: "Purple Widget"},
{y: 339, text: "Red Widget"},
{y: 1067, text: "Yellow Widget"}];
sessionScope.chartData = resultArray;
}
在 Scriptblock 中使用 CSJS 的 Xpage 源代码:
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" dojoParseOnLoad="true"
dojoTheme="true" xmlns:xe="http://www.ibm.com/xsp/coreex">
<xp:this.beforePageLoad><![CDATA[#{javascript:createChartData();}]]></xp:this.beforePageLoad>
<xp:this.resources>
<xp:dojoModule name="dojox.charting.Chart2D"></xp:dojoModule>
<xp:script src="/scriptsChartDojo.jss" clientSide="false"></xp:script>
</xp:this.resources>
<xp:scriptBlock id="scriptBlock1">
<xp:this.value><![CDATA[makeCharts = function()
{
//Create a new 2D Chart
var pieChart = new dojox.charting.Chart2D("#{id:panel1}");
alert(sessionScope.get("chartData")); // Does not alert anything.
// Add the only/default plot for the pie chart
pieChart.addPlot("default", {type: "Pie", radius: 150, fontColor: "black", labels: false});
// Add the data series
pieChart.addSeries("Number of Orders", sessionScope.get("chartData")); // Causes blank screen to load
//Render the chart!
pieChart.render();
};
XSP.addOnLoad(makeCharts);]]></xp:this.value>
</xp:scriptBlock>
<xp:panel style="height:450px;width:450px" id="panel1"></xp:panel>
</xp:view>
在 xp:scriptBlock 中,您将客户端 JS 与服务器端 JS 组合在一起。服务器端 JS 代码必须在 #{javascript:<code>}
中才能被解析。所以这样做:
<xp:scriptBlock id="scriptBlock1">
<xp:this.value><![CDATA[
makeCharts = function() {
//Create a new 2D Chart
var pieChart = new dojox.charting.Chart2D("#{id:panel1}");
alert(sessionScope.get("chartData")); // Does not alert anything.
// Add the only/default plot for the pie chart
pieChart.addPlot("default", {type: "Pie", radius: 150, fontColor: "black", labels: false});
// Add the data series
pieChart.addSeries("Number of Orders", #{javascript:sessionScope.get("chartData")});
//Render the chart!
pieChart.render();
};
XSP.addOnLoad(makeCharts);
]]></xp:this.value>
</xp:scriptBlock>
更新: 您的 SSJS Array() 未按预期返回。如果您改为使用简单的字符串,您会看到代码可以正常工作并将数据从 SSJS 发送到 CSJS。这是一个简单的例子:
<xp:this.beforePageLoad><![CDATA[#{javascript:
var resultArray = "[{y: 1978, text: \"Blue Widget\"},{y: 1669, text: \"Brown Widget\"},{y: 2017, text: \"Green Widget\"},{y: 334, text: \"Orange Widget\"},{y: 1051, text: \"Pink Widget\"},{y: 1545, text: \"Purple Widget\"},{y: 339, text: \"Red Widget\"},{y: 1067, text: \"Yellow Widget\"}]";
sessionScope.chartData = resultArray;
}]]></xp:this.beforePageLoad>
<xp:scriptBlock id="scriptBlock1">
<xp:this.value><![CDATA[
makeCharts = function() {
console.log(#{javascript:sessionScope.get("chartData")});
};
XSP.addOnLoad(makeCharts);
]]></xp:this.value>
</xp:scriptBlock>
您可以将作用域变量绑定到隐藏的文本字段中。确保它的 readOnly
属性设置为 false
。参见
然后在您的 CSJS 中通过以下方式获取其值:
var field = document.getElementById("#{id:hiddenInput1}");
myJson = field.value;