vue如何创建vnode_VueJS渲染VNode

tl;dr:

Given a VueJS VNode object, how do I get the HTML element that would be generated if it were rendered?

e.g.:

> temp1

VNode {tag: "h1", data: undefined, children: Array(1), text: undefined, elm: undefined, …}

> temp1.children[0]

VNode {tag: undefined, data: undefined, children: undefined, text: "Test", elm: undefined, …}

> doSomething(temp1)

Test

Goal

I'm attempting to build a small VueJS wrapper around the DataTables.net library.

To mimic the behavior of HTML tables in my markup, I want something like the following:

NameAgeSalary

{{ person.name }}{{ person.age }}{{ person.salary }}

What I've done so far

I've started to implement this as follows:

DataTable.vue

/* global $ */

export default {

data: () => ({

instance: null

}),

mounted() {

this.instance = $(this.$refs.table).dataTable();

this.$el.addEventListener("dt.row_added", function(e) {

this.addRow(e.detail);

});

},

methods: {

addRow(row) {

// TODO

console.log(row);

}

}

};

DataTableRow.vue

/* global CustomEvent */

export default {

mounted() {

this.$nextTick(() => {

this.$el.dispatchEvent(new CustomEvent("dt.row_added", {

bubbles: true,

detail: this.$slots.default.filter(col => col.tag === "td")

}));

});

},

render() { return ""; }

};

What this currently does:

When the page loads, the DataTable is initialized. So the column headers are properly formatted and I see "Showing 0 to 0 of 0 entries" in the bottom left

The CustomEvent is able to bubble up past the

and be caught by the DataTable element successfully (circumventing the limitation in VueJS that you can't listen to events on slots)

What this does not do:

Actually add the row

My event is giving me an array of VNode objects. There's one VNode per column in my row. The DataTables API has an addRow function which can be called like so:

this.instance.row.add(["col1", "col2", "col3"]);

In my case, I want the resultant element from the rendering of the VNode to be the elements in this array.

var elems = [];

for (var i = 0; i < row.length; i++)

elems[i] = compile(row[i]);

this.instance.row.add(elems);

Unfortunately this compile method eludes me. I tried skimming the VueJS documentation and I tried Googling it, but no dice. I tried manually passing the createElement function (the parameter passed to the render method) but this threw an error. How can I ask VueJS to render a VNode without injecting the result into the DOM?

解决方案

I ran into the same issue wanting to do basically the same thing with a row details template for DataTables.net.

One solution could be to create a generic component that renders out a VNode and instantiate that programmatically. Here is how my setup for a dynamic detail row that I insert using datatable's row.child() API.

RenderNode.js

export default {

props: ['node'],

render(h, context) {

return this.node ? this.node : ''

}

}

Datatables.vue

Include the renderer component from above

import Vue from 'vue'

import nodeRenderer from './RenderNode'

Instantiate and mount the renderer to get the compiled HTML

// Assume we have `myVNode` and want its compiled HTML

const DetailConstructor = Vue.extend(nodeRenderer)

const detailRenderer = new DetailConstructor({

propsData: {

node: myVNode

}

})

detailRenderer.$mount()

// detailRenderer.$el is now a compiled DOM element

row.child(detailRenderer.$el).show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值