jquery模板构建网格
我有一个类似下面的jsonjquery模板构建网格,jquery,jquery-templates,Jquery,Jquery Templates,我有一个类似下面的json [ { "UserName": "John Robertson", "OperatorUserId": 1177, "PartCategoryID": 0, "PartCategoryName": null, "UnitCount": 22, "CategoryWiseCount": 13, "TotalUsers": 0, "TotalCategories": 0, "MainArrayCoun
[
{
"UserName": "John Robertson",
"OperatorUserId": 1177,
"PartCategoryID": 0,
"PartCategoryName": null,
"UnitCount": 22,
"CategoryWiseCount": 13,
"TotalUsers": 0,
"TotalCategories": 0,
"MainArrayCount": null,
"NewMainArrayCount": null,
"ListCategorywiseCount": [6, 1, 2, 0, 0, 0, 13, 0, 0],
"ListCategories": ["Base Unit", "Hard Drive", "Laptop", "Laptop AC Adapter", "Motherboard", "Optical Drive", "Processor (CPU)", "Server", "TFT"],
"ListUsers": ["John Robertson", "user1", "Margaret Graham", "Stuart White", "Bob van Schie", "Caird Hay", "bsoni", "tsname"]
}, {
"UserName": "user1",
"OperatorUserId": 39,
"PartCategoryID": 0,
"PartCategoryName": null,
"UnitCount": 13,
"CategoryWiseCount": 1,
"TotalUsers": 0,
"TotalCategories": 0,
"MainArrayCount": null,
"NewMainArrayCount": null,
"ListCategorywiseCount": [1, 0, 3, 1, 1, 0, 0, 6, 1],
"ListCategories": ["Base Unit", "Hard Drive", "Laptop", "Laptop AC Adapter", "Motherboard", "Optical Drive", "Processor (CPU)", "Server", "TFT"],
"ListUsers": ["John Robertson", "user1", "Margaret Graham", "Stuart White", "Bob van Schie", "Caird Hay", "bsoni", "tsname"]
}, {
"UserName": "Margaret Graham",
"OperatorUserId": 1169,
"PartCategoryID": 0,
"PartCategoryName": null,
"UnitCount": 2,
"CategoryWiseCount": 2,
"TotalUsers": 0,
"TotalCategories": 0,
"MainArrayCount": null,
"NewMainArrayCount": null,
"ListCategorywiseCount": [2, 0, 0, 0, 0, 0, 0, 0, 0],
"ListCategories": ["Base Unit", "Hard Drive", "Laptop", "Laptop AC Adapter", "Motherboard", "Optical Drive", "Processor (CPU)", "Server", "TFT"],
"ListUsers": ["John Robertson", "user1", "Margaret Graham", "Stuart White", "Bob van Schie", "Caird Hay", "bsoni", "tsname"]
}, {
"UserName": "Stuart White",
"OperatorUserId": 961,
"PartCategoryID": 0,
"PartCategoryName": null,
"UnitCount": 1,
"CategoryWiseCount": 1,
"TotalUsers": 0,
"TotalCategories": 0,
"MainArrayCount": null,
"NewMainArrayCount": null,
"ListCategorywiseCount": [0, 1, 0, 0, 0, 0, 0, 0, 0],
"ListCategories": ["Base Unit", "Hard Drive", "Laptop", "Laptop AC Adapter", "Motherboard", "Optical Drive", "Processor (CPU)", "Server", "TFT"],
"ListUsers": ["John Robertson", "user1", "Margaret Graham", "Stuart White", "Bob van Schie", "Caird Hay", "bsoni", "tsname"]
}, {
"UserName": "Bob van Schie",
"OperatorUserId": 1007,
"PartCategoryID": 0,
"PartCategoryName": null,
"UnitCount": 1,
"CategoryWiseCount": 1,
"TotalUsers": 0,
"TotalCategories": 0,
"MainArrayCount": null,
"NewMainArrayCount": null,
"ListCategorywiseCount": [0, 1, 0, 0, 0, 0, 0, 0, 0],
"ListCategories": ["Base Unit", "Hard Drive", "Laptop", "Laptop AC Adapter", "Motherboard", "Optical Drive", "Processor (CPU)", "Server", "TFT"],
"ListUsers": ["John Robertson", "user1", "Margaret Graham", "Stuart White", "Bob van Schie", "Caird Hay", "bsoni", "tsname"]
}, {
"UserName": "Caird Hay",
"OperatorUserId": 1184,
"PartCategoryID": 0,
"PartCategoryName": null,
"UnitCount": 1,
"CategoryWiseCount": 1,
"TotalUsers": 0,
"TotalCategories": 0,
"MainArrayCount": null,
"NewMainArrayCount": null,
"ListCategorywiseCount": [0, 1, 0, 0, 0, 0, 0, 0, 0],
"ListCategories": ["Base Unit", "Hard Drive", "Laptop", "Laptop AC Adapter", "Motherboard", "Optical Drive", "Processor (CPU)", "Server", "TFT"],
"ListUsers": ["John Robertson", "user1", "Margaret Graham", "Stuart White", "Bob van Schie", "Caird Hay", "bsoni", "tsname"]
}, {
"UserName": "bsoni",
"OperatorUserId": 2575,
"PartCategoryID": 0,
"PartCategoryName": null,
"UnitCount": 3,
"CategoryWiseCount": 2,
"TotalUsers": 0,
"TotalCategories": 0,
"MainArrayCount": null,
"NewMainArrayCount": null,
"ListCategorywiseCount": [0, 0, 0, 0, 1, 0, 2, 0, 0],
"ListCategories": ["Base Unit", "Hard Drive", "Laptop", "Laptop AC Adapter", "Motherboard", "Optical Drive", "Processor (CPU)", "Server", "TFT"],
"ListUsers": ["John Robertson", "user1", "Margaret Graham", "Stuart White", "Bob van Schie", "Caird Hay", "bsoni", "tsname"]
}, {
"UserName": "tsname",
"OperatorUserId": 2576,
"PartCategoryID": 0,
"PartCategoryName": null,
"UnitCount": 1,
"CategoryWiseCount": 1,
"TotalUsers": 0,
"TotalCategories": 0,
"MainArrayCount": null,
"NewMainArrayCount": null,
"ListCategorywiseCount": [0, 0, 0, 0, 0, 1, 0, 0, 0],
"ListCategories": ["Base Unit", "Hard Drive", "Laptop", "Laptop AC Adapter", "Motherboard", "Optical Drive", "Processor (CPU)", "Server", "TFT"],
"ListUsers": ["John Robertson", "user1", "Margaret Graham", "Stuart White", "Bob van Schie", "Caird Hay", "bsoni", "tsname"]
}]
John Robertson User1 Margaret Graham .....
Base unit 6 1 ...
Hard Drive 1 0 ...
.....
我想使用jquery模板查看网格中的上述数据
John Robertson User1 Margaret Graham .....
Base unit 6 1 ...
Hard Drive 1 0 ...
.....
解释
在我们开始之前,有几件事需要弄清楚
John Robertson User1 Margaret Graham .....
Base unit 6 1 ...
Hard Drive 1 0 ...
.....
第一:我假设“模板”指的是一个插件,因为它仍然处于测试阶段,而且还不推荐使用。此外,这对你也没多大帮助
John Robertson User1 Margaret Graham .....
Base unit 6 1 ...
Hard Drive 1 0 ...
.....
你可以通过学习如何构建自己的插件,不过我将给你一个完整的例子来说明如何做到这一点
John Robertson User1 Margaret Graham .....
Base unit 6 1 ...
Hard Drive 1 0 ...
.....
通常,当您想要与插件相关的元素时,它以
John Robertson User1 Margaret Graham .....
Base unit 6 1 ...
Hard Drive 1 0 ...
.....
(function($) {
//Prototype
$.fn.myPlugin = function(){
//Logic
$(this).append("Hello world!");
}
})(JQuery);
$("#element").myPlugin();
否则,作为通过$
访问的通用函数,它只是
John Robertson User1 Margaret Graham .....
Base unit 6 1 ...
Hard Drive 1 0 ...
.....
(function($) {
//Prototype
$.myPlugin = function(){
//Logic
alert("Hello world!");
}
})(JQuery);
$.myPlugin();
秒:通过AJAX发送JSON时,您希望将对象绑定到JSON,而不是数组。JSON代表JavaScript对象表示法,因此即使通过查看名称,它也应该告诉您它与对象有关。
换句话说,您需要使用大括号
{}
发回一个对象
John Robertson User1 Margaret Graham .....
Base unit 6 1 ...
Hard Drive 1 0 ...
.....
因此,您的JSON应该如下所示
John Robertson User1 Margaret Graham .....
Base unit 6 1 ...
Hard Drive 1 0 ...
.....
{
data: ["Hello ",
"World",
"!",
{time: "HH:mm:ss", date: "yyyy-mm-dd"}
}
$.ajax({
url: "example_json_output.txt",
dataType: "json",
success: function(json){
alert(JSON.stringify(json));
}
});
你可以阅读更多关于它的内容
John Robertson User1 Margaret Graham .....
Base unit 6 1 ...
Hard Drive 1 0 ...
.....
第三名:为了澄清,要在AJAX调用中请求JSON数据,必须将
数据类型指定为JSON。是这样做的
John Robertson User1 Margaret Graham .....
Base unit 6 1 ...
Hard Drive 1 0 ...
.....
{
data: ["Hello ",
"World",
"!",
{time: "HH:mm:ss", date: "yyyy-mm-dd"}
}
$.ajax({
url: "example_json_output.txt",
dataType: "json",
success: function(json){
alert(JSON.stringify(json));
}
});
请注意,我使用了函数,它序列化(将所有变量放入文本)变量。非常适合调试目的!:)
John Robertson User1 Margaret Graham .....
Base unit 6 1 ...
Hard Drive 1 0 ...
.....
或者,您可以使用速记版本
John Robertson User1 Margaret Graham .....
Base unit 6 1 ...
Hard Drive 1 0 ...
.....
解决方案
在我给出代码之前,这里是一个屏幕截图,显示了您的结果应该是什么样子。
John Robertson User1 Margaret Graham .....
Base unit 6 1 ...
Hard Drive 1 0 ...
.....
当然,我不是说你应该像我那样做,事实上,我鼓励你用不同的方式去做。熟能生巧。此外,这可能不是你想要的布局
John Robertson User1 Margaret Graham .....
Base unit 6 1 ...
Hard Drive 1 0 ...
.....
正如所承诺的,下面是生成上述屏幕截图的所有代码。
我将从JavaScript文件和JSON输出开始,因为它们是最相关的。
请注意,所有文件都在同一文件夹中
John Robertson User1 Margaret Graham .....
Base unit 6 1 ...
Hard Drive 1 0 ...
.....
文件:JavaScript“example.js”
John Robertson User1 Margaret Graham .....
Base unit 6 1 ...
Hard Drive 1 0 ...
.....
文件:HTML“example.HTML”
John Robertson User1 Margaret Graham .....
Base unit 6 1 ...
Hard Drive 1 0 ...
.....
您使用的是哪个jquery模板插件?您好,非常感谢。但我使用jquery模板实现了它。