Browse Source

Add Column.getValueProvider to Grid (#8732)

* Add Column.getValueProvider to Grid

Fixes #8680
tags/8.1.0.alpha1
Artur 7 years ago
parent
commit
45921980b2

+ 15
- 4
server/src/main/java/com/vaadin/ui/Grid.java View File

@@ -645,8 +645,8 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
.hasKey(diffStateKey) : "Field name has changed";
Type type = null;
try {
type = getState(false).getClass()
.getField(diffStateKey).getGenericType();
type = getState(false).getClass().getField(diffStateKey)
.getGenericType();
} catch (NoSuchFieldException | SecurityException e) {
e.printStackTrace();
}
@@ -887,7 +887,8 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
if (hasCommonComparableBaseType(a, b)) {
return compareComparables(a, b);
} else {
return compareComparables(Objects.toString(a, ""), Objects.toString(b, ""));
return compareComparables(Objects.toString(a, ""),
Objects.toString(b, ""));
}
}

@@ -907,7 +908,7 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
}
}
if ((a == null && b instanceof Comparable<?>)
|| (b == null && a instanceof Comparable<?>)) {
|| (b == null && a instanceof Comparable<?>)) {
return true;
}

@@ -1075,6 +1076,16 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
return this;
}

/**
* Gets the function used to produce the value for data in this column
* based on the row item.
*
* @return the value provider function
*/
public SerializableFunction<T, ? extends V> getValueProvider() {
return valueProvider;
}

/**
* Sets whether the user can sort this column or not.
*

+ 48
- 0
server/src/test/java/com/vaadin/tests/components/grid/GridValueProvider.java View File

@@ -0,0 +1,48 @@
/*
* Copyright 2000-2016 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.tests.components.grid;

import org.junit.Assert;
import org.junit.Test;

import com.vaadin.tests.data.bean.Person;
import com.vaadin.tests.data.bean.Sex;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.Column;

public class GridValueProvider {

@Test
public void getExplicitValueProvider() {
Grid<Person> grid = new Grid<>();
Column<Person, String> col = grid.addColumn(
person -> person.getFirstName() + " " + person.getLastName());
Person person = new Person("first", "last", "email", 123, Sex.UNKNOWN,
null);
Assert.assertEquals("first last", col.getValueProvider().apply(person));
}

@Test
public void getBeanColumnValueProvider() {
Grid<Person> grid = new Grid<>(Person.class);
Column<Person, String> col = (Column<Person, String>) grid
.getColumn("email");
Person person = new Person("first", "last", "eeemaaail", 123,
Sex.UNKNOWN, null);
Assert.assertEquals("eeemaaail", col.getValueProvider().apply(person));

}
}

Loading…
Cancel
Save