How do I add a CSS class to a row in a TelerikGrid - grid

I need to apply a class to each row where the related "TransactionsRO" record's ColorControl = 'F'. I have tried using an #if statement in the HTML (see below), but I can't figure out how to define "rowRecord." I also tried a button with the "Test()" method, but I need help with the "Code goes here" line.
<TelerikGrid Data="#GridDataSource"
EditMode="#Telerik.Blazor.GridEditMode.Incell"
#ref="Grid"
SelectionMode="Telerik.Blazor.GridSelectionMode.Multiple"
#*...*#>
<GridToolBar>
<TelerikButton OnClick="#Test()">Click me</TelerikButton>
</GridToolBar>
<GridColumns>
<GridColumn Field="#nameof(TransactionsRO.ColorControl)" Width="50px"></GridColumn>
#*#if("rowRecord" == 'F')
{
class = myCssClass
}*#
<GridCheckboxColumn CheckBoxOnlySelection="true"
Width="50px" />
</GridColumns>
</TelerikGrid>
#code {
[Parameter]
public TransactionsCollection GridDataSource { get; set; }
public TelerikGrid<TransactionsRO> Grid;
public EventCallback Test(){
List <TransactionsRO> gridRows = Grid.Data.ToList();
List<int> fRows = new List<int>();
foreach (TransactionsRO row in gridRows)
{
if(row.ColorControl == 'F'.ToString()){
int rowNum = gridRows.IndexOf(row);
fRows.Add(rowNum);
}
}
foreach(TransactionsRO row in Grid){
//Code goes here
}
EventCallback placeHolder;
return placeHolder;
}
}

Use the rowrender and cellrender events (at the grid and column level respectively)

Related

Sort vaadin grid containing text field as component column

I am using vaadin 8. This grid contains a number of columns. Two columns are having textfield as component column because user wants to enter something in string format. Hence we use TextField component inside both columns. This is done by using grid.addComponentColumn method. Even after enabling setSorting(true), it seems that sorting is not working for both these columns.
addComponentColumn(DataGrid::getUserMessage).setId("userMessage").setSortable(true).setCaption("UserMessage");
i have tried below two things but still it is not sorting.
First
addComponentColumn(DataGrid::getUserMessage).setId("userMessage").setSortable(true).setCaption("UserMessage").setComparator((p1, p2) -> p1.getUserMessage().getValue().compareTo(p2.getUserMessage().getValue()));
Second
addComponentColumn(DataGrid::getUserMessage).setId("userMessage").setSortable(true).setCaption("UserMessage").setSortOrderProvider(direction -> Arrays.asList(new QuerySortOrder("userMessage", direction)).stream());
Data grid is the class which contains column names and its setter/getters.
How can I make this work? Can someone demonstrate it by a snippet
Update below solution works! This piece of code is for improvement for sorting containin null values while using comparator
#Override
public int compare(final DataGrid a, final DataGrid b) {
if (a.getUserMessage().getIntegerValue() == null && b.getUserMessage().getIntegerValue() == null) {
return 0;
}
if (a.getUserMessage().getIntegerValue() == null) {
return -1;
}
if (b.getUserMessage().getIntegerValue() == null) {
return 1;
}
return a.getUserMessage().getIntegerValue().compareTo(b.getUserMessage().getIntegerValue());
}
);```
Here is a minimal example,
List<Person> personList = new ArrayList<>();
personList.add(new Person("Lucas", "Lucas Message"));
personList.add(new Person("Samuel", "Samuel Message"));
personList.add(new Person("Aaron", "Aaron Message"));
Grid<Person> grid = new Grid<>();
grid.setItems(personList);
grid.addColumn(Person::getName).setCaption("Name");
grid.addComponentColumn(person -> {
TextField tf = new TextField();
tf.setValue(person.getMessage());
tf.addValueChangeListener(e -> {
person.setMessage(e.getValue());
});
return tf;
}).setId("customColumn").setComparator(
(p1, p2) -> p1.getMessage().compareTo(p2.getMessage()))
.setCaption("Message");
And the Person class
public class Person {
private String name;
private String message;
public Person(String name, String message) {
setName(name);
setMessage(message);
}
// Getters and Setters
}

ASP.NET Gridview truncate BoundField from code behind

I am adding a dynamic column to an ASP.NET grid view.
Code to add the dynamic column:
List<DataControlField> columns; // this contains all gridview columns.
BoundField boundField = new BoundField();
boundField.DataField = long_text_column.SortExpression;
boundField.SortExpression = long_text_column.SortExpression;
columns.Insert(0, boundField);
How can I trim / truncate long_text_column to show only first 15 characters on the UI.
NOTE: I do not want to trim at the database level for other reasons.
I would add an extra property to the class with only a get that returns long_text_column with a max length of 15.
public class DataControlField
{
public string long_text_column { get; set; }
public string long_text_column_max15
{
get
{
if (!string.IsNullOrEmpty(long_text_column) && long_text_column.Length > 15)
return long_text_column.Substring(0, 15);
else
return long_text_column;
}
}
}

Highlight Dates in Rich:calendar

I have a calendar which is working fine. it displays records on click and changes the colour to green or red. if records are available date will become red colored.
I don't want onclick I want calendar to displays date colours on
calendars onload event.** KIndly help. following is my code . I think problem with css, I guess
.postive-records .rf-cal-sel {
background-color: green;
}
.no-records .rf-cal-sel {
background-color: red;
}
<rich:calendar styleClass="#{eventMaster.eventMasterList!=null and eventMaster.eventMasterList.size()>0?'postive-records':'no-records'}" locale="EN"
id="searchDateCalendarcommon" popup="false"
showApplyButton="false" datePattern="dd/MM/yyyy"
value="#{eventMaster.eventSearchDate}" >
<rich:tooltip followMouse="false"
showDelay="100"
direction="topRight"
layout="block"
onmouseover="">
<h:outputText value="#{eventMaster.calendarTooltipText}"
style="color: red;font-size: 12px;"/>
</rich:tooltip>
<f:ajax event="change" listener="#{commonOutputContentBean.showEvents()}"
render="cmnoplayoutfrm searchDateCalendarcommon" />
</rich:calendar>
The calendar simply displays days in a month, it isn't linked to any data. Furthermore styleClass is applied to the entire calendar not to the cells. You need to provide a dataModel.
The datamodel has to implement org.richfaces.model.CalendarDataModel, and you also need to implement org.richfaces.model.CalendarDataModelItem for the days which is a plain object with getters and setters. The CalendarModel would be something like this:
public class MyCalendarModel implements CalendarDataModel {
private boolean hasEvents(Calendar calendar) {
// …
}
#Override
public CalendarDataModelItem[] getData(Date[] dateArray) {
CalendarDataModelItem[] modelItems = new MyCalendarModelItem[dateArray.length];
Calendar current = GregorianCalendar.getInstance();
Calendar today = GregorianCalendar.getInstance();
today.setTime(new Date());
CalendarModelItem modelItem;
for (int i = 0; i < dateArray.length; i++) {
current.setTime(dateArray[i]);
modelItem = new MyCalendarModelItem();
if (hasEvents(current)) {
modelItem.setStyleClass("postive-records");
} else {
modelItem.setStyleClass("no-records");
}
modelItems[i] = modelItem;
}
return modelItems;
}
#Override
public Object getToolTip(Date date) {
return null;
}
}
You can check the example in the showcase.

How to create table with dynamic amount of columns

Thare is a tutorial at javaFX documentation page. This example describes how to make tableView, if you have some sertain java class, which can tell you which columns you are going to have. (That is a Person class in this example).
But what if i do not have any specific class, and number of columns can vary from time to time?
In my case i have such data structure:
class TableData{
List<Row> rows; //A list with all my rows i need to have in my table
}
class Row{
List<Column> columns; //Cells\Columns for every row.
}
class Column{
Attribute attr; //Each column - is somethig like a wrapper for the real data i need to show in a cell;
}
class Attribute{ //My precues data
String name;
SupportingInfo info;
}
class SupportingInfo{//Some supporting fields...
String name;
String value;
//...etc....
}
So, my case is very similar to this one.
The only differents is that data from the case above is not binded with its representation in javaFX table (so, even if some one will make extra controls to edit this data in a tableView, the actual object with that data will never know about it.), because it(data) goes to the table like some strings, not like some objects;
So, what do i need - is to push data to the table (like that: table.setItems(tableData)), set some set Factories, to give user ability to edit data, and to have this edited data in my tableData object;
Here are some code ive tried to make for this purpose:
//prepare my table
private void createTableHeader(TableView table, List<Attribute> ias) {
int i = 0;
for (final Attribute ia : ias) {
final int j = i;
i++;
TableColumn tc = new TableColumn(ia.getName());
tc.setSortable(true);
tc.setCellValueFactory(new Callback<CellDataFeatures<List<Attribute>, String>, ObservableValue<String>>() {
#Override
public ObservableValue<String> call(CellDataFeatures<List<Attribute>, String> arg0) {
if(arg0.getValue().get(j).getSupportingInfo() == null){
arg0.getValue().get(j).setSupportingInfo(new SupportingInfo());
}
return new SimpleObjectProperty(arg0.getValue().get(j),"value");
}
});
table.getColumns().add(tc);
}
}
//loading some data to my tableView
private void createTableBody(TableView curTable, List<Row> rows) {
ObservableList<List<Attribute>> data = FXCollections.observableArrayList();
for (Row row : rows) {
data.add(row.getColumns());
}
curTable.setItems(data);
}
//this one is to define some extra controls for editing data in a table by users
private void makeCellFactory(TableColumn curTableCol, final Attribute templateIa, final Document doc) {
curTableCol.setCellFactory(new Callback<TableColumn, TableCell>() {
public TableCell call(TableColumn p) {
final EditingCell cell = new EditingCell(templateIa, doc);
return cell;
}
});
}
But, as a result, i have just empty rows in my table, with an ability to click some cell and recieve table editing controls. But there is not defult values in by table;
What am i doing wrong in my code?
Ok, i've found a solution:
ts.setCellFactory should look like this:
tc.setCellValueFactory(new Callback<CellDataFeatures<List<Attribute>, SupportingInfo>, ObservableValue<Attribute>>() {
#Override
public ObservableValue<Attribute> call(CellDataFeatures<List<Attribute>, SupportingInfo> arg0) {
return new SimpleObjectProperty(arg0.getValue().get(j),"value",arg0.getValue().get(j));
}
});
Also, this code is needed to catch new values and put the incoming data to the table:
tc.setOnEditCommit(new EventHandler<CellEditEvent<List<Attribute>, Attribute>>() {
#Override
public void handle(CellEditEvent<List<Attribute>, Attribute> t) { t.getTableView().getItems().get(t.getTablePosition().getRow()).get(j).setSupportingInfo(t.getNewValue().getSupportingInfo());
}
});

Lucene number of occurrences

I am using Lucene.net in my Web App.
Everithing works fine, but now i have to show the number of occurrences of my 'searchstring' in every single document of the hits array.
How can i do this? I use usual BooleanQuery.
That is my search:
BooleanQuery bq = new BooleanQuery();
bq.Add(QueryParser.Parse(Lquery, "", CurIndexDescritor.GetLangAnalizer()), false,false);
BooleanQuery.SetMaxClauseCount(int.MaxValue);
IndexSearcher searcher = new IndexSearcher(indexPath);
Hits hits = (filter != null) ? searcher.Search(bq, filter) : searcher.Search(bq);
for (int i = 0; i < hits.Length(); i++)
{
Document doc = hits.Doc(i);
SearchResultItem MyDb = new SearchResultItem();
MyDb.key = doc.Get(KeyField);
MyDb.score = hits.Score(i);
result.Add(MyDb);
}
Where can i get the number of occurrences?
Thanks!
If you dont want the score back and dont want to order the results using score you could probably build a custom Similarity implementation.
I quickly tested the following code, and it appears to work fine with TermQueries and PhraseQueries, i didnt test more query types tho. A PhraseQuery hit counts as a single occurence.
public class OccurenceSimilarity : DefaultSimilarity
{
public override float Tf(float freq)
{
return freq;
}
public override float Idf(int docFreq, int numDocs)
{
return 1;
}
public override float Coord(int overlap, int maxOverlap)
{
return 1;
}
public override float QueryNorm(float sumOfSquaredWeights)
{
return 1;
}
public override Explanation.IDFExplanation idfExplain(System.Collections.ICollection terms, Searcher searcher)
{
return CACHED_IDF_EXPLAIN;
}
public override Explanation.IDFExplanation IdfExplain(Term term, Searcher searcher)
{
return CACHED_IDF_EXPLAIN;
}
public override float SloppyFreq(int distance)
{
return 1;
}
private static Explanation.IDFExplanation CACHED_IDF_EXPLAIN = new ExplainIt();
private class ExplainIt : Explanation.IDFExplanation
{
public override string Explain()
{
return "1";
}
public override float GetIdf()
{
return 1.0f;
}
}
}
To use it:
Similarity.SetDefault(new OccurenceSimilarity());

Resources