Customize row id

Tags: #<Tag:0x00007efc70947500> #<Tag:0x00007efc70947320>

Hi,

I am new to this grid thing and would like to know if it is possible to define the row id using my own provided id (when the data was first loaded).

My use case is like this, we load the grid for users to do their data entry, the rows and columns are pre-defined, and they are not able to add new rows or columns, they simply fill in the data on this spreadsheet.

I already tried to use the dataschema to define the data model and it was able to send the field name to the server, but for the id field, it is still shows the row index, e.g. {“data”:[[0,“score1”,“4”,“2”]]}
What I want is something like this {“data”:[[“my_preloaded_id”,“score1”,“4”,“2”]]}

Is it able to do this?

Thanks

Hi @mike99

Thank you for contacting us. This ID you are getting is actually the row index number, and that can’t be changed, as many plugins and hooks rely on those indexes.

Thanks for your reply.

Now, I made some custom code within the afterchange hook as below:

afterChange: function (changes, source) {
   changes?.forEach(([row, prop, oldValue, newValue], index) => {
     var id_field = this.getDataAtCell(row,0);
     let object1 = {my_own_id: id_field};
     changes[index] = Object.assign({}, object1,
                    { [prop]: newValue });
  });
  fetch('url_to_save_data', {
    method: 'POST',
    mode: 'no-cors',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({ data: changes })
  })
}

When I load my data, I store the key (id) field as the first column, read only. Then, when there is a change to the cell data, I use the getDataAtCell function to retrieve the key, then I wrapped it and saved in the changes object and then post it to the server.

And the json posted to the server will be something like below:
{"data":[{"my_id":"70708844","score1":"24"}]}

I hope this will be useful for others who want to have a custom id field to pass back to their server.

Hi @mike99

Thank you for your solution. I believe it will be helpful for other people looking for such solution. I will close this topic now, but if you have any other questions open a new one.