使用多个视图和修改列进行过滤

问题描述 投票:0回答:2

我有一个 ApplicationCrudController,它有一个默认视图,以及一个在列表设置函数内称为“简化视图”的自定义视图。但是,当我在“简化视图”中尝试应用过滤器时,我总是被重定向回默认设置,我该怎么办?

此外,在我的简化视图中,我将多行分组为一行,然后将具有相同“partid”的行中的“makeid”和“modelid”列连接在一起。我应该如何更改我的品牌和型号过滤器,以便在应用时,使用过滤后的品牌和型号重新生成连接的字符串?

非常感谢您的宝贵时间,非常感谢您的帮助!

//ApplicationCrudController.php
   public function setupSimplifiedView(){        

        CRUD::modifyColumn('regionid', [
            'name' => 'regionid',          // column name in the CRUD list view
            'label' => 'Region',    // label for the column
            'type' => 'select',
            'entity' => 'region',  // The relationship chain
            'attribute' => 'regionabbr',  // The column to display (region name)
            'model' => "App\Models\Region",  // The related model
            'relation_type' => 'BelongsTo',
        ]);

        CRUD::modifyColumn('positionid', [
            'name' => 'positionid',         
            'label' => 'Position',    
            'type' => 'select',
            'entity' => 'position',  
            'attribute' => 'Position',  
            'model' => "App\Models\Position",  
            'relation_type' => 'BelongsTo',
        ]);

       

        // CRUD::column('start')->type('text');
        // CRUD::column('end')->type('text');
        CRUD::column('updatetime')->type('datetime')->format('YYYY-MM-DD');
        CRUD::column('releasetime')->type('datetime')->format('YYYY-MM-DD');
        
        CRUD::addClause('where', 'del_flg', 0); // Hide soft deleted items
        CRUD::removeColumn('del_flg');
        CRUD::removeColumn('modelid');
        CRUD::removeColumn('makeid');
        CRUD::removeColumn('start');
        CRUD::removeColumn('end');
        CRUD::removeColumn('remark');
        CRUD::removeColumn('editor');

        CRUD::groupBy('partid');

        CRUD::addColumn([
            'name' => 'makes_and_models',
            'label' => 'Application',
            'type' => 'model_function', // Specify that this is a model function column
            'function_name' => 'getMakesAndModels', // The method defined in application model
            'limit' => 100,
        ])->afterColumn('positionid');

        $this->crud->denyAccess('delete');
        $this->crud->denyAccess('update');

    }


    /**
     * Define what happens when the List operation is loaded.
     * 
     * @see  https://backpackforlaravel.com/docs/crud-operation-list-entries
     * @return void
     */
    protected function setupListOperation()
    { 
        CRUD::setFromDb(); 

        CRUD::modifyColumn('modelid', [
            'name' => 'modelid',
            'label' => 'Model',
            'type' => 'select',
            'entity' => 'carmodel',  
            'attribute' => 'ModelName',   
            'model' => "App\Models\Carmodel", 
            'relation_type' => 'BelongsTo',
        ]);

        CRUD::modifyColumn('makeid', [
            'name' => 'makeid',
            'label' => 'Make',
            'type' => 'select',
            'entity' => 'make',  
            'attribute' => 'MakeName',   
            'model' => "App\Models\Make",  
            'relation_type' => 'BelongsTo',
        ]);



        CRUD::modifyColumn('regionid', [
            'name' => 'regionid',         
            'label' => 'Region',    
            'type' => 'select',
            'entity' => 'region',  
            'attribute' => 'regionabbr',  
            'model' => "App\Models\Region",
            'relation_type' => 'BelongsTo',
        ]);

        CRUD::modifyColumn('positionid', [
            'name' => 'positionid',         
            'label' => 'Position',    
            'type' => 'select',
            'entity' => 'position',  
            'attribute' => 'Position', 
            'model' => "App\Models\Position", 
            'relation_type' => 'BelongsTo',
        ]);

       

        CRUD::column('start')->type('text');
        CRUD::column('end')->type('text');
        CRUD::column('updatetime')->type('datetime')->format('YYYY-MM-DD');
        CRUD::column('releasetime')->type('datetime')->format('YYYY-MM-DD');
        
        CRUD::addClause('where', 'del_flg', 0); // Hide soft deleted items
        CRUD::removeColumn('del_flg');



        $this->runCustomViews([
            'setupSimplifiedView' => __('Simplified View'),
            //'setupDatabaseView' => __('Applications'),
            //..
        ]);
        

        // Filters

        CRUD::filter('region')
            ->type('select2')
            ->values(function() { return \App\Models\Region::all()->keyBy('regionid')->pluck('regionabbr', 'regionid')->toArray();}
            )
            ->whenActive(function ($value) {
                CRUD::addClause('where', 'regionid', $value);
            })->apply();

        CRUD::filter('make')
            ->type('select2')
            ->values(function() { 
                return \App\Models\Make::all()->keyBy('MakeID')->pluck('MakeName', 'MakeID')->toArray();}
            )
            ->whenActive(function ($value) {
                CRUD::addClause('where', 'MakeID', $value);
            })->apply();

        CRUD::filter('model')
            ->type('select2')
            // ->values(backpack_url('application/fetch/models'))
            // ->method('POST')
            ->values(function () {
                $makeId = request()->get('make');  
                if ($makeId) {
                    $modelIds = \App\Models\Basevehicle::where('MakeID', $makeId)
                                               ->pluck('ModelID')
                                               ->toArray();
                    return \App\Models\Carmodel::whereIn('ModelID', $modelIds)
                                               ->pluck('ModelName', 'ModelID')
                                               ->toArray();
                }
        
                return [];
            })
            ->whenActive(function ($value) {
                CRUD::addClause('where', 'ModelID', $value);
            })->apply();
        
//Application.php
// ...
public function getMakesAndModels() {
        $partId = $this->partid;
        $applications = Application::where('partid', $partId)->where('del_flg', 0)->get(); // Not deleted & partid is equal

            $first = true; //first occurence of the application
            $applicationStr = '';
            
            $count = 0;
            $prevModel = '';
            $prevMake = '';
            $altStr = '';

            foreach ($applications as $application) {
                if ($count >= 5) {
                    break; //limit the application models to less than five
                }
                $make = \App\Models\Make::where('MakeID', $application->makeid)->value('MakeName');
                $model = \App\Models\Carmodel::where('ModelID', $application->modelid)->value('ModelName');
                
                
                if ($application->start == $application->end) {
                    $yearStr = substr($application->start, 2);
                } else {
                    $yearStr = substr($application->start, 2) . '-' . substr($application->end, 2);
                }

                $carStr = '';

                if (strcmp($make, $prevMake) != 0) { //not equal to previous make
                    $carStr .= ' '.$make;
                    $count++;
                }

                if (strcmp($model, $prevModel) != 0) { //not equal to previous model
                    $carStr .= ' '.$model;
                }

                // Append each row of info to the application string
                // First occrerence without comma
                if ($first) {
                    $applicationStr .= $yearStr. $carStr;
                    $first = false;
                } else {
                    $applicationStr .= ', ' . $yearStr. $carStr;
                }

                $prevModel = $model;
                $prevMake = $make;
            }

            return $applicationStr; 
    }

我尝试在简化视图中添加过滤器,但它保持不变。

php laravel eloquent laravel-backpack
2个回答
0
投票

自定义视图(用于ListOperation)当前版本v6不支持过滤器,也许将来会支持,就像v7一样。


-1
投票
    // Load simplified view and add a custom parameter
return redirect()->to('application?view=simplified');

//Modify your setupListOperation() to check for the view=simplified parameter:

    protected function setupListOperation()
{
    $view = request()->get('view');
    if ($view == 'simplified') {
        $this->setupSimplifiedView();
    } else {
        CRUD::setFromDb();  // Default view setup
    }
}

// 第 1 步:修改过滤器以处理分组数据 确保您的过滤器正确应用于 makeid 和 modelid,并且它们应用于分组数据。这是一个例子:

    CRUD::filter('make')
    ->type('select2')
    ->values(function() { 
        return \App\Models\Make::all()->keyBy('MakeID')->pluck('MakeName', 'MakeID')->toArray(); 
    })
    ->whenActive(function ($value) {
        // Apply filter only for the rows that match the make
        CRUD::addClause('where', 'makeid', $value);
    })->apply();

CRUD::filter('model')
    ->type('select2')
    ->values(function () {
        $makeId = request()->get('make');  
        if ($makeId) {
            $modelIds = \App\Models\Basevehicle::where('MakeID', $makeId)->pluck('ModelID')->toArray();
            return \App\Models\Carmodel::whereIn('ModelID', $modelIds)->pluck('ModelName', 'ModelID')->toArray();
        }
        return [];
    })
    ->whenActive(function ($value) {
        CRUD::addClause('where', 'modelid', $value);
    })->apply();

//Step 2: Regenerate the Concatenated String in getMakesAndModels()
Now, modify the getMakesAndModels() method in the Application model so that it regenerates the concatenated makeid and modelid values after the filters have been applied.

    public function getMakesAndModels() {
    $partId = $this->partid;

    // Apply filters if present
    $applicationsQuery = Application::where('partid', $partId)->where('del_flg', 0);
    
    if (request()->has('make')) {
        $applicationsQuery->where('makeid', request()->get('make'));
    }
    
    if (request()->has('model')) {
        $applicationsQuery->where('modelid', request()->get('model'));
    }
    
    $applications = $applicationsQuery->get();  // Get filtered applications
    
    $applicationStr = '';
    $prevMake = '';
    $prevModel = '';
    $count = 0;
    $first = true;

    foreach ($applications as $application) {
        if ($count >= 5) {
            break;
        }

        $make = \App\Models\Make::where('MakeID', $application->makeid)->value('MakeName');
        $model = \App\Models\Carmodel::where('ModelID', $application->modelid)->value('ModelName');

        $yearStr = $application->start == $application->end ? substr($application->start, 2) : substr($application->start, 2) . '-' . substr($application->end, 2);
        $carStr = '';

        if ($make != $prevMake) {
            $carStr .= ' ' . $make;
            $count++;
        }

        if ($model != $prevModel) {
            $carStr .= ' ' . $model;
        }

        if ($first) {
            $applicationStr .= $yearStr . $carStr;
            $first = false;
        } else {
            $applicationStr .= ', ' . $yearStr . $carStr;
        }

        $prevMake = $make;
        $prevModel = $model;
    }

    return $applicationStr;
}

// 会话状态(替代方法):如果您不想在 URL 中传递视图状态,您可以将当前视图存储在会话中并在控制器方法中检查它。

    if (session()->has('view')) {
    $view = session()->get('view');
    if ($view == 'simplified') {
        $this->setupSimplifiedView();
    }
} else {
    CRUD::setFromDb();
}
© www.soinside.com 2019 - 2024. All rights reserved.