棘手的 jQuery 模板问题
假设我有一个这样的模板
<script id="rowTemplate" type="text/x-jquery-tmpl">
<tr>
<a href="${ $.tmpl(viewModel.EditUrl, $data).text() }">Edit </a>
<a href="${ $.tmpl(viewModel.DetailsUrl, $data).text() }">Details</a></td>
<td>${Id}</td>
<td>${Number}</td>
<td>${Description}</td>
<td>${Total}</td>
<td><input type='image' src="/images/delete.gif" alt="delete" data-bind="click: function() { processCommand({name:'Delete', Data: this, IsAjax:false}) }"></a></td>
</tr>
</script>
显然我们正在绑定一个集合,并且对于每个条目,都会创建一行。现在我无法得到的是引用被绑定的“数据”的实例,但必须访问全局“viewModel”变量。我想获取正在绑定的数据的实例,并获取属性“EditUrl”(它本身有一个模板文本)。那么我该怎么做呢?
完整的例子(我也使用knockout.js,但问题不是关于它) 问题是我将“Dtos”列表绑定到行。但 EditUrl 和 CreateUrl 不在 Dtos 上,而是在包含 Dtos 的对象(父级)上。
var viewModel = ko.mapping.fromJS({"Dtos":[{"Id":0,
"Description":"some description",
"Number":0,"Total":0.0,
"Date":"\/Date(1303495442114-0500)\/"},
{"Id":1,"Description":"some description","Number":100,"Total":200.0,
"Date":"\/Date(1271959442114-0500)\/"},
{"Id":2,"Description":"some description","Number":200,"Total":400.0,
"Date":"\/Date(1240423442114-0500)\/"},"CreateUrl":"http://localhost:16555/TestOrderProcessingPage/tabid/63/ctl/OrderEdit/orderId/-1/Default.aspx",
"EditUrl":"http://localhost:16555/TestOrderProcessingPage/tabid/63/ctl/OrderEdit/mid/387/Default.aspx?orderId=${Id}",
"DetailsUrl":"http://localhost:16555/TestOrderProcessingPage/tabid/63/ctl/OrderDetails/mid/387/Default.aspx?orderId=${Id}"});
<div data-bind='template: {name: "contentTemplate"}' />
<script id="contentTemplate" type="text/x-jquery-tmpl">
{{tmpl contentHeaderTemplate}}
<div data-bind='template: {name: "tableTemplate"}'></div>
<br />
</script>
<script id="contentHeaderTemplate" type="text/x-jquery-tmpl">
<h2>
Orders</h2>
<a href="${CreateUrl}">
Create New Order</a>
<br />
<br />
</script>
<script id="tableTemplate" type="text/x-jquery-tmpl">
<table class="gridview" cellspacing="0" rules="all" border="1">
<tbody data-bind='template: {name:"rowTemplate", foreach:Dtos}'></tbody>
</table>
</script>
<script id="rowTemplate" type="text/x-jquery-tmpl">
<tr>
//this is problem "viewModel", there is not global viewModel variable any more, so the question is how to refer to data being bound to the row.
<td><a href="${ $.tmpl(viewModel.EditUrl, $data).text() }">Edit </a>
<a href="${ $.tmpl(viewModel.DetailsUrl, $data).text() }">Details</a></td>
<td>${Id}</td>
<td>${Number}</td>
<td>${Description}</td>
<td>${Total}</td>
<td><input type='image' src="/images/delete.gif" alt="delete" data-bind="click: function() { processCommand({name:'Delete', Data: this, IsAjax:false}) }"></a></td>
</tr>
</script>
<script id="headerTemplate" type="text/x-jquery-tmpl">
<thead>
<tr>
<th></th>
<th>Id</th>
<th>Number</th>
<th>Description</th>
<th>Total</th>
<th></th>
</tr>
</thead>
</script>
Let's say I have a template like this
<script id="rowTemplate" type="text/x-jquery-tmpl">
<tr>
<a href="${ $.tmpl(viewModel.EditUrl, $data).text() }">Edit </a>
<a href="${ $.tmpl(viewModel.DetailsUrl, $data).text() }">Details</a></td>
<td>${Id}</td>
<td>${Number}</td>
<td>${Description}</td>
<td>${Total}</td>
<td><input type='image' src="/images/delete.gif" alt="delete" data-bind="click: function() { processCommand({name:'Delete', Data: this, IsAjax:false}) }"></a></td>
</tr>
</script>
Obviously we are binding a collection and for each entry, a row is created. Now what I cannot get is to refer to the instance of the "data" being bound but have to reach into global "viewModel" variable. I would like to get the instance of the data being bound, and get the property "EditUrl" (which itself has a template text). So how do I do that?
Full example ( I use also knockout.js but the question is not about it)
Problem is that I bind a list of "Dtos" to a rows. But EditUrl and CreateUrl are not on the Dtos, but on the object that contains Dtos ( a parent).
var viewModel = ko.mapping.fromJS({"Dtos":[{"Id":0,
"Description":"some description",
"Number":0,"Total":0.0,
"Date":"\/Date(1303495442114-0500)\/"},
{"Id":1,"Description":"some description","Number":100,"Total":200.0,
"Date":"\/Date(1271959442114-0500)\/"},
{"Id":2,"Description":"some description","Number":200,"Total":400.0,
"Date":"\/Date(1240423442114-0500)\/"},"CreateUrl":"http://localhost:16555/TestOrderProcessingPage/tabid/63/ctl/OrderEdit/orderId/-1/Default.aspx",
"EditUrl":"http://localhost:16555/TestOrderProcessingPage/tabid/63/ctl/OrderEdit/mid/387/Default.aspx?orderId=${Id}",
"DetailsUrl":"http://localhost:16555/TestOrderProcessingPage/tabid/63/ctl/OrderDetails/mid/387/Default.aspx?orderId=${Id}"});
<div data-bind='template: {name: "contentTemplate"}' />
<script id="contentTemplate" type="text/x-jquery-tmpl">
{{tmpl contentHeaderTemplate}}
<div data-bind='template: {name: "tableTemplate"}'></div>
<br />
</script>
<script id="contentHeaderTemplate" type="text/x-jquery-tmpl">
<h2>
Orders</h2>
<a href="${CreateUrl}">
Create New Order</a>
<br />
<br />
</script>
<script id="tableTemplate" type="text/x-jquery-tmpl">
<table class="gridview" cellspacing="0" rules="all" border="1">
<tbody data-bind='template: {name:"rowTemplate", foreach:Dtos}'></tbody>
</table>
</script>
<script id="rowTemplate" type="text/x-jquery-tmpl">
<tr>
//this is problem "viewModel", there is not global viewModel variable any more, so the question is how to refer to data being bound to the row.
<td><a href="${ $.tmpl(viewModel.EditUrl, $data).text() }">Edit </a>
<a href="${ $.tmpl(viewModel.DetailsUrl, $data).text() }">Details</a></td>
<td>${Id}</td>
<td>${Number}</td>
<td>${Description}</td>
<td>${Total}</td>
<td><input type='image' src="/images/delete.gif" alt="delete" data-bind="click: function() { processCommand({name:'Delete', Data: this, IsAjax:false}) }"></a></td>
</tr>
</script>
<script id="headerTemplate" type="text/x-jquery-tmpl">
<thead>
<tr>
<th></th>
<th>Id</th>
<th>Number</th>
<th>Description</th>
<th>Total</th>
<th></th>
</tr>
</thead>
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是通过“options”参数将 viewModel 或所需的变量传递给 jQuery 模板。
我认为您在这里使用 Knockout,所以您可以做的是:
然后,在您的模板中,您可以访问行模板中的这些属性,例如:
$item.edit
和$item.details
(或者任何你想调用你的属性的名称)。如果您不使用 Knockout,那么您可以将选项传递到
$.tmpl
中,如下所示:The easiest thing to do is pass in the the viewModel or the variables that you need in through the "options" parameter to jQuery templates.
I am thinking that you are using Knockout here, so what you can do is something like:
Then, in your template you can access these properties in the row template like:
$item.edit
and$item.details
(or whatever you want to call your properties).If you are not using Knockout, then you can pass options into
$.tmpl
like: