knockout.js - how to data-bind knockout js viewmodel to jquery dynamically created rows -


within page bound viewmodel have section user can add elements page elements filter section comprised of few value fields , select element needs databound viewmodel.

running select option inline in html works fine. once pull out , add jquery adds filter control fails bind. realize due in part view model being bound , have tried call apply bindings again fails well.

i can't use foreach have in other areas because section optional. users not have add filters if don't want why went jquery route.

can provide suggestions on how re-bind select element view model once page has rendered or there better approach using knockout accomplish this?

code: html

  <div id="filtersection" data-bind="with: reportobject">                 <select data-bind="options: selectedattributes(), optionstext: function(selectedattributes){ return selectedattributes.namehierarchy() + '.' + selectedattributes.labelname() },  optionscaption:'select field...'">                 </select><!-- select here works added testing -->                 <div id="filterbuilder">                     <input type="button" value="add filter" title="add filter" class="special" id="add" />                  </div>             </div> 

jquery

function createfilterrow() {         $("<div class='filterrow'><select class='queryterm' name='condition'><option  value='and'>and</option><option value='or'>or</option></select>" +     "<select data-bind='options: selectedattributes(), optionscaption:'select field...'></select>&nbsp;&nbsp;<select name='operator'class='operclass'><option value='equals' id='opt1'>equals</option><option id='opt1' value='contains'>contains</option><option id='opt2' value='does not contain'>does not contain</option><option id='opt3' value='like'>like</option><option id='opt4' value='between'>between</option></select>&nbsp;&nbsp;<input type='text' class='queryterm' name='fieldvalue1' id='fieldvalue1' value='' size='25' />&nbsp;&nbsp;<a id='btnremove' class='ui-button-text-only'>remove</a><a id='btnadd' class='ui-button-text-only'>add</a></div>").appendto('#filterbuilder');     };      //orignal add row button     $(document.body).on('click', '#add', function (event) {         createfilterrow();         ko.applybindings(reportwriterviewmodel);     }); 

the jquery block here insertion of fields dom , event click. apply bindings invoked further line. omitted ko code works everywhere else know issue not in vm in injecting these elements post vm initalization.

i have tried use applybindings , using dom element name no luck

 ko.applybindings(reportwriterviewmodel , document.getelementbyid('filtersection')); 

thanks in advance,

-cheers

i indeed use foreach binding. after all, observable array can have 0 entries if user hasn't clicked "add" yet. knockout designed handle kind of dynamic ui.

use on view model:

reportobject.filterrows = ko.observablearray(); reportobject.addfilterrow = function () {     reportobject.filterrows.push({}); }; reportobject.removefilterrow = function (filterrow) {     reportobject.filterrows.remove(filterrow); } 

and in html:

<div id="filtersection" data-bind="with: reportobject">     <div data-bind="foreach: filterrows">         <div class="filterrow">             <!-- put selects , inputs here -->             <a data-bind="click: $parent.removefilterrow" class='ui-button-text-only'>remove</a>         </div>     </div>     <div id="filterbuilder">         <input data-bind="click: addfilterrow" type="button" value="add filter" title="add filter" class="special" />      </div> </div> 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -