具有自动完成和分页的Yui数据表

时间:2009-06-16 15:55:39

标签: javascript yui pagination yui-datatable

我尝试了解如何使用服务器端分页动态数据(我看到示例:http://developer.yahoo.com/yui/examples/datatable/dt_dynamicdata.html)。

我的问题是我无法自定义我的请求,当我尝试分页时,我的请求是: QCT-list.htmlsort = geneid&安培; DIR = ASC&安培;的startIndex = 50&安培;结果= 25

请求应采用以下形式: qct-list.html?name = cd4& symbol = cd4& start = 0& limit = 25或 qct-list.html?name = cd4& symbol = cd4& start = 25& limit = 50 ...

以下是我的代码的副本(部分):

        var myColumnDefs = [
            {key:"geneid", label:"Gene", sortable:true},
            {key:"name", label:"Name", sortable:true},
            {key:"symbol", label:"Symbol", sortable:true},
            {key:"lastupdated", label:"Last Updated", formatter:"date", sortable:true},
            {key:"lastmodified", label:"Last Modified", formatter:"date", sortable:true}
        ];

        var myDataSource = new YAHOO.util.DataSource("qct-list.html");
        myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
        myDataSource.connXhrMode = "queueRequests";
        myDataSource.responseSchema = {
            resultsList: "geneItemList",
            fields: ["col","qv","limit","start","geneid","name","symbol","lastupdated","lastmodified"],
            metaFields: {
                    totalRecords: "totalRecords" // Access to value in the server response
                }
        };

        var qctPaginator = new YAHOO.widget.Paginator({
            rowsPerPage: 25,
            totalRecords : YAHOO.widget.Paginator.VALUE_UNLIMITED,
            template: YAHOO.widget.Paginator.TEMPLATE_ROWS_PER_PAGE,
            rowsPerPageOptions: [25,50,75,100]
                        });

        var myRequestBuilder = function(ostate, oSelf) {
            oState = oState || {pagination:null};
            var name = Dom.get('dt_input_name').value;
            var symbol = Dom.get('dt_input_symbol').value;
            var start = (oState.pagination) ? oState.pagination.recordOffset : 0;
            var limit = (oState.pagination) ? oState.pagination.rowsPerPage : 25;
            return  "?name=" + name + "&symbol"+ symbol + "&start=" + start + "&limit=" + limit;
        }

        var oConfigs = {
            dynamicData: true, // Enables dynamic server-driven data
            selectionMode: "single",
            paginator: qctPaginator ,
            generateRequest : myRequestBuilder,
            paginationEventHandler : YAHOO.widget.DataTable.handleDataSourcePagination,
            initialRequest: "?name=&symbol=&start=0&limit="
        };

        var myGeneListTable = new YAHOO.widget.DataTable("geneListTable", myColumnDefs, myDataSource, oConfigs);

        myGeneListTable.handleDataReturnPayload = function(oRequest, oResponse, oPayload) {
            oPayload.totalRecords = oResponse.meta.totalRecords;
            return oPayload;
        }

有人可以解释如何使用DataTable的分页以及如何个性化请求吗?

2 个答案:

答案 0 :(得分:1)

我几乎设法让它成为我的分页。 我的RequestBuilder现在运行正常,我得到了我需要的请求: ?QCT-list.html NAME =干扰素安培;符号=安培;开始= 25安培;限制= 25

这里是新代码:

var myColumnDefs = [
 {key:"geneid", label:"Gene", sortable:true},
 {key:"name", label:"Name", sortable:true},
 {key:"symbol", label:"Symbol", sortable:true},
 {key:"lastupdated", label:"Last Updated", formatter:"date", sortable:true},
 {key:"lastmodified", label:"Last Modified", formatter:"date", sortable:true}
];

var myDataSource = new YAHOO.util.DataSource("qct-list.html");
 myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
 myDataSource.connXhrMode = "queueRequests";
 myDataSource.responseSchema = {
 resultsList: "geneItemList",
 fields: ["col","qv","limit","start","geneid","name","symbol","lastupdated","lastmodified"],
 metaFields: {
                    totalRecords: "totalRecords" // Access to value in the server response
                }
};

var qctPaginator = new YAHOO.widget.Paginator({
    rowsPerPage: 25,
    rowsPerPageOptions: [25,50,75,100]
    });

var myRequestBuilder = function(oState,oSelf)
{
 oState = oState || {pagination:null, sortedBy:null};
 // var sort = (oState.sortedBy) ? oState.sortedBy.key : "geneid";
 // var dir = (oState.sortedBy && oState.sortedBy.dir === YAHOO.widget.DataTable.CLASS_DESC) ? "desc" : "asc";
    var start = (oState.pagination != null) ? oState.pagination.recordOffset : 0;
    var limit = (oState.pagination != null) ? oState.pagination.rowsPerPage : 25;
    var name = Dom.get('dt_input_name').value || "";
    var symbol = Dom.get('dt_input_symbol').value || "";

    return '?name='+ name + '&symbol=' + symbol + '&start='+ start + '&limit=' + limit;

}

var oConfigs = {
  initialRequest: "?name=interferon&symbol=&start=&limit=",
  dynamicData: true, // Enables dynamic server-driven data
  selectionMode: "single",
  paginator: qctPaginator,
  generateRequest : myRequestBuilder
};

var myGeneListTable = new YAHOO.widget.DataTable("geneListTable", myColumnDefs, myDataSource, oConfigs);

 myGeneListTable.handleDataReturnPayload = function(oRequest, oResponse, oPayload) {

    if (oPayload == undefined) {
        oPayload = {};  
    }
    oPayload.totalRecords = oResponse.meta.totalRecords;
    return oPayload;
 }

答案 1 :(得分:0)

我认为你需要交换oConfigs和我的RequestBuilder的声明,以便

var myRequestBuilder = ...

var oConfigs = ...