te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Vue - Using v-for twice to render a tables rows and cells? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue - Using v-for twice to render a tables rows and cells? - Stack Overflow

programmeradmin2浏览0评论

I would like to use Vue to render a table with rows but have the cells as a ponent.

I have made a working example of what I would like to see as the end result and have some code relating to how I think that I could go about this:

HTML

<div id="app">
  <table>
    <thead>
      <tr>
        <th>Row</th>
        <th>ID</th>
        <th>Name</th>
        <th>Age</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, rindex) in people">
        <td>{{ rindex }}</td>
        <cell v-for="(value, vindex, rindex) in row" :value="value" :vindex="vindex" :rindex="rindex"></cell>
      </tr>
    </tbody>
  </table>
  <template id="template-cell">
    <td>{{ value }}</td>
  </template>
</div>

JS

// Register ponent
Vueponent('cell', {
  template: '#template-cell',
  name: 'row-value',
  props: ['value', 'vindex', 'rindex']
  // In here I would like to access value, vindex and rindex
});
// Start application
new Vue({
  el: '#app',
  data: {
    people: [
      {id: 3, name: 'Bob', age: 27},
      {id: 4, name: 'Frank', age: 32},
      {id: 5, name: 'Joe', age: 38}
    ]
  }
});

This is because, in the Vue documentation on v-for they show the example:

And another for the index:

<div v-for="(value, key, index) in object">
  {{ index }}. {{ key }} : {{ value }}
</div>

So logically thinking, I should be able to get the rows index in the cell ponent without too much issue but this is not so as it es up with a bunch of errors indicating that the values are undefined.

If anyone could point me in the right direction here then I would be greatly appreciate it as I'm out of ideas with this problem but would really like to understand how to implement this correctly.

(Note: the reason for me wanting to render the cells in the ponent is to register the change of values, see the answer to another question that I asked here if you're interested)

I would like to use Vue to render a table with rows but have the cells as a ponent.

I have made a working example of what I would like to see as the end result and have some code relating to how I think that I could go about this:

HTML

<div id="app">
  <table>
    <thead>
      <tr>
        <th>Row</th>
        <th>ID</th>
        <th>Name</th>
        <th>Age</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, rindex) in people">
        <td>{{ rindex }}</td>
        <cell v-for="(value, vindex, rindex) in row" :value="value" :vindex="vindex" :rindex="rindex"></cell>
      </tr>
    </tbody>
  </table>
  <template id="template-cell">
    <td>{{ value }}</td>
  </template>
</div>

JS

// Register ponent
Vue.ponent('cell', {
  template: '#template-cell',
  name: 'row-value',
  props: ['value', 'vindex', 'rindex']
  // In here I would like to access value, vindex and rindex
});
// Start application
new Vue({
  el: '#app',
  data: {
    people: [
      {id: 3, name: 'Bob', age: 27},
      {id: 4, name: 'Frank', age: 32},
      {id: 5, name: 'Joe', age: 38}
    ]
  }
});

This is because, in the Vue documentation on v-for they show the example:

And another for the index:

<div v-for="(value, key, index) in object">
  {{ index }}. {{ key }} : {{ value }}
</div>

So logically thinking, I should be able to get the rows index in the cell ponent without too much issue but this is not so as it es up with a bunch of errors indicating that the values are undefined.

If anyone could point me in the right direction here then I would be greatly appreciate it as I'm out of ideas with this problem but would really like to understand how to implement this correctly.

(Note: the reason for me wanting to render the cells in the ponent is to register the change of values, see the answer to another question that I asked here if you're interested)

Share Improve this question edited Jul 13, 2022 at 22:55 tony19 138k23 gold badges277 silver badges347 bronze badges asked Dec 14, 2016 at 22:33 Craig van TonderCraig van Tonder 7,68718 gold badges65 silver badges111 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 9

You just need to enclose your cell ponent inside an template element, this is because table in HTML expects only td inside tr, when it sees cell, it will not render it(see similar problem with Angular here). However template can be used here as a content fragment and after vue piles, it will add tr in place of cell in the HTML.

  <tr v-for="(row, rindex) in people">
    <td>{{ rindex }}</td>
    <template>
       <cell  v-for="(value, vindex) in row" :value="value" :vindex="vindex" :rindex="rindex" ></cell>
    </template>
  </tr>

see working pen here.

I am not sure if you can replace index with rindex in the v-for constructor. If the implementation of v-for expects the string labels for key and index, then your rindex and vindex will not work.

I see another problem, as quoted from the reference page in question: List Rendering in Vue

When iterating over an object, the order is based on the key enumeration order of Object.keys(), which is not guaranteed to be consistent across JavaScript engine implementations.

Therefore, your first cell may have (id, key, name) in that order, while your second cell may have (name, id, key) - different order. Therefore, the best way is to avoid object iterator for cell and explicitly define the order of appearance as follows:

<tr v-for="(person, index) in people">
    <td>{{ index }}</td>
    <cell :value="index" valueType="index" :rowIndex="index"></cell>
    <cell :value="person.id" valueType="id" :rowIndex="index"></cell>
    <cell :value="person.name" valueType="name" :rowIndex="index"></cell>
</tr>

Now your cell ponent definition needs to be modified as:

Vue.ponent("cell", {
    template: "#template-cell",
    name: "row-value",
    props: ["value", "valueType", "rowIndex"],
    // your ponent code here...
})

Now you can use valueType and rowIndex inside your cell ponent.

发布评论

评论列表(0)

  1. 暂无评论