0

i want to use a function to set individual content of table cells. In this example i want to wrap the status with the <strong> - Tag (I dont edit the template, because at my application it's stored in a component, which is used multiple times ...)

tableData: [
  {
    "name": "test1",
    "status": "1"
  },
  {
    "name": "test2",
    "status": "0"
  },
  {
    "name": "test3",
    "status": "1"
  }
],
columns: {
  name: {
    title: "name"
  },
  status: {
    title: "status",
    content: function(entry) {
      return "<strong>" + entry.status + "</strong>";
    }
  }
}

I tried smth like value.content.call() in the v-for loop, but this doesnt work.

JsFiddle: https://jsfiddle.net/7anuorbu/4/

4
  • 1
    I think your approach is not good.You are keeping methods into the data object. Commented Nov 8, 2016 at 7:22
  • @Belmin is there another good way to do this? Commented Nov 8, 2016 at 9:40
  • The way you done It confuse me a bit.Why you didn't do basic iteration of your array, and then wrap your status value in strong tag in template ? Or if you want to go without putting strong tag directly in template, use computed property, or method do it for you. Commented Nov 8, 2016 at 9:43
  • @Belmin i dont want to change the template, how can i use computed property without editing the template? Commented Nov 8, 2016 at 9:48

1 Answer 1

2
+200

You can use help of v-html, which takes care of rendering of html in the views. In the HTML, you can call the function like this:

<tr v-for="entry in tableData">
  <td v-for="(value, key) in columns" v-html="value.content(entry)">
  </td>
</tr>

To make this work I have modified columns data as well, as both elements need to have content.

   columns: {
      name: {
        title: "name"        ,
        content: function(entry) {
          return "<span>" + entry.name + "</span>";
        }
      },
      status: {
        title: "status",
        content: function(entry) {
          return "<strong>" + entry.status + "</strong>";
        }
      }

Whole working code is here.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks - working!! i also added v-if to check if value.content is a function, so i dont have to add it everywhere :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.