2 Replies Latest reply on Jun 25, 2010 9:20 PM by armalock

    Multiple data sources for a (advanced)datagrid?

    armalock

      Hi all,

       

      I have two arrays of objects coming in from two different sources keyed on a single column called "id". They have the exact same column structure. I want to show these in a grouped column structure in advanced data grid. The question is can I do that without manually merging the two arrays?

       

      Concrete example:

       

      Array1: [{id:1, name:"Cooper", zip:"94536"},

                   {id:2, name:"Corolla", zip:"94404"}]

       

      Array2: [{id:1, name:"One", zip:"94555"},

                   {id:2, name:"Camry", zip:"94403"}]

      This will look like:
      IdNameZip
      Source1Source2Source1Source2
      1CooperOne9453694555
      2CorollaCamry9440494403
      I would like to do this without creating an array like:
      [{id:1, name1:"Cooper", name2:"One", zip1:"94536", zip2:"94555"},
      {id:1, name1:"Corolla", name2:"Camry", zip1:"94404", zip2:"94403"}]
      Charts are quite good at this since you can assign different data sources to different series and the axis defines the key on which the data will be merged. However, I could not find anything similar for tables.
      Any pointers?
      Thanks so much!
        • 1. Re: Multiple data sources for a (advanced)datagrid?
          armalock Level 1

          It does not look like my problem has an easy solution. It seems I have to create a merged version of the two arrays somehow. I definitely do not want to create deep copies of the objects contained in either array. My immediate (and perhaps naive) approach is to create a class called MergedArray and expose a getKey() method to determine from which array the data should be coming from. Then in my dataField property, i would use mergedarray.getKey() with the appropriate params.. Experts, please let me know how you would implement something like this and drive an advanced data grid with it including its filtering and sort capabilities.

           

          PS: If avoiding deep copy will lead to horrendously complicated code, I would rather sacrifice memory .

          • 2. Re: Multiple data sources for a (advanced)datagrid?
            armalock Level 1

            Come to think of it, is this even possible? How does a datagrid use its dataField parameter to lookup data? I am guessing data_array[row_index][col.dataField] would determine the value of a single cell at row_index and col. If so, my naive approach would require me to override the bracket operator so that the appropriate value is returned..

             

            e.g. when a simple array of objects is used, you have [{name:John, lastname:Smith}], datagridcolumns dataField is then set to "name". Now I will have two separate arrays:

             

            arr1: [{name:John, lastname:Smith}]

            arr2: [{name:Smith, lastname:John}]

             

            mergedarray will have a reference to both of them. Then when getKey("name", 1) is called, it will return a special key which can be used to access the name property from array 1, something like "name1". If I cannot override the bracket operator, mergedarray[0][name1] will return nothing.

             

            I remember reading somewhere that this kind of override is not possible. Any ideas?