admin管理员组

文章数量:1414605

I wanted to display three different lists of json in a table. The first contains URLs, the second URLs to google maps and the last messages.

To render it I have a template like this:

<script type="text/x-template" id="grid-template">
  <table class="table">
    <thead>
      <tr>
        <th v-for="key in columns"
          @click="sortBy(key)"
          :class="{ active: sortKey == key }">
            ${ key | capitalize }
          <span class="arrow" :class="sortOrders[key] > 0 ? 'asc' : 'dsc'">
          </span>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="entry in filteredData">
        <td v-for="key in columns">${isUrl(key,entry[key])}</td>
      </tr>
    </tbody>
  </table>
</script>


  Vueponent('demo-grid', {
    template: '#grid-template',
    delimiters: ['${', '}'],
    props: {
      data: Array,
      columns: Array,
      filterKey: String
    },
    data: function () {
      var sortOrders = {}
      this.columns.forEach(function (key) {
        sortOrders[key] = 1
      })
      return {
        sortKey: '',
        sortOrders: sortOrders
      }
    },
    puted: {
      filteredData: function () {
        var sortKey = this.sortKey
        var filterKey = this.filterKey && this.filterKey.toLowerCase()
        var order = this.sortOrders[sortKey] || 1
        var data = this.data
        if (filterKey) {
          data = data.filter(function (row) {
            return Object.keys(row).some(function (key) {
              return String(row[key]).toLowerCase().indexOf(filterKey) > -1
            })
          })
        }
        if (sortKey) {
          data = data.slice().sort(function (a, b) {
            a = a[sortKey]
            b = b[sortKey]
            return (a === b ? 0 : a > b ? 1 : -1) * order
          })
        }
        return data
      }
    },
    filters: {
      capitalize: function (str) {
        return str.charAt(0).toUpperCase() + str.slice(1)
      }
    },
    methods: {
      sortBy: function (key) {
        this.sortKey = key
        this.sortOrders[key] = this.sortOrders[key] * -1
      },

      isUrl: function (key, str){
        if (key == "url"){
          var exp = /^((?:https?|ftp):\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/ig;
          return str.replace(exp,"<a :href='$1'>$1</a>");
        }
        else{
          console.log("ej")
          return str
        }
      }
    }

  })

I wanted to create a hyperlink when the columns' key name is "url", but vue auto-escapes this. How can I manage to replace text for a hyperlink?

An example of the data:

[{
  "date": "2017-08-09 18:25:06.226631", 
  "id": 5, 
  "private": true,
  "reviewed": false, 
  "title": "To Protect Voting, Use Open-Source Software - NYTimes", 
  "url": "j.mp/2upm633", 
  "user": 1
}]

I wanted to display three different lists of json in a table. The first contains URLs, the second URLs to google maps and the last messages.

To render it I have a template like this:

<script type="text/x-template" id="grid-template">
  <table class="table">
    <thead>
      <tr>
        <th v-for="key in columns"
          @click="sortBy(key)"
          :class="{ active: sortKey == key }">
            ${ key | capitalize }
          <span class="arrow" :class="sortOrders[key] > 0 ? 'asc' : 'dsc'">
          </span>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="entry in filteredData">
        <td v-for="key in columns">${isUrl(key,entry[key])}</td>
      </tr>
    </tbody>
  </table>
</script>


  Vue.ponent('demo-grid', {
    template: '#grid-template',
    delimiters: ['${', '}'],
    props: {
      data: Array,
      columns: Array,
      filterKey: String
    },
    data: function () {
      var sortOrders = {}
      this.columns.forEach(function (key) {
        sortOrders[key] = 1
      })
      return {
        sortKey: '',
        sortOrders: sortOrders
      }
    },
    puted: {
      filteredData: function () {
        var sortKey = this.sortKey
        var filterKey = this.filterKey && this.filterKey.toLowerCase()
        var order = this.sortOrders[sortKey] || 1
        var data = this.data
        if (filterKey) {
          data = data.filter(function (row) {
            return Object.keys(row).some(function (key) {
              return String(row[key]).toLowerCase().indexOf(filterKey) > -1
            })
          })
        }
        if (sortKey) {
          data = data.slice().sort(function (a, b) {
            a = a[sortKey]
            b = b[sortKey]
            return (a === b ? 0 : a > b ? 1 : -1) * order
          })
        }
        return data
      }
    },
    filters: {
      capitalize: function (str) {
        return str.charAt(0).toUpperCase() + str.slice(1)
      }
    },
    methods: {
      sortBy: function (key) {
        this.sortKey = key
        this.sortOrders[key] = this.sortOrders[key] * -1
      },

      isUrl: function (key, str){
        if (key == "url"){
          var exp = /^((?:https?|ftp):\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/ig;
          return str.replace(exp,"<a :href='$1'>$1</a>");
        }
        else{
          console.log("ej")
          return str
        }
      }
    }

  })

I wanted to create a hyperlink when the columns' key name is "url", but vue auto-escapes this. How can I manage to replace text for a hyperlink?

An example of the data:

[{
  "date": "2017-08-09 18:25:06.226631", 
  "id": 5, 
  "private": true,
  "reviewed": false, 
  "title": "To Protect Voting, Use Open-Source Software - NYTimes.", 
  "url": "j.mp/2upm633", 
  "user": 1
}]
Share Improve this question edited Aug 9, 2017 at 18:02 thanksd 55.7k23 gold badges165 silver badges154 bronze badges asked Aug 9, 2017 at 17:45 Yábir GarciaYábir Garcia 3394 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

You can format the url in the template like so:

<tr v-for="entry in filteredData">
  <td v-for="key in columns">
    <template v-if="key === 'url'">
      <a :href="entry[key]">${ entry[key] }</a>
    </template>
    <template v-else>
      ${ entry[key] }
    </template>
  </td>
</tr>

本文标签: javascriptVue js insert link in tableStack Overflow