开发者

When no data is returned from database

I am intiating a loading panel in init method and hiding it in ReturnDataPayload event.This is working perfectly when data Table has got some values in it.But when there is no data returned from database , the control is not going to returnDataPayLoad event.Please help me in finding开发者_Python百科 an event which will be fired even when the response doesn't have any data or tell me a way to hide the loading panel.


If you want a custom behavior, use DataSource's sendRequest method of the dataTable's dataSource

(function() {
    var YdataTable  = YAHOO.widget.DataTable,
        YdataSource = YAHOO.util.DataSource;

    var settings = {
        container:"<DATATABLE_CONTAINER_GOES_HERE>",
        source:"<URL_TO_RETRIEVE_YOUR_DATA>",
        columnSettings:[
            {key:"id", label:"Id"}
        ],
        dataSourceSettings:{
            responseType:YdataSource.TYPE_JSON,
            responseSchema:{
                resultsList:"rs",
                fields:[
                    {key:"id"}
                ]
            }
        },
        dataTableSettings:{
            initialLoad:false
        }
    }

    var dataTable = new YdataTable(
                    settings.container, 
                    settings.columnSettings, 
                    new YdataSource(
                    settings.source, 
                    settings.dataSourceSettings), 
                    settings.dataTableSettings);
})();

keep in mind No matter which source is your data: XML, JSON, JavaScript object, TEXT, you always will get your data in a unified way through DataSource's sendRequest method. So when you want to retrieve your data and, at the same time, add custom behavior, use it

dataTable.getDataSource().sendRequest(null, {
    success:function(request, response, payload) {
        if(response.results.length == 0) {
            // No data returned
            // Do what you want right here
            // You can, for instance, hide the dataTable by calling this.setStyle("display", "none");
        } else {
            // Some data returned
            // If you want to use default the DataTable behavior, just call

            this.onDataReturnInitializeTable(request, response, payload);
        }
    },
    scope:dataTable,
    argument:dataTable.getState()
});

The properties of the response are

  • results (Array): Your source of data in a unified way. For each object in the results Array, There is a property according to responseSchema's fields property. Notice i use response.results.length to verify if some data has been returned
  • error (Boolean): Indicates data error
  • cached (Boolean): Indicates cached response
  • meta (Object): Schema-parsed meta data

On the YUI dataTable page, look for Loading data at runtime to see some built-in functions provided by YUI dataTable

I hope it can be useful and feel free to ask for help for anything else you want about YUI. See a demo page of nice features of YUI dataTable

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜