eherve / mongoose-datatable

Server side dataTable request support for mongoose
MIT License
42 stars 28 forks source link

Nested object array search (Question) #43

Closed lnkpaulo closed 9 years ago

lnkpaulo commented 9 years ago

Hi,

First of all thank you for the excellent plugin.

I have a problem when I'm working with nested objects with arrays. My model is:

...
loc: [{
    type: {
      type: String,
      default: 'Feature'
    },
    properties: {
      country: {
        type: String,
        default: 'nd'
      },
      status: {
        type: Number,
        default: '0'
      },
      modifiedAt: Date
    },
    geometry: {
      type: {
        type: String,
        default: 'Point'
      },
      coordinates: {
        type: [Number],
        index: '2dsphere'
      }
    }
  }]
...

and my goal is to retrieve, e.g. loc.properties.country, however I cannot see any info in my tables. Please see my debug info:

Query: { draw: '1',
  columns:
   [ { data: 'loc.properties.country',
       name: '',
       searchable: 'true',
       orderable: 'true',
       search: [Object] } ],
  order: [ { column: '0', dir: 'asc' } ],
  start: '0',
  length: '10',
  search: { value: '', regex: 'false' },
  _: '1443568143064' }
Search Criteria builded: { options:
   { handlers: { field: { arrayPath: 'loc' } },
     conditions: { username: 'donald' } },
  pageStart: 0,
  pageSize: 10,
  nbColumns: 1,
  search: undefined,
  fields:
   [ Field {
       index: 0,
       model: 'users',
       path: 'loc.properties.country',
       searchable: true,
       search: undefined,
       sortable: true,
       sort: { dir: 'asc', precedence: 0 },
       selectable: true,
       type: 'String',
       ref: undefined,
       refType: undefined,
       arrayType: undefined,
       base: undefined,
       arrayPath: 'loc' } ],
  select: { 'loc.properties.country': 1 },
  sort: { 'loc.properties.country': 'asc' },
  conditions: { '$and': [ { username: 'donald' } ] },
  populate: [] }
Data: { draw: '1',
  recordsTotal: 1,
  recordsFiltered: 1,
  data:
   [ { _id: 5605528705fa0cd019316360,
       loc:
        [ { properties: { country: 'none' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } } ] } ] }

How I can pass the right data to datatables?

Thanks.

eherve commented 9 years ago

Hi,

Your are getting the country and it is sent to DataTable. But on DataTable cannot retireve the value sent on the field 'loc.properties.country' because loc is an array and DataTable do not allow 'loc.'properties'.

You need to use a renderer on the field (like in the test package with the module) and process manually the data:

 { data: "loc.properties.country", defaultContent: "",
        render: function(data, type, full) {
                    return JSON.stringify(full.loc); }
      }

I hope this helps

Regards

lnkpaulo commented 9 years ago

Hi,

Thank you for the quick answer and the hint. I was trying to avoid process the data manually but it seems for now it is the solution.

Once more thanks for the excellent plug-in.

Regards.