Skip to main content

Develop with FastTrack

DataGrid

The DataGrid widget is based on the KendoReact Data Grid component and displays content from /v1/search results in a table. Optional pagination controls allow users to navigate the pages in the results.

DataGrid MarkLogic setup

Content from documents in search results can be displayed in a DataGrid table. The content must be extracted and included in search results. To do this, the extract-document-data property can be used in the query options of the application. See Include document extracts in search results for details.

Display search results in a table

In addition to displaying content from documents, the DataGrid widget can display responses from a /v1/search in a table. Content for each result in the search response is displayed in a table row. For example, consider this /v1/search response payload:

{
    "snippet-format": "snippet",
    "total": 3,
    "start": 1,
    "page-length": 10,
    "selected": "include",
    "results": [
        {
            "index": 1,
            "uri": "/person/1001.json",
            "path": "fn:doc(\"/person/1001.json\")",
            "href": "/v1/documents?uri=%2Fperson%2F1001.json",
            "extracted": {
                "kind": "array",
                "content": [
                    {
                        "envelope": {
                            "entityType": "person",
                            "id": 1001,
                            "firstName": "Nerta",
                            "lastName": "Hallwood",
                            "title": "Marketing Manager",
                            "status": "active",
                            "activities": [
                                {
                                    "description": "Started at Fadeo",
                                    "ts": "2013-06-22"
                                },
                                {
                                    "description": "Promoted",
                                    "ts": "2019-08-15"
                                }
                            ]
                        }
                    }
                ]
            }
        },
        // more results...
    ],
    // more metadata...
}

Content from this payload can be displayed in a React application using this code:

import { useContext } from "react";
import "./App.css";
import { MarkLogicContext, SearchBox, DataGrid } from "ml-fasttrack";
 
function App() {
 
  const context = useContext(MarkLogicContext);
 
  const handleSearch = (params) => {
    context.setQtext(params?.q);
  } 

  const handleClick = (uri) => {
    console.log("Result clicked: " + uri)
  } 
 
  return (
    <div className="App">
      <div>
        <SearchBox onSearch={handleSearch}/>
      </div>
      <div>
        <DataGrid
          data={context.searchResponse.results}
          gridColumns={{
            gridColumn: [
              { title: "Index", field: "index" },
              { title: "URI", field: "uri" },
              {
                title: "First Name",
                cell: (props) => (<td>{(<span>{props?.dataItem?.extracted.content[0].envelope.firstName}</span>)}</td>)
              },
              {
                title: "Last Name",
                cell: (props) => (<td>{(<span>{props?.dataItem?.extracted.content[0].envelope.lastName}</span>)}</td>)
              },
              {
                title: "Actions",
                cell: (props) => (
                  <td 
                    onClick={() => handleClick(props?.dataItem?.uri)} 
                    style={{ color: '#0d6efd', cursor: 'pointer' }}
                  >
                    {(<span>{"Click me"}</span>)}
                  </td>
                )
              }
            ]
          }}
          pagerButtonCount={5}
          pageSizeChanger={[1, 2, 5]}
          paginationSize="medium"
          paginationFooter={true}
          showPreviousNext={true}
          showInfoSummary={true}
        />
      </div>
    </div>
  );
}
 
export default App;

Code explanation

In the example:

  • The data prop is set to the results in the search response in the application context. 

  • The search results are populated from the queries submitted by the SearchBox widget.

  • The table columns are configured with an array in the gridColumns prop.

  • The title property in each gridColumns object defines the title of the column.

  • The content in the columns for each result can be configured with a field property or cell callback function:

    • field property - use the property to define the path to a value at the result root. (For example, the index or uri of each search result).

    • cell callback function - this method displays values other than those at the result root. The callback receives an object argument with the dataItem property set to the result content. The callback must return content wrapped in a td tag to show a table cell.

DataGrid example rendering

The example code in Display search results in a table displays a table like this:

The example code in Display search results in a table displays this table.

DataGrid API

Property

Type

Description

data

object

Array of objects to display in the table.

gridColumns

GridColumnProps[]

Array of configuration objects that define each column in the table. See the KendoReact GridColumnProps

initialSort

SortDescriptor[]

Configuration for the initial sorting. See the KendoReact SortDescriptor.

sortable

boolean

Allow sorting in the columns.

resizable

boolean

Allow resizable columns.

style

CSSProperties

CSS styles applied to the widget.

onPageChange

((event: GridPageChangeEvent) => void)

Callback function to handle paging. Overrides the native paging function. See the KendoReact GridPageChangeEvent.

onSortChange

((event: GridSortChangeEvent) => void)

Callback function to handle sorting. Overrides the native sort function. See the KendoReact GridSortChangeEvent.

paginationHeader

boolean

Indicates whether to display pagination controls in the header.

paginationFooter

boolean

Indicates whether to display pagination controls in the footer.

pageSizeChanger

number[] | (string | number)[]

Numeric array for displaying a menu in the pagination for configuring the number of items on each page.

showPreviousNext

boolean

Whether to display previous and next buttons in the pagination.

showInfoSummary

boolean

Optional value for displaying the results summary in the pagination options.

paginationClassName

string

Class name applied to the pagination container.

paginationSize

"small" | "medium" | "large"  

Size of the pagination.

pagerButtonCount

number

The number of page buttons to display in the pagination controls.