snipe-it icon indicating copy to clipboard operation
snipe-it copied to clipboard

Request: Filter option in column headers

Open olivermarshall2 opened this issue 10 years ago • 17 comments

Hi

I'd like to see filter options in the column headers of all table views. Currently you can sort on all column headers, which is good, but it would be good to be able to filter on each one as well.

Such as being able to show only items with a category "Apple Macbook" with a purchase date of 2015.

Olly

olivermarshall2 avatar Feb 05 '16 11:02 olivermarshall2

Just signed up to second this--I've been using Snipe IT for a few months now and love it...having the ability to granularly filter data rather than sorting alone would be a huge plus.

I work for a K-8 school district and we've got about 7,000 assets (always growing too). I can search by location or model, etc to limit things somewhat, but the ability to combine searches/filters would be so incredibly helpful for me and my staff.

-Rob

onekmilesbehind avatar Feb 09 '16 23:02 onekmilesbehind

I would also appreciate this feature.

SylentBobNJ avatar Feb 16 '16 18:02 SylentBobNJ

Agreed. Would like to see this feature.

rburkholder2305 avatar Sep 20 '16 19:09 rburkholder2305

We all would. Unfortunately, the javascript library we use for data tables is a little wonky with this stuff when it's server-side sorting.

snipe avatar Sep 20 '16 19:09 snipe

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions!

stale[bot] avatar Jan 20 '18 22:01 stale[bot]

I hate to reopen an old issue but since it was closed as stale, I'd like to query on if this would be possible? I would like to be able to search for all assets with 'hp' but where the serial number is blank or purchase/warranty date is blank/below a certain value. Can this be done with the current version or v5?

Jaspotn0612 avatar Feb 28 '20 00:02 Jaspotn0612

Also interested in the feature will it ever happen? Would be extremely useful to filter by date for licenses to track renewals, or at least have a generic custom report that's not just for assets!

namarie23 avatar May 28 '20 18:05 namarie23

This feature is crucial to using SnipeIT. As a new user, I am very surprised that SnipeIT has no way of drilling down into categories and filtering views. If the norm is to wade into several thousands of rows of data to find a category associated to an asset, then it's nearly impossible to use.

The only alternative is to bookmark the Categories setup page which at least allows you to drill into a specific category and view assets associated with it.

gpsdev1 avatar Dec 06 '22 14:12 gpsdev1

I hate to reopen an old issue but since it was closed as stale, I'd like to query on if this would be possible? I would like to be able to search for all assets with 'hp' but where the serial number is blank or purchase/warranty date is blank/below a certain value. Can this be done with the current version or v5?

I think it should be possible. I recently added a column filter for assets listing that's somewhat configurable (filter types, filter key, etc). image

amiraiman avatar Oct 24 '24 09:10 amiraiman

I can't believe I'm commenting on something like this again in 2025, but shouldn't this be a standard feature? I hate having to export the assets to Excel every time and filter and search there for what I need. I got SnipeIT to replace Excel, not to have to keep searching for everything in Excel.

aPollO2k avatar Apr 03 '25 15:04 aPollO2k

I can't believe I'm commenting on something like this again in 2025,

Yeah, me either.

If it was simple to do, we would have done it. We have to worry about more than just your implementation. How different localizations affect the UI, etc. The js library we use does support filters on headers, but the UX is generally terrible, and it conflicts with other libraries.

That said, we welcome your pull request.

snipe avatar Apr 03 '25 15:04 snipe

@amiraiman If you'd like to share that code, we can see what we can do with it to move this forward. The current BS tables implementation still looks awful and doesn't seem to work very well, at least not with server-side data.

snipe avatar Apr 03 '25 17:04 snipe

@amiraiman - just a follow up here - would you be able to share that code?

snipe avatar Jun 30 '25 10:06 snipe

@snipe - I use bootstrap table filter extension for the column filters. It's already included in the bootstrap-table npm module installed in this project, so I just had to update the webpack.mix.js to include it in the project.

As for the filter usage, I added data-filter-control="true" to the table element that I want to have the column filter on to enable it, then I include the necessary column options attributes (i.e data-filter-control & data-filter-data) through AppPresenter and filter them in the model's query scope filter method scopeByFilter().

For instance to add a select column filter on the Assets table to be able to filter the assets by each individual Asset Model, I did the following changes:

One-time changes:

  1. Include the extension in webpack
  2. In the API app/Http/Controllers//Api/AssetsController.php @ line 151, I changed the conditionals to allow filtering & searching at the same time.
if ($request->filled('search')) {
    $assets->TextSearch($request->input('search'));
}

if ((!is_null($filter)) && (count($filter) > 0)) {
    $assets->ByFilter($filter);
}
  1. Add the data-filter-control="true" & data-filter-field-key-map="tableColumnName:dbColumnName" to resources/views/hardware/index.blade.php.
  2. Update queryParams processing in resources/views/partials/bootstrap-table.blade.php. This is where i use the data-filter-field-key-map attribute (a custom attribute I made up, nothing to do with the extension) to bind the table column name set in AssetPresenter with the actual db column name I'll be querying in the API. It's basically a comma separated list of 'tableColumnName:dbColumnName'. I.e if I have multiple column filters, the attribute would look like data-filter-field-key-map="model:model_id,category:category_id". Then in the queryParams config, I would read the value and split it, loop through every pairs, and then update the query parameters to use the actual db column instead of the column name.

Changes for every column filter:

  1. Add table column & db column map to data-filter-field-key-map attribute in resources/views/hardware/index.blade.php.
  2. Add the filterControl & filterData column attributes on app/Presenters/AssetPresenter.php
[
    'field' => 'model',
    'filterControl' => "select",
    'filterData' => 
        "json:" . json_encode(
            AssetModel::select(
                [
                    'models.id',
                    'models.name',
                ]
            )->get()
                ->mapWithKeys(fn ($item) => [$item->id => $item->name])
                ->toArray()
        ),
    'searchable' => true,
    'sortable' => true,
    'title' => trans('admin/hardware/form.model'),
    'visible' => true,
    'formatter' => 'modelsLinkObjFormatter',
]
  1. Exclude the model_id attribute from being searched globally & add the model_id filter clause in the query scope in app/Models/Asset.php. I.e in the scopeByFilter($query, $filter) method, I inserted a new filter
if ($fieldname == 'model_id') {
    $query->where(function ($query) use ($search_val) {
        $query->whereHas('model', function ($query) use ($search_val) {
            $query->where('models.id', '=', $search_val);
        });
    });
}

and then at the end exclude it in the global search scope

if (($fieldname!='category') && ($fieldname!='category_id') && ($fieldname!='model_number') && ($fieldname!='model_id') && ($fieldname!='rtd_location') && ($fieldname!='location') && ($fieldname!='supplier')
                    && ($fieldname!='status_label') && ($fieldname!='status_label_id') && ($fieldname!='assigned_to') && ($fieldname!='model') && ($fieldname!='company') && ($fieldname!='manufacturer')) {
                        $query->where('assets.'.$fieldname, 'LIKE', '%' . $search_val .
}

P/S: I added this functionality to be used in our local instance of snipe-it, and it is only used by our SysAdmin and not our employees. I did not do any further testing / checks on data leakage through when adding the custom filter in the API request, etc since our SysAdmin has admin privileges and they are the sole user of the system

amiraiman avatar Jun 30 '25 12:06 amiraiman

@amiraiman - thanks a ton for this explanation. I had attempted to use that extension as well, but it would never pick up the select2 (so it looked crappy) and of course we'll need to figure out how to handle access and such, but this is a big help! Much appreciated!

snipe avatar Jun 30 '25 12:06 snipe

@snipe - no problem, glad I could help.

we'll need to figure out how to handle access and such

Yeah, I guess this is the hardest part of making this feature (access control, custom fields, filter types, etc). In my case, it's pretty much clear cut which columns they want a filter on, as well as what type of filter they would like so I could just hard coded those in the codebase, but when you need to dynamically generate those filters based on user input, it's definitely much more work than my proposed workaround.

Good luck!

amiraiman avatar Jun 30 '25 14:06 amiraiman

Do you think in your experiments that we could implement the standard (access controlled) select2 lists here? vs this:

 "json:" . json_encode(
            AssetModel::select(
                [
                    'models.id',
                    'models.name',
                ]
            )->get()
                ->mapWithKeys(fn ($item) => [$item->id => $item->name])
                ->toArray()
        ),

We already have selectlist endpoints for all major models (locations. assets, companies, categories, etc). I haven't dug too much into this, but it would be a lot cleaner if we could re-use the same lists we use in the GUI

snipe avatar Jun 30 '25 17:06 snipe