datatablewebix

Can the index/key field of a Webix Datatable be something other than 'id'?


I have data parsed into a Webix Datatable that includes a property named 'id' for each object in that data. The 'id' is an integer but is not necessarily unique; not until all of the data in the datatable is validated. Each object also has a Unique Identifier where the property is named 'uid'. This is a short string and is unique to each object all of the time. Is there a way that the Webix Datatable can be directed to use the 'uid' property (corresponding to the 'uid' field in the Datatable) as the index/key for each object in the data table?

I've considered mapping the 'uid' property to 'id' and the 'id' property to something like 'objectId':

scheme:{
    $init:function(obj){
          obj.objectId = obj.id;
          obj.id = obj.uid;
        }
      }

This seems pretty clunky when all I want to do is have the 'uid' property (as opposed to the 'id' property') as the index/key for each object in the Webix Datatable. Is there a better/direct way of doing what I'm trying to do?


Solution

  • Your idea of remapping the id in the scheme would be the most straightforward and standard way.

    You could also create a data driver and specify that as the datatype. That is good for overarching data transformations of every data load. The driver has access to the raw ajax data before the component begins parsing the data.

    webix.DataDriver.customParser = webix.extend({
      getRecords: function (result) {
        let data = result.data;
        for(let i = 0; i < data.length; i++) {
          data[i].oldId = data[i].id;
          data[i].id = data[i].uid;
        }
      }
    }, webix.DataDriver.json);
    
    

    Then, in your view:

    {
      view: 'datatable',
      url: someUrl,
      datatype: 'customParser'
    }