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

Hard Limit in Config (for Pagination) #2494

Closed
wants to merge 6 commits into from
Closed

Hard Limit in Config (for Pagination) #2494

wants to merge 6 commits into from

Conversation

saaiful
Copy link

@saaiful saaiful commented Oct 25, 2020

@saaiful saaiful closed this Oct 25, 2020
@saaiful saaiful reopened this Oct 25, 2020
@yajra yajra added Hacktoberfest hacktoberfest-accepted Accepted for Hacktoberfest labels Oct 31, 2020
@@ -119,4 +119,10 @@
'options' => 0,
],

/**
* Maximum record per page
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the correct word is records?

Maximum records per page.

@yajra
Copy link
Owner

yajra commented Oct 31, 2020

PR looks good aside from the minor typo? Will test this today on actual project. Thanks!

@yajra
Copy link
Owner

yajra commented Oct 31, 2020

Maybe we can also include records_per_page on the config while we're at it instead of hard coding it at 10?

Ignore this one, can be discussed on another PR.

@yajra
Copy link
Owner

yajra commented Oct 31, 2020

Just tested this and it breaks the dataTables info? Tried setting the max records to 3 and the requested length is 10.

image

Expected info should be something like Showing 1 to 3 of 10 results or I might be missing something?

@saaiful
Copy link
Author

saaiful commented Nov 1, 2020

Just tested this and it breaks the dataTables info? Tried setting the max records to 3 and the requested length is 10.

image

Expected info should be something like Showing 1 to 3 of 10 results or I might be missing something?

I will try to fix that.

@saaiful
Copy link
Author

saaiful commented Jan 15, 2021

I just solved the issue.
In Yajra\DataTables\Html\Builder

     * Generate DataTables js parameters.
     *
     * @param  array $attributes
     * @return string
     */
    public function parameterize($attributes = [])
    {
        $parameters = (new Parameters($attributes))->toArray();

        $max_record_per_page = $this->config->get('datatables.max_record_per_page', 0);
        if (!array_key_exists('lengthMenu', $parameters)) {
            $parameters['lengthMenu'] = [10, 25, 50, 100];
        }

        if ($max_record_per_page != 0) {
            $lengthMenu = array_unique($parameters['lengthMenu']);
            foreach ($lengthMenu as $key => $value) {
                if ($value > $max_record_per_page) {
                    unset($lengthMenu[$key]);
                }
            }
            $lengthMenu = array_values($lengthMenu);
            if (empty($lengthMenu)) {$lengthMenu = [$max_record_per_page];}
            sort($lengthMenu);
            $parameters['lengthMenu'] = $lengthMenu;
        }

        $values = [];
        $replacements = [];

        foreach (Arr::dot($parameters) as $key => $value) {
            if ($this->isCallbackFunction($value, $key)) {
                $values[] = trim($value);
                Arr::set($parameters, $key, '%' . $key . '%');
                $replacements[] = '"%' . $key . '%"';
            }
        }

        $new = [];
        foreach ($parameters as $key => $value) {
            Arr::set($new, $key, $value);
        }

        $json = json_encode($new);

        $json = str_replace($replacements, $values, $json);

        return $json;
    }

This will insert lengthMenu parameter according to max_record_per_page in config file. This will not affect manual lengthMenu insertion in "builder parameters".

@yajra

@saaiful saaiful closed this Jan 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hacktoberfest hacktoberfest-accepted Accepted for Hacktoberfest
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants