For
用于循环一个数组或是对象, VueJs的语法都可以使用, k-for的与v-for的行为基本一致!
示例代码如下:
k-for Array
<script env="server">
var list = [];
list.push({ name: "nameone" });
list.push({ name: "nametwo" });
</script>
<div k-for="item in list">
<h2 k-content="item.name">name</h2>
</div>
v-for Array with Index
<script env="server">
var list = [];
list.push({ name: "nameone" });
list.push({ name: "nametwo" });
</script>
<div env="server" v-for="(item, index) in list" repeat-self="true">
<span v-html="index">number </span> : <span v-html="item.name">name</span>
</div>
k-for Number
<div k-for="n in 10">
<h2 k-content="n"> from 1 to 10</h2>
</div>
<div k-for="1, 4">
<h2>repeated 4 times</h2>
</div>
<div k-for="1, 4, alias">
<h2 k-content="alias">number</h2>
</div>
v-for Number
这个与k-for的行为是一致的
<div env="server">
<div v-for="n in 10">
<h2 v-html="n"> from 1 to 10</h2>
</div>
<div v-for="1, 4">
<h2>repeated 4 times</h2>
</div>
<div v-for="1, 4, alias">
<h2 v-html="alias">number</h2>
</div>
</div>
k-for Object
<div env="server">
<script>
var obj = { firstname: "Joe", lastname: "Vincent" };
</script>
<div k-for="(value, key) in obj">
<span k-content="key">key</span> : <span k-content="value">name</span>
</div>
</div>
v-for Object
代码
<div env="server">
<script>
var obj = { firstname: "Joe", lastname: "Vincent" };
</script>
<div v-for="(value, key) in obj">
<span v-text="key">key</span> : <span v-text="value">name</span>
</div>
</div>
结果
<div>
<div>
<span>firstname</span> : <span>Joe</span>
</div><div>
<span>lastname</span> : <span>Vincent</span>
</div>
</div>
v-for Object Index
<div env="server">
<script>
var obj = { firstname: "Joe", lastname: "Vincent" };
</script>
<div v-for="(value, name, index) in obj">
<span v-text="index"></span> <span v-text="name">key</span> : <span v-text="value">name</span>
</div>
</div>
v-for Object with naming binding
<script env=server>
var list = [];
list.push({ firstname: "firstname1", two: "twovalue1" });
list.push({ firstname: "firstname2", two: "twovalue2" });
</script>
<div env='server' v-for="{firstname, two, three} in list">
<p>{{firstname}}_{{two}}_{{three}}</p>
</div>
repeat-self
当前元素是否重复, 默认是false,可以不指定
代码
<script env="server">
var list = [];
list.push({ name: "nameone" });
list.push({ name: "nametwo" });
</script>
<div k-for="item in list" repeat-self="false">
<h2 k-content="item.name">name</h2>
</div>
结果
<div>
<h2>nameone</h2>
<h2>nametwo</h2>
</div>
代码
<script env="server">
var list = [];
list.push({ name: "nameone" });
list.push({ name: "nametwo" });
</script>
<div k-for="item in list" repeat-self="true">
<h2 k-content="item.name">name</h2>
</div>
结果
<div>
<h2>nameone</h2>
</div><div>
<h2>nametwo</h2>
</div>