Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datatable: data with function, doesn't work sort #88

Open
jlopes90 opened this issue Jun 5, 2022 · 1 comment
Open

datatable: data with function, doesn't work sort #88

jlopes90 opened this issue Jun 5, 2022 · 1 comment

Comments

@jlopes90
Copy link

jlopes90 commented Jun 5, 2022

I use data: function(row) and it works but sorting doesn't work.

Example:

const columns = [];
columns.push({
    data: function(row) {
        return row.id;
    }
});
columns.push({
    data: function(row) {
        return row.name;
    }
});
columns.push({
    data: function(row) {
        const elString = jsoperation.modal(row.json);
        return elString;
    }
});

$(document).ready(function () {
    $('#example').dataTable({
        "serverSide": true,
        "ajax": "example",
        "columns": columns
    });
});
@jlopes90
Copy link
Author

jlopes90 commented Jun 6, 2022

I made the changes more or less and it works fine.


public function setColumnAttributes(): void
{
$columns = $this->options->columns();
if ($columns) {
foreach ($columns as $attr) {
$index = $attr['data']['_'] ?? $attr['data'];
if ($this->columns->visible()->isExists($index)) {
$this->columns->visible()->get($index)->attr = $attr;
}
}
}
}

to

public function setColumnAttributes(): void
{
    $columns = $this->options->columns();
    if ($columns) {
        foreach ($columns as $key => $attr) {
            $index = $attr['data']['_'] ?? $attr['data'];

            if ($index === 'function') {
                $index = $key;
            }

            if ($this->columns->visible()->isExists($index)) {
                $this->columns->visible()->get($index)->attr = $attr;
            }
        }
    }
}

protected function orderBy(): string
{
$orders = $this->options->order();
$orders = array_filter($orders, function ($order) {
return \in_array($order['dir'], ['asc', 'desc'],
true) && $this->columns->visible()->offsetGet($order['column'])->isOrderable();
});
$o = [];
foreach ($orders as $order) {
$data = $this->options->columns()[$order['column']]['data'];
$id = $data['sort'] ?? $data['_'] ?? $data;
if ($this->columns->visible()->isExists($id)) {
$o[] = $this->columns->visible()->get($id)->name.' '.$order['dir'];
}
}
if (\count($o) === 0) {
if ($this->hasDefaultOrder()) {
return '';
}
$o[] = $this->defaultOrder();
}
return $this->db->makeOrderByString($o);
}

to

protected function orderBy(): string
{
    ...

    foreach ($orders as $order) {
        $data = $this->options->columns()[$order['column']]['data'];
        $id = $data['sort'] ?? $data['_'] ?? $data;

        if ($id === 'function') {
            $id = $order['column'];
        }

        if ($this->columns->visible()->isExists($id)) {
            $o[] = $this->columns->visible()->get($id)->name.' '.$order['dir'];
        }
    }

    ...
}

@jlopes90 jlopes90 changed the title datatable: data with function datatable: data with function, doesn't work sort Jun 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant