当前位置: 首页 > 知识库问答 >
问题:

填充具有javascript变量的超文本标记语言表,并将该超文本标记语言变量作为typefirst结果返回

莫兴言
2023-03-14

我在输入上使用typeahead发送建议。。一切都很好,但我知道我想动态创建HTML表并在表中赋值,所以在我的java脚本中,我声明HTML变量来存储表,然后在返回值时使用这个变量。当我将html保存在变量中时,问题就出现了,它开始给出错误,即变量未定义,当我将它放在引号中时,返回时,它只显示变量。。我不知道该怎么做,也不知道应该使用哪种方法,但我被困在了这一点上,我的HTML正在工作

 <input class="typeahead" placeholder="Enter to Search" />

这里是我的javascript工作

 var jsonData = [{
country: "Holland",
city: "Amsterdam"
}, {
country: "Belgium",
city: "Brussel"
}, {
country: "Germany",
city: "Berlin"
}, {
country: "France",
    "city": "Paris"
}];

var dataSource = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('country', 'city'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: jsonData

});

dataSource.initialize();
var html='<div><table width="100%" border="1"><tr><td width="50%" align="right">\'+data.country+\'</td><td width="50%">' + 'data.city' + '</td></tr></table></div>';

$('.typeahead').typeahead({
minLength: 1,
highlight: true
}, {
name: 'countries',
display: function(item){ return item.country+'–'+item.city},
source: dataSource.ttAdapter(),
templates: {
    empty: [
        '<div class="empty">No Matches Found</div>'].join('\n'),
            header: '<div><h5><table width="100%" border="1"><thead><tr><th width="50%" align="center">Item Name</th><th width="50%" align="center">City Name</td></th></table></h5></div>', 
    suggestion: function (data) {
        return html
    }
  }
  }); 

这是我正在使用的JSFIDLE链接

有什么建议吗?

现在我可以动态生成HTML。在你的建议的帮助下。所以现在我要在更新的fiddle中动态生成数组键,但问题是当我动态生成数组键时,它会给出未定义的结果,当我硬编码数组键时,它会返回我所需的真实值。请查看我的更新的fiddle下面是我的javascript代码

 var jsonData = [{ 
 a: "Holland",
b: "Amsterdam"
}, {
a: "Holland",
b: "Amsterdam"
}, {
a: "Holland",
b: "Amsterdam"
}, {
a: "Holland",
b: "Amsterdam"
}, {
a: "Belgium",
b: "Brussel"
}, {
a: "Germany",
b: "Berlin"
}, {
a: "France",
b: "Paris"
}];

 var dataSource = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('a', 'b'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: jsonData

 });

dataSource.initialize();

var generateHTML= function(data) {
var key='a';
var html='<div><table width="100%" border="1"><tr>';
for(var i=0; i<2; i++)
{
html+='<td width="50%" align="right">' + data. key + '</td>';
  //key++;
}
//'<td width="50%">' + data.city + '</td></tr></table></div>';
html+='</tr></table></div>';
return html;
//return '<div><table width="100%" border="1"><tr><td width="50%" align="right">' + data.country + '</td><td width="50%">' + data.city + '</td></tr></table></div>'
};

$('.typeahead').typeahead({
minLength: 1,
highlight: true
}, {
name: 'countries',
display: function(item){ return item.country+'–'+item.city},
source: dataSource.ttAdapter(),
templates: {
    empty: [
        '<div class="empty">No Matches Found</div>'].join('\n'),
            header: '<div><h5><table width="100%" border="1"><thead>    <tr><th width="50%" align="center">Item Name</th><th width="50%" align="center">City Name</td></th></table></h5></div>', 
    suggestion: function (data) {
        return generateHTML(data)
    }
  }
  });

谢谢大家的帮助和建议。我能够成功地做到这一点。当我们通过对象调用Java脚本数组时,我们必须按照以下模式传递动态生成的键

 obj[key];

下面是我正在使用的JSFIDLE链接

共有1个答案

邢高爽
2023-03-14

由于您的HTML不在函数suggestion的范围内,因此变量HTML无法看到名为data的变量。

var generateHTML= function(data) {
    return '<div><table width="100%" border="1"><tr><td width="50%" align="right">' + data.country + '</td><td width="50%">' + data.city + '</td></tr></table></div>'
};

然后在建议函数上调用此函数:

$('.typeahead').typeahead({
    minLength: 1,
    highlight: true
}, {
    name: 'countries',
    display: function(item){ return item.country+'–'+item.city},
    source: dataSource.ttAdapter(),
    templates: {
        empty: [
            '<div class="empty">No Matches Found</div>'].join('\n'),
                header: '<div><h5><table width="100%" border="1"><thead><tr><th width="50%" align="center">Item Name</th><th width="50%" align="center">City Name</td></th></table></h5></div>', 
        suggestion: function (data) {
            return generateHTML(data)
        }
    }
});

下面是如何做到这一点的一个例子:http://jsfiddle.net/59b62kz3/

 类似资料:
  • 在我的。java文件,我有 在我的第1页。html文件: 现在,my page1显示: 是否可以将我的结果变量呈现为html,以便我的page1显示一个大的绿色通行证?除了th: text${var}之外,还有其他格式选项吗?我正在使用Spring boot和thymeleaf。我尽量不使用javascript。 类似这样的东西,但对于java

  • 我正在工作的工具提示和从后端我将获得数据与html标记。我需要在工具提示中显示相应的标签中的相应数据。例如,我将从后端获得Hello用户单击此处。我必须显示为你好用户在h1格式,点击这里应该是一个锚。我尝试了这两个功能,并取代其不工作。 具有以下功能: 替换: https://codesandbox.io/s/serene-fast-u8fie?file=/App.svelte

  • 我在将一些客户端数据表逻辑迁移到服务器端时遇到了一些困难。 我当前的问题是,对于Datatables,如果要对一大组数据(20000行)进行分页,我首先需要加载控制器中的所有行,然后将它们传递给视图: 然后,在加载所有内容和Datatables将记录分页为500页之前,需要等待大约2分钟: 我更改了Datatables声明,以通过处理服务器端处理,如下所示: 让它按我所希望的方式工作非常顺利,但问

  • 是否可以以某种方式将锚点(或哈希)添加到 Struts 2 操作 URL?具体来说: 我有一个html表单,如果用户单击“添加更多字段”按钮,它可以扩展更多字段。该按钮向后端发送一个html submit(动作“thismyaction”),其中一个列表对象被另一组输入字段填充。然后,操作返回到tile“thisismyform ”,它像以前一样加载相同的jsp,在这里新的字段集是可见的。 (不幸

  • 我有两个数组都包含HTML元素,我想从第一个数组中删除第二个数组中也存在的所有元素。 我试过这样的东西,但似乎不起作用:

  • 我试图在HTML的pre标签中包装文本,但它不起作用。我使用下面的CSS作为我的标签。 我从如何在pre标记中换行文本? 我已添加