How do I "properly" get a row number clicked in Ta...
# kvision
u
How do I "properly" get a row number clicked in Tabulator? This works on 2 of my views:
Copy code
val tabulatorRowIndex = ((event.detail as io.kvision.tabulator.js.Tabulator.RowComponent).getIndex() as Int) - 1
But on the third its failing on cast exception
r
See http://tabulator.info/docs/4.9/components#component-row and compare
getIndex()
and
getPosition()
getIndex()
returns the value of the index column in your data (by dafault
id
column)
if your data doesn't contain integer
id
the cast will fail
if you just want a row number - use
getPosition()
u
Hm, but it doesnt seem to match whats actually happening:
Copy code
setEventListener<Tabulator<SystemUser>> {
                    rowClickTabulator = { event ->
                        val casted = (event.detail as io.kvision.tabulator.js.Tabulator.RowComponent).getData()
                        println("Data: $casted")
                        val model = casted as SystemUser
it breaks down at the last line in this snippet, it's supposed to give me the SystemUser object thats represented in the clicked row, right?
also the println of data is [object Object]
r
No.
Unfortunately it's a bit complicated.
Tabulator keeps it's data model separately.
And it's represented with plain JS objects.
Your external Kotlin data model is converted to plain JS objects.
That's why, when you access internal tabulator components (like
RowComponent
or
CellComponent
) you will get only plain JS objects.
Moreover, with legacy backend you could cast, because Kotlin internal representation and JS representation were similar.
It's not the case with IR - you can't cast.
u
hm, that explains a lot. So, the .getIndex() method will give me "id" field of that object? If so I could fetch the object from the list I passed into tabulator, right?
r
Exactly
u
alright, thanks a lot
r
Tabulator has some internal conversion methods to get Kotlin data class from intenral JS object. I'm thinking about making them public in the next release.
m
I’ve just hit this issue while migrating our project to IR. @Robert Jaros did you by any chance end up making public those internal conversion methods you mentioned a year+ ago? Thinking about using ’em if they’re available 🙂
r
I think so. There is a public
toKotlinObjTabulator
method. But since KV6 Tabulator supports only serializable data classes, so this method just deserializes the plain js data to Kotlin class using tabulator internal serializer.
m
Hmmm… I didn’t think the internal, mangled JS representation of a Kotlin class within the IR compiler could be deserialized by the kotlinx serializer… In any case, this solved my issues, thanks!!!
d
Hi @Robert Jaros, I am probably trying to solve similar problem as Unux did in this thread. I want to click on the row of tabulator and after that open new page with editable detail. So I want to get id of my domain object. I am trying to do this:
Copy code
tabulator.onEvent {
    rowClickTabulator = { event ->
        val rowComponent = event.detail as Tabulator.RowComponent
        val rowIndex = rowComponent.getPosition() as Int - 1
        val data = tabulator.getData(RowRangeLookup.VISIBLE)?.get(rowIndex) as UniversityEvent
        console.log(data)
        console.log(data.name)
    }
}
but I am receiving
ClassCastException
Can you advice me what I am doing wrong? Is my approach even correct? Thanks, in advance for your help!
r
Hi, I think your code is ok. The same patterns works fine for me.
Do you use current KVision version (6.x)?
d
Yes I am on version 6.3.1.
I should also mentioned that I am using
TabulatorRemote
and with those modules:
Copy code
startApplication(
        ::App,
        module.hot,
        BootstrapModule,
        BootstrapCssModule,
        DatetimeModule,
        BootstrapIconsModule,
        CoreModule,
        FontAwesomeModule,
        TabulatorCssMaterializeModule
    )
not sure if adding
TabulatorModule
is also necessary?
r
I see you are using
val tabulator = TabulatorRemote(...)
Please try
val tabulator = tabulatorRemote()
A DSL builder function instead of a constructor.
The function is an inline function with reified parameter to automatically infer model class type. A constructor can't do this (you could pass
kClass: KClass<T>
parameter manually)
d
Thank you Robert! That’s was the problem. Usage of DSL builder
tabulatorRemote()
helped!
133 Views