I need to show an array of objects in the table like representation. Table has columns with the properties, and when clicked on the column it should show more data inside the table. It should be sortable.
Is there a JS library that could do this, so I dont have to write this from scratch?
Please see the attached image with the JSON object. When the user clicks on Ana, additional row is inserted.
I use datatables.net for all my "more complex than lists"-tables. I It's a very well kept library with loads of features and great flexibility. In the "con" column I would say that it's so complex that it probably has quite a steep learning curve. Although the documentation is great so there is always hope for most problems.
I created the demo https://jsfiddle.net/OlegKi/kc2537ty/1/ which demonstrates the usage of free jqGrid with subgrids. It displays the results like
after the user clicks on the "+" icon in the second line.
The corresponding code you can find below
Small comments to the code. Free jqGrid saves all properties of input data in
data
parameter. I addedid
property to every item of input data. It's not mandatory, but it could be helpful if you would add more functionality to the grid. See the introduction for more details.The columns are sortable based on the type of the data specified by
sorttype
property ofcolModel
. To simplify usage some standard types of data free jqGrid provides some standard templates which are shortcurts for some set of settings. I usedtemplate: "integer"
in the demo, but you could replace it tosorttype: "integer"
if only sorting by integer functionality is important.If the user click on "+" icon to expand the subgrid then jqGrid inserts new row and creates the div for the data part of the subgrid. You can replace
subGridRowExpanded
from above example to the followingto understand what I mean. The unique id of the div will be the first parameter of the callback. One can create any common HTML content in the subgrid. Thus one can create empty
<table>
, append it to the subgrid div and then convert the table to the subgrid.To access to the item of data, which corresponds to the expanding row one can use
$(this).jqGrid("getLocalRow", rowid)
. The return data is the item of original data. It hasloc
property which we need. To be able to use the data as input for jqGrid we create array with the element. I's mostly all, what one have to know to understand how the above code works.You can add call of
.jqGrid("filterToolbar")
to be able to filter the data or to addpager: true
(ortoppager: true
, or both) to have the pager and to userowNum: 5
to specify the number of rows in the page. In the way you can load relatively large set of data in the grid and the user can use local paging, sorting and filtering. See the demo which shows the performance of loading, sorting and filtering of the local grid with 4000 rows and another one with 40000 rows. All works pretty quickly if one uses local paging and not displays all the data at once.