Get task index on "onAfterTaskAdd" event


#1

Hi!

I’m trying to get the index of a task created on onAfterTaskAdd event, but I get undefined or -1 using “$index” property or using gantt.getGlobalTaskIndex.

const onAfterTaskAdd = gantt.attachEvent("onAfterTaskAdd", (id, item) => {
  const tG = gantt.getTask(id)
  var index = tG["$index"]
  

  console.log(tG.$index) // undefined
  console.log(gantt.getGlobalTaskIndex(id)) //-1
  console.log(index) //undefined

})

So, how can I get the index of a newly created task?

Thanks in advance


#2

Hi @aibarr!

I’m not sure I understand you correctly, but I’ve tried to reproduce your issue in this snippet. Please open it and click on the + and Add new task buttons. Look at the messages in the top right with hte indexes of created tasks:
http://snippet.dhtmlx.com/e99845e72

What version of Gantt do you use?

Please, reproduce the issue in the snippet:
http://snippet.dhtmlx.com/c9e794d5c
Then click on the “share” button and send me the link.


#3

Hi @guldmi

I was able to partialy replicate the problem on the next Snippet http://snippet.dhtmlx.com/50d93c73f

As you can see, when “$index” is called an “undefined” is obtained. This time, gantt.getGlobalTaskIndex throws right, but I think that it does because it wins on race conditions. My code on the “onAfterTaskAdd” event is actually more longer than this example, so in the console of my code it actually keeps evaluating while the event execution doesn’t finished yet; this is just an assumption, so I’m not plenty sure about this.

Thanks for all!