最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

knockout.js - How can I create a text link in a Knockout JavaScript table? - Stack Overflow

programmeradmin2浏览0评论

I've got some KnockoutJS code working - it pulls in a list and binds it to a table.

For the table-data which displays the name, I would like that to be an <a href=...>, but not sure how. The name is still displayed. But you can click on it.

Here's my current code:

<tbody data-bind="foreach: items">
    <tr>
        <td data-bind="text: name()"></td>
        <td data-bind="text: price()"></td>
        <td data-bind="text: endsOn()"></td>
    </tr>   
</tbody>

nothing too crazy.

I have another property called url which contains the full http://blah URL to direct the users to. Also, I would like a new tab to open up.

Any suggestions?

I've got some KnockoutJS code working - it pulls in a list and binds it to a table.

For the table-data which displays the name, I would like that to be an <a href=...>, but not sure how. The name is still displayed. But you can click on it.

Here's my current code:

<tbody data-bind="foreach: items">
    <tr>
        <td data-bind="text: name()"></td>
        <td data-bind="text: price()"></td>
        <td data-bind="text: endsOn()"></td>
    </tr>   
</tbody>

nothing too crazy.

I have another property called url which contains the full http://blah URL to direct the users to. Also, I would like a new tab to open up.

Any suggestions?

Share Improve this question edited Sep 4, 2022 at 14:16 halfer 20.4k19 gold badges108 silver badges201 bronze badges asked Dec 12, 2012 at 13:00 Pure.KromePure.Krome 87k120 gold badges424 silver badges685 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 20

You have to remove data-bind attribute from td tag and put a with attr binding inside td:

<tbody data-bind="foreach: items">
    <tr>
        <td><a data-bind="text: name, attr: {href: url}" target="_new"></a></td>
        <td data-bind="text: price"></td>
        <td data-bind="text: endsOn"></td>
    </tr>   
</tbody>

P.S. You don't have to put () after property name in data-bind attribute if you don't construct expression.

发布评论

评论列表(0)

  1. 暂无评论