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

Ability to override getItemMetadata for non-group rows in DataView #262

Open
vlsi opened this issue Jul 20, 2018 · 5 comments
Open

Ability to override getItemMetadata for non-group rows in DataView #262

vlsi opened this issue Jul 20, 2018 · 5 comments

Comments

@vlsi
Copy link
Contributor

vlsi commented Jul 20, 2018

DataView always returns null for non-group rows in getItemMetadata:

SlickGrid/slick.dataview.js

Lines 455 to 471 in 14bb1a8

function getItemMetadata(i) {
var item = rows[i];
if (item === undefined) {
return null;
}
// overrides for grouping rows
if (item.__group) {
return options.groupItemMetadataProvider.getGroupRowMetadata(item);
}
// overrides for totals rows
if (item.__groupTotals) {
return options.groupItemMetadataProvider.getTotalsRowMetadata(item);
}
return null;

Use case: "add css class to the grid row when item has certain property".
metadata.cssClasses seems to be the way to add row css classes, however DataView does not allow to use that:

SlickGrid/slick.grid.js

Lines 1680 to 1686 in 14bb1a8

var metadata = data.getItemMetadata && data.getItemMetadata(row);
if (metadata && metadata.cssClasses) {
rowCss += " " + metadata.cssClasses;
}
stringArray.push("<div class='ui-widget-content " + rowCss + "' style='top:" + getRowTop(row) + "px'>");

@vlsi
Copy link
Contributor Author

vlsi commented Jul 20, 2018

PS. The sad thing with getItemMetadata is it is used in multiple places, and there's no way to provide the metadata lazily.
In other words, metadata.cssClasses and metadata.columns are used in different grid methods, however getItemMetadata has to return all of the metadata values every time.

@6pac
Copy link
Owner

6pac commented Jul 22, 2018

Perhaps this lack of row level DataView metadata needs addressing. There have been complaints about how often metadata is fetched, too. I'll be too busy for even minor re-architecting until september. Happy to accept sensible PRs in the meantime.

@6pac
Copy link
Owner

6pac commented Jul 22, 2018

I seem to recall that I added a way to define row css in my personal repo, then added it across to the main repo. I usually document these things by adding an example, rather than by adding to the wiki (bad, I know!).
Have you checked out the extended formatter behaviour in http://6pac.github.io/SlickGrid/examples/example2-formatters.html

@rohanc
Copy link

rohanc commented Oct 17, 2023

The example linked above seems to demonstrate adding CSS to cells, but not to whole rows.

There are various recipes on stackoverflow for adding CSS to whole rows, as long as you are using DataView. These involve overriding the getItemMetadata method. e.g.

https://stackoverflow.com/questions/8930167/how-do-i-add-a-css-class-to-particular-rows-in-slickgrid

function row_metadata(old_metadata_provider) {
  return function(row) {
    var item = this.getItem(row),
        ret = old_metadata_provider(row);

    if (item && item._dirty) {
      ret = ret || {};
      ret.cssClasses = (ret.cssClasses || '') + ' dirty';
    }

    return ret;
  };
}

dataView.getItemMetadata = row_metadata(dataView.getItemMetadata);

Since I upgraded from v2 to v5, I found I need to provide this context when calling old_metadata_provider:

ret = old_metadata_provider.call(this,row) || {}

Then it works again. I'm unsure whether const item=this.rows[row] or this.getItem(row) (as in the original from slick.dataview.ts) is more appropriate. Both seem to work.

@ghiscoding
Copy link
Collaborator

ghiscoding commented Oct 17, 2023

Since I upgraded from v2 to v5, I found I need to provide this context when calling old_metadata_provider:

ret = old_metadata_provider.call(this,row) || {}

Then it works again. I'm unsure whether const item=this.rows[row] or this.getItem(row) (as in the original from slick.dataview.ts) is more appropriate. Both seem to work.

You need to provide this because we migrated to TypeScript and ES Modules, we no longer use any var which previously had global access scope but since we now use TypeScript Classes, we need to provide the this scope in some cases, I did mention a similar migration step to be done for the DraggableGrouping here but yeah it's possible that there are other special cases like the one you just encountered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants