bgultekin / laravel4-datatables-package

Server-side handler of DataTables Jquery Plugin for Laravel 4
267 stars 108 forks source link

Sorting columns with mysql functions fails #54

Closed toddmcbrearty closed 10 years ago

toddmcbrearty commented 11 years ago

I found that if you are sorting on columns that have functions in the select such as CONCAT or DISTINCT it puts the functions in the where clauses. This also occurs if you alias a field. eq...( field_name as fn)

I've added this function in

private function cleanColumns( $cols )
{
        //just add in more functions if you use them
   //i add spaces before and after parenthesis if you do not remove the spaces below
        $_search = [
        'GROUP_CONCAT( ',
        'CONCAT( ',
        'DISTINCT( ',
        ',',
        ' )',
        'as',
    ];

    foreach ( $cols as $col )
    {
        $_column = explode( ' ' , str_replace( $_search, '', $col, $count ) );

        if ( $count > 0 )
        {
            $columns[] = array_shift( $_column );
        }
        else
        {
            $columns[] = end( $_column );
        }
    }

    return $columns;
}

then added this to the top of the filtering() function

$columns = $this->cleanColumns( $this->columns );

then just below

$copy_this = $this;

add

$copy_this->columns = $columns

then in this for loop

for ($i=0,$c=count($columns);$i<$c;$i++)

change

$this->columns to $columns

also in the ordering() function just after

    if(!is_null(Input::get('iSortCol_0')))

add

$columns = $this->cleanColumns( $this->last_columns );

then change

$this->last_columns to $columns 

in the for loop

seems to work let me know if there is a better way

MarkVaughn commented 11 years ago

can you create a pull request in order for us to test this code?