Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access nested properties with dataIndex #46

Closed
queimadus opened this issue May 10, 2016 · 15 comments
Closed

Access nested properties with dataIndex #46

queimadus opened this issue May 10, 2016 · 15 comments

Comments

@queimadus
Copy link
Contributor

Have you considered using something like https://github.com/mariocasciaro/object-path to parse dataIndex?

const data = [{
  name: "John", 
  age: 25 , 
  custom: {
    one: 1, 
    two: 2
  }
}]

const columns = [{
  title: "Name",
  dataIndex: "name"
}, {
  title: "Age",
  dataIndex: "age"
}, {
  title: "My custom property one",
  dataIndex: "custom.one"
}, {
  title: "My custom property two",
  dataIndex: "custom.two"
}]
Name Age My custom property one My custom property two
John 25 1 2
@benjycui
Copy link
Member

Or https://github.com/benjycui/exist.js 😅

@afc163
Copy link
Member

afc163 commented May 27, 2016

We need this, PR welcome~ @queimadus

@afc163 afc163 closed this as completed in #47 Jun 1, 2016
afc163 added a commit to ant-design/ant-design that referenced this issue Jun 1, 2016
afc163 referenced this issue in ant-design/ant-design Jun 3, 2016
@bodfaj13
Copy link

bodfaj13 commented Mar 15, 2020

@khanakia
Copy link

khanakia commented Apr 6, 2020

I cannot sort the tables anymore because it sends the fields like this now
sortField: ["serviceType", "title"]
but I need it to the like this
sortField: ["serviceType.title"]

So how to do that ???

@haniamine
Copy link

I tried a lot of things but currently the only one that worked for me is using a "render" instead of
"dataIndex" at the columns.
render: (record) => record.object.attribute

so i'm suggesting this:

const data = [{
  name: "John", 
  age: 25 , 
  custom: {
    one: 1, 
    two: 2
  }
}]

const columns = [{
  title: "Name",
  dataIndex: "name"
}, {
  title: "Age",
  dataIndex: "age"
}, {
  title: "My custom property one",
 render: (record) => record.custom.one
}, {
  title: "My custom property two",
render: (record) => record.custom.two
}]

@khanakia
Copy link

I tried a lot of things but currently the only one that worked for me is using a "render" instead of
"dataIndex" at the columns.
render: (record) => record.object.attribute

so i'm suggesting this:

const data = [{
  name: "John", 
  age: 25 , 
  custom: {
    one: 1, 
    two: 2
  }
}]

const columns = [{
  title: "Name",
  dataIndex: "name"
}, {
  title: "Age",
  dataIndex: "age"
}, {
  title: "My custom property one",
 render: (record) => record.custom.one
}, {
  title: "My custom property two",
render: (record) => record.custom.two
}]

You are giving the solution to render the value. But I was talking about sortField when you click on the Sorting Icon in Table Header then it sends the value like that as I said above.

@haniamine
Copy link

@khanakia For sorting it's the same thing, you need to add sorter to columns:

{
    title: "My custom property one",
    sorter: (a, b) => a.custom.one-b.custom.one,
    render: (record) => record.custom.one,
  }

if your column had string values:

sorter: (a, b) => a.custom.one.localeCompare(b.custom.one),

@khanakia
Copy link

Ah, i see. I will give it a try. :) Thanks

@rikinshah23
Copy link

rikinshah23 commented May 11, 2020

How to approach when trying to render an image?
I am trying below but its not showing image.

const columns = [
  {
    title: 'image',
    dataIndex: 'notificationImage.url',
    key: 'notificationImage.url',
    render: (record) =>
    //console.log(text, record && record.notificationImage && record.notificationImage.url, index)
    {
      record && record.notificationImage &&
        <img
          width="460" height="345"
          src={record.notificationImage.url} />
    }
  },]

@khanakia
Copy link

You are doing wrong. Do this way

const columns = [
  {
    title: 'image',
    dataIndex: ['notificationImage', 'url'],  // This is what changed
    key: 'notificationImage.url',
    render: (record) =>
    //console.log(text, record && record.notificationImage && record.notificationImage.url, index)
    {
      record && record.notificationImage &&
        <img
          width="460" height="345"
          src={record.notificationImage.url} />
    }
  },]

@rikinshah23
Copy link

@khanakia Thanks. That worked!

@rajkumarWebelight
Copy link

rajkumarWebelight commented Sep 11, 2020

What when you search data {firstName+lastName} in Table column

@fridolinf
Copy link

fridolinf commented Jun 5, 2021

sorry may i ask. how to display it if the row name is the same, if i match it it will output like this
image
image

so I want get the name of product
image

but user hame same name
image

I want get the value name of product and user
image

@khanakia
Copy link

khanakia commented Jun 5, 2021

@fridolinf it seems record.product is NULL please double check the DATA you are passing to the table.

@fridolinf
Copy link

@fridolinf it seems record.product is NULL please double check the DATA you are passing to the table.

thank you for answering my question. I solved the problem
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants