0
votes

Is there any way to preselect a select or other field value when creating a new entity based on the applied filters?

I am trying to ease the work when adding multiple new records in Backpack and pre-filling some data base on the filters applied to the listing would help me a lot.

1
what did you try? show us some code. You could always make a custom select field and add as many params as you want - Indra

1 Answers

1
votes

There's no way to natively do that, but you could customize things to achieve this:

  • overwrite the create button by having a file in your resources/views/vendor/backpack/crud/buttons/create.blade.php so that you could place an ID on that button, say... #crud_add_button;
  • insert some javascript in your public/vendor/backpack/crud/js/list.js that upon DataTable filtering (or search) it places all the GET parameters on the #create_add_button too;
  • this way, when you click the add button the URL could look like http://backpackapp.local/admin/monster/create?draft=1,category=23; that's what would actually get clicked by the user;
  • in your EntityCrudController, have a create() method that overwrites the one in CrudController; this method could set default parameters on each field, based on the GET parameters, then return parent::create(); [note: this method might not even be needed, since I believe Laravel merges GET, POST and OLD parameters, and default is already configured to pick up the value if it's in a parameter; but I'm not sure];

Hope it helps.