开发者

JQuery Datatables, filtring not working (trough ajax/async)

I'm using JQuery Datatables. I'm getting the table data trough ajax and there's a method that I would like to use, called fnGetColumnData (that gets table 开发者_StackOverflow社区values for each column). Since ajax is async, running on domready, and this function is anonymous, on the moment it tries to getdata, the data is not available. So, the SELECT filters are blank.

Docs @ http://www.datatables.net/examples/api/multi_filter_select.html

My code @ http://pastie.org/1896827 (* I couldnt paste it right here because the toolbar was not available at the time I posted.)

How can I solve this ?

Thanks for looking


Since you are using ajax as datasource, i think you have to populate the select in another way, because on the client side you don't have access to the full data of the column. You have to take the data to poulate the select with AJAX too. I'd something like this:

i'd change this call

this.innerHTML = fnCreateSelect( oTable.fnGetColumnData(i) );

in

this.innerHTML = fnCreateSelect( i );

and then change fnCreateSelect so that it gets the option from an Ajax Source like this

function fnCreateSelect( iColumnNumber )
{
    var r='<select><option value=""></option>';
    $.getJSON(
              'createSelect.php',
               { colNumber: iColumnNumber },
               function (json) 
                 {//i don't check for success to make things simpler
                      foreach (option in json.data){
                           r += '<option value="'+option.name+'">'+option.name+'</option>';
                      }
                 }
               );
    return r+'</select>';
}

And than have a createSelect.php file that does something like this:

$colNumber = $_GET['colNumber'];
switch($colNumber){
case '0': $sql = "SELECT DISTINCT colName0 from table";
          break;
case '1': $sql = SELECT DISTINCT colname1 from table"
          break;
//make as many cases as the number of your columns then retrieve the data from the DB and return the json
}

In this way you populate the select filters with the actual data they have to filter

P.S i've written the code just to show you my idea, i didn't try it out because i don't have access to your server side data, if it's not clear i'll try to be more precise.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜