knockout.js - inserting a div between divs using knockout -
im using knockout , want insert / show div between 2 divs. im creating employee details page. employees listed , when user clicks on employee want / details show under employee
<div>user 1</div> <div>user 2</div> <div>user 3</div>
clicked
<div>user 1</div> <div>user details etc</div> <div>user 2</div> <div>user 3</div>
im storing selected user in editable property populated when employee clicked , using binding can user come after users, details come under relevant employee. ideas?
knockoutjs doesn't manipulate dom way. use jquery or native js document.createelement('user details etc') , append between users divs. closest behavior in knockout if
binding. explained @ end. still needs there defined @ first, , knockout can control it.
for knockout way can start visibility:
<div>user 1</div> <div data-bind="visible: selecteduser() == user1">user 1 details etc</div> <div>user 2</div> <div data-bind="visible: selecteduser() == user2">user 2 details etc</div> <div>user 3</div> <div data-bind="visible: selecteduser() == user3">user 3 details etc</div>
or better, in loop:
<!-- ko foreach: users --> <div data-bind="text: $data.username"></div> <div data-bind="visible: $parent.selecteduser() == $data.username, text: $data.userdetails"></div> <!-- /ko -->
if want keep divs out of dom, change visibility
if
. knockout website:
if plays similar role visible binding. difference that, visible, contained markup remains in dom , has data-bind attributes applied - visible binding uses css toggle container element’s visibility. if binding, however, physically adds or removes contained markup in dom, , applies bindings descendants if expression true.
you can read more in documentation: http://knockoutjs.com/documentation/if-binding.html
edit: , modified jsfiddle: http://jsfiddle.net/xwck9/1/
Comments
Post a Comment