简体   繁体   English

尝试在javascript中制作下一个按钮以显示json数组

[英]trying to make next button in javascript to display json array

I am trying to display the json array according to certain start and end indices.我正在尝试根据某些开始和结束索引显示 json 数组。 I did displayed it all but when I click on first button it doesn't work.我确实显示了所有内容,但是当我单击第一个按钮时它不起作用。 Here is my code .. after clicking on first button it says "this.data is undefined" any help ??这是我的代码 .. 点击第一个按钮后,它说“this.data is undefined”有什么帮助吗?

 function GridLibrary(data) { this.data = data; } GridLibrary.prototype.display = function() { var html = []; html.push("<table >\\n<tbody>"); html.push("<tr>"); for ( var propertyNames in this.data[0]) { html.push("<th>" + propertyNames + "</th>"); } html.push("</tr>"); // loop through the array of objects for (var i = startIndex; i < endIndex; i++) { item = this.data[i]; html.push("<tr>"); for ( var key in item) { html.push("<td>" + item[key] + "</td>"); } html.push("</tr>"); } html.push("<table>\\n</tbody>"); $('body').append(html.join("")); }; var grid = new GridLibrary(); $("#first").click(function() { //size = parseInt(document.getElementById("listsize").value, 10); startIndex = 3; endIndex = 6; grid.display(); });

the Grid.jsp Grid.jsp

<script type="text/javascript">
    var json = [ {
        "id" : 1,
        "name" : "name1",
        "age" : 10,
        "feedback" : "feedback1"
    }, {
        "id" : 2,
        "name" : "name2",
        "age" : 90,
        "feedback" : "feedback2"
    }, {
        "id" : 3,
        "name" : "name3",
        "age" : 30,
        "feedback" : "feedback3"
    }, {
        "id" : 4,
        "name" : "name4",
        "age" : 50,
        "feedback" : "feedback4"
    }, {
        "id" : 5,
        "name" : "name5",
        "age" : 55,
        "feedback" : "feedback5"
    }, {
        "id" : 6,
        "name" : "name6",
        "age" : 68,
        "feedback" : "feedback6"
    }, {
        "id" : 7,
        "name" : "name7",
        "age" : 57,
        "feedback" : "feedback7"
    }, {
        "id" : 8,
        "name" : "name8",
        "age" : 89,
        "feedback" : "feedback8"
    }, {
        "id" : 9,
        "name" : "name9",
        "age" : 65,
        "feedback" : "feedback9"
    }, {
        "id" : 10,
        "name" : "name10",
        "age" : 54,
        "feedback" : "feedback10"
    }, {
        "id" : 11,
        "name" : "name11",
        "age" : 51,
        "feedback" : "feedback11"
    }, {
        "id" : 12,
        "name" : "name12",
        "age" : 97,
        "feedback" : "feedback12"
    } ];
    new GridLibrary(json).display();
</script>

I've made a few assumptions here.我在这里做了一些假设。

1) Your constructor should accept a parameter which will be your data: 1)你的构造函数应该接受一个参数,这将是你的数据:

function GridLibrary(data){
  this.data = data;
  return this;
}

2) This will be used like so: 2)这将像这样使用:

var grid = new GridLibrary(json);

3) You should then pass in startIndex and endIndex as parameters to display() : 3) 然后您应该将startIndexendIndex作为参数传递给display()

grid.display(0, 11);

or或者

grid.display(3, 6);

and change the method to accept those arguments:并更改方法以接受这些参数:

GridLibrary.prototype.display = function(startIndex, endIndex) {...

4) Finally you should change the HTML (not append) to a specific div otherwise you will overwrite your first button: 4) 最后,您应该将 HTML(而不是附加)更改为特定的div否则您将覆盖您的first按钮:

$('#body').html(html.join(""));

DEMO演示

Just by looking at your code, it seems like you don't close your table tag properly.仅通过查看您的代码,您似乎没有正确关闭 table 标记。 The last html.push adds a new <table> , instead of </table> .最后一个 html.push 添加了一个新的<table> ,而不是</table> They are also in the wrong order, it should be </tbody></table>他们的顺序也错了,应该是</tbody></table>

edit: fixed formatting编辑:固定格式

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

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