具有 JSON 和客户端过滤数据错误的 YUI DataTable
我不明白我在这里做错了什么!我不断收到数据错误。但我验证了 JSON 并且没问题...
这是来自 YUI Datatble 示例的 javascript(稍作修改)。
<pre class="prettyprint"><code>
<script type="text/javascript">
YAHOO.util.Event.addListener(window, "load", function() {
//var Ex = YAHOO.namespace('example');
var dataSource = new YAHOO.util.DataSource("jsondb/json_meta_proxy.html",{
responseType : YAHOO.util.DataSource.TYPE_JSON,
responseSchema : {
resultsList: "records",
fields: [
{key:"idprojet"},
{key:"nomprojet"}
],
metaFields: {
totalRecords: "totalRecords"
}
},
doBeforeCallback : function (req,raw,res,cb) {
// This is the filter function
var data = res.results || [],
filtered = [],
i,l;
if (req) {
req = req.toLowerCase();
for (i = 0, l = data.length; i < l; ++i) {
if (!data[i].state.toLowerCase().indexOf(req)) {
filtered.push(data[i]);
}
}
res.results = filtered;
}
return res;
}
});
var cols = [
{key:"idprojet"},
{key:"nomprojet"}
];
var paginateur = new YAHOO.widget.Paginator({
rowsPerPage : 25,
pageLinks : 10
});
var conf = {
paginator : paginateur,
sortedBy: {key:'idprojet', dir:YAHOO.widget.DataTable.CLASS_ASC}
};
var dataTable = new YAHOO.widget.DataTable('tbl',cols,dataSource,conf);
var filterTimeout = null;
var updateFilter = function () {
// Reset timeout
filterTimeout = null;
// Reset sort
var state = dataTable.getState();
state.sortedBy = {key:'idprojet', dir:YAHOO.widget.DataTable.CLASS_ASC};
// Get filtered data
dataSource.sendRequest(YAHOO.util.Dom.get('filter').value,{
success : dataTable.onDataReturnInitializeTable,
failure : dataTable.onDataReturnInitializeTable,
scope : dataTable,
argument: idprojet
});
};
YAHOO.util.Event.on('filter','keyup',function (e) {
clearTimeout(filterTimeout);
setTimeout(updateFilter,600);
});
});
</script>
这是文件“jsondb/json_meta_proxy.html”中的 JSON 数据
{
"recordsReturned": 1,
"totalRecords": 1,
"startIndex": 0,
"sort": "idprojet",
"dir": "asc",
"records": [
{
"idprojet": "11256",
"nomprojet": ""
}
]
}
非常感谢您的帮助!
I don't understand what I'm doing wrong here ! I keep getting a Data Error. But I validated the JSON and it's ok...
Here is the javascript from the YUI Datatble example (slightly modified).
<pre class="prettyprint"><code>
<script type="text/javascript">
YAHOO.util.Event.addListener(window, "load", function() {
//var Ex = YAHOO.namespace('example');
var dataSource = new YAHOO.util.DataSource("jsondb/json_meta_proxy.html",{
responseType : YAHOO.util.DataSource.TYPE_JSON,
responseSchema : {
resultsList: "records",
fields: [
{key:"idprojet"},
{key:"nomprojet"}
],
metaFields: {
totalRecords: "totalRecords"
}
},
doBeforeCallback : function (req,raw,res,cb) {
// This is the filter function
var data = res.results || [],
filtered = [],
i,l;
if (req) {
req = req.toLowerCase();
for (i = 0, l = data.length; i < l; ++i) {
if (!data[i].state.toLowerCase().indexOf(req)) {
filtered.push(data[i]);
}
}
res.results = filtered;
}
return res;
}
});
var cols = [
{key:"idprojet"},
{key:"nomprojet"}
];
var paginateur = new YAHOO.widget.Paginator({
rowsPerPage : 25,
pageLinks : 10
});
var conf = {
paginator : paginateur,
sortedBy: {key:'idprojet', dir:YAHOO.widget.DataTable.CLASS_ASC}
};
var dataTable = new YAHOO.widget.DataTable('tbl',cols,dataSource,conf);
var filterTimeout = null;
var updateFilter = function () {
// Reset timeout
filterTimeout = null;
// Reset sort
var state = dataTable.getState();
state.sortedBy = {key:'idprojet', dir:YAHOO.widget.DataTable.CLASS_ASC};
// Get filtered data
dataSource.sendRequest(YAHOO.util.Dom.get('filter').value,{
success : dataTable.onDataReturnInitializeTable,
failure : dataTable.onDataReturnInitializeTable,
scope : dataTable,
argument: idprojet
});
};
YAHOO.util.Event.on('filter','keyup',function (e) {
clearTimeout(filterTimeout);
setTimeout(updateFilter,600);
});
});
</script>
and here is the JSON data in the file "jsondb/json_meta_proxy.html"
{
"recordsReturned": 1,
"totalRecords": 1,
"startIndex": 0,
"sort": "idprojet",
"dir": "asc",
"records": [
{
"idprojet": "11256",
"nomprojet": ""
}
]
}
Many thanks for your help !!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改为 TYPE_JSARRAY 解决了问题...
感谢您的评论埃里克!
我去了这个论坛,它确实是数据表问题的地方。
changing to TYPE_JSARRAY solved the problem...
Thanks for your comments Eric !
I went to this forum and it is indeed the place for datatable questions.
如果您在 StackOverflow 上没有得到答案,您可能需要尝试将问题发布到 YUI 开发者论坛:http:// /yuilibrary.com/forum。许多 YUI 专家,包括最有经验的 DataTable 开发人员,都在那里闲逛。
-埃里克
If you don't get an answer on StackOverflow, you may want to try posting your question to the YUI developer forums: http://yuilibrary.com/forum. Many YUI experts, including the most experienced DataTable developers, hang out there.
-Eric