Laravel-Backpack / community-forum

A workspace to discuss improvement and feature ideas, before they're actually implemented.
28 stars 0 forks source link

[Bug] #807

Closed rawad1 closed 8 months ago

rawad1 commented 8 months ago

I have the function to show the table:

protected function setupListOperation()
{
    if (backpack_auth()->user() && backpack_auth()->user()->is_super_admin == 0) {
        $this->crud->addClause('where', 'added_by', backpack_auth()->user()->id);
    }

    CRUD::column('created_at');
    CRUD::column('phone');
    CRUD::addColumn([
        'label' => "Country", // Table column heading
        'type' => "select",
        'name' => 'country', // the column that contains the ID of that connected entity;
        'entity' => 'country_rel', // the method that defines the relationship in your Model
        'attribute' => "name", // foreign key attribute that is shown to user
        'model' => Country::class, // foreign key model
    ]);
    CRUD::addColumn([
        'label' => "Added by", // Table column heading
        'type' => "select",
        'name' => 'added_by', // the column that contains the ID of that connected entity;
        'entity' => 'added_by_rel', // the method that defines the relationship in your Model
        'attribute' => "name", // foreign key attribute that is shown to user
        'model' => User::class, // foreign key model
    ]);
    CRUD::addColumn([
        'name' => 'source_label',
        'label' => 'Source',
        'type' => 'text',
    ]);
    CRUD::addColumn([
        'name' => 'status_label',
        'label' => 'Status',
        'type' => 'text',
    ]);
    CRUD::addColumn([
        'label' => "Export Batch", // Table column heading
        'type' => "select",
        'name' => 'export_date', // the column that contains the ID of that connected entity;
        'entity' => 'export_rel', // the method that defines the relationship in your Model
        'attribute' => "name", // foreign key attribute that is shown to user
        'model' => Exports::class, // foreign key model
    ]);
    CRUD::addColumn([
        'label' => "Export By", // Table column heading
        'type' => "select",
        'name' => 'exported_by', // the column that contains the ID of that connected entity;
        'entity' => 'export_rel', // the method that defines the relationship in your Model
        'attribute' => "exported_user", // foreign key attribute that is shown to user
        'model' => Exports::class, // foreign key model
    ]);

    /**
     * Columns can be defined using the fluent syntax or array syntax:
     * - CRUD::column('price')->type('number');
     * - CRUD::addColumn(['name' => 'price', 'type' => 'number']);
     */

    $countries = Country::pluck('name', 'id')->all();
    $this->crud->addFilter([
        'name' => 'country',
        'type' => 'select2',
        'label' => 'Country',
    ],
        $countries,
        function ($value = null) {
            if ($value !== null) {
                $this->crud->addClause('where', 'country', $value);
            }
        });
    $sources = [
        'Facebook',
        'Twitter',
        'Telegram',
        'Wechat',
        '手工Manual',
        '招聘Recruit'
    ];

    $this->crud->addFilter([
        'name' => 'source',
        'type' => 'select2',
        'label' => 'Source',
    ],
        $sources,
        function ($value = null) {
            if ($value !== null) {
                $this->crud->addClause('where', 'source', $value);
            }
        });

    $this->crud->addFilter([
        'name' => 'status',
        'type' => 'select2',
        'label' => 'Status',
    ], [
        '0' => 'Not Exported',
        '1' => 'Exported',
    ], function ($value = null) {
        if ($value !== null) {
            $this->crud->addClause('where', 'status', $value);
        }
    });
    $users = User::pluck('name', 'id')->all();
    $this->crud->addFilter([
        'name' => 'added_by',
        'type' => 'select2',
        'label' => 'Admin',
        'entity' => 'added_by', // Name of the relationship method in the model
        'attribute' => 'name', // Attribute to display in the filter dropdown
    ], $users, function ($value = null) {
        if ($value !== null) {
            $this->crud->addClause('where', 'added_by', $value);
        }
    });
    $this->crud->addFilter([
        'type' => 'date_range',
        'name' => 'from_to',
        'label' => 'Date range'
    ],
        false,
        function ($value) { // if the filter is active, apply these constraints
            $dates = json_decode($value);
            $this->crud->addClause('where', 'created_at', '>=', $dates->from);
            $this->crud->addClause('where', 'created_at', '<=', $dates->to . ' 23:59:59');
        });

    $this->crud->button('export')->view('crud::buttons.quick')->meta([
        'label' => 'New Export',
        'icon' => 'la la-download',
        'wrapper' => [
            // 'element' => 'a',
            'href' => '#',  // Prevent the default action
            'id' => 'ajax-export',  // Add an ID to the button
            'data-ajax' => route('export') ,
            'title' => 'Export Data',
        ]
    ]);

it works fine locally but the export button is not showing

pxpm commented 8 months ago

@rawad1 there is no such thing as data-ajax in the quick button.

You should configure the action in the button wrapper as shown in the docs:

'wrapper' => [
        'element' => 'a',
        'href' => route('my.route.name'),
        'target' => '_blank', // open in a new browser window
        'title' => 'tooltip that's shown to the user',
    ]

Please have a look at the quick button docs

And also don't forget to enable access to that button with CRUD::allowAccess('Export').

Cheers