1

I would like to ask if there is a way to filter datatable server-side in initilization?

I've tried a code that goes something like this:

function listTable(arg1, param2, value3) {
var searchTbl = $("#tblsearch").dataTable({
                    "bRetrieve": true,
                    "bProcessing": true,
                    "bServerSide": true,                   
                    "sAjaxSource": "server-side.php",
                    "bPaginate": false,
                    "bLengthChange": false,
                    "bFilter": false,
                    "bSort": true,
                    "bInfo": false,
                    "bAutoWidth": false
                });

searchTbl.filter(arg1, param2, value3);
searchTbl.fnDraw();
}

but it did not work.

2 Answers 2

5

Salute brod,

As what I've understand. You are trying to filter your records to display on jquery dataTables from your server-side server-side.php script.

It will only work if you may provide a sample of your server-side.php script.

However to provide you with better understanding and input, your desired filter will all depend on the database query are executing on the server-side script. For example:

"SELECT column_1, column_2, column_3 WHERE column_1 = 'some value' AND column_2 LIKE '%another value%' GROUP BY column_3"

A very good example is in this link: DataTables server-side processing example

Where you may modify this part:

/* Individual column filtering */
    for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {
        if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
        {
            if ( $sWhere == "" )
            {
                $sWhere = "WHERE ";
            }
            else
            {
                $sWhere .= " AND ";
            }
            $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
        }
    }

And in this way the data that will compose your json output to be passed to and parsed by jquery dataTable will be filtered desirably.

Sign up to request clarification or add additional context in comments.

Comments

1

whether you are trying to search data in the table ? If so below is the part

function doSearch() {
        var searchText = document.getElementById('searchTerm').value;
        var targetTable = document.getElementById('mytable');
        var targetTableColCount;

        //Loop through table rows
        for (var rowIndex = 0; rowIndex < targetTable.rows.length; rowIndex++) {
            var rowData = '';

            //Get column count from header row
            if (rowIndex == 0) {
                targetTableColCount = targetTable.rows.item(rowIndex).cells.length;
                continue; //do not execute further code for header row.
            }

            //Process data rows. (rowIndex >= 1)
            for (var colIndex = 0; colIndex < targetTableColCount; colIndex++) {
                var cellText = '';

                if (navigator.appName == 'Microsoft Internet Explorer')
                    cellText = targetTable.rows.item(rowIndex).cells.item(colIndex).innerText;
                else
                    cellText = targetTable.rows.item(rowIndex).cells.item(colIndex).textContent;

                rowData += cellText;

            }

            //If search term is not found in row data
            //then hide the row, else show
            if (rowData.indexOf(searchText) == -1)
                targetTable.rows.item(rowIndex).style.display = 'none';
            else
                targetTable.rows.item(rowIndex).style.display = 'table-row';
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.