Dynamically generate html and assign data-bind

121 views
Skip to first unread message

folg...@gmail.com

unread,
Jun 19, 2013, 3:30:43 AM6/19/13
to knock...@googlegroups.com
Hi Guys,

I'm trying to find a way to dynamically generate the html with knockout (instead of jQuery etc) and set data-bind attribute to it.

For example,


<ul data-bind='foreach:MetaData'>
    <li>
          <input type="text" data-bind='value:{{Name}}'>  //how to bind to the content of Name??
    </li>
</ul>

function ViewModel(){
  var self = this;
   self.MetaData = [{ Name: 'FirstName' }, { Name: 'LastName'}];
   self.FirstName = ko.observable();
   self.LastName = ko.observable();
}
var vm = new ViewModel();
ko.applyBindings(vm);


Then i can use self.FirstName() to get binded value,one thing is i don't know how to deal with {{Name}} in the html, anyone has any clue on this?



parwej ahamad

unread,
Jun 19, 2013, 4:08:16 AM6/19/13
to knock...@googlegroups.com
<input type="text" data-bind='value: Name'>  should work!





--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

folg...@gmail.com

unread,
Jun 19, 2013, 4:14:19 AM6/19/13
to knock...@googlegroups.com
Thanks for the reply, but i don't it work.

<input type="text" data-bind='value: Name'> this will try to bind the value to "Name" instead of the content of Name.

parwej ahamad

unread,
Jun 19, 2013, 4:17:33 AM6/19/13
to knock...@googlegroups.com
Can you put your code in fiddle?
in the mean time you can try :

<input type="text" data-bind="value: $data.Name"> 

folg...@gmail.com

unread,
Jun 19, 2013, 4:23:21 AM6/19/13
to knock...@googlegroups.com
Sorry, i just know what i want instead of how to implement that.

$data.Name is same as Name here.

What i want is try to bind the input value to the content of the Name, let's say if the Name is FirstName, then i want it bind to FirstName

parwej ahamad

unread,
Jun 19, 2013, 4:42:26 AM6/19/13
to knock...@googlegroups.com
I misunderstood your question, I hope you are looking this:

Html
<ul data-bind='foreach:MetaData'>
    <li>
          <input type="text" data-bind='value: $parent[Name]()'>  //how to bind to the content of Name??
    </li>
</ul>

Js
        function ViewModel() {
            var self = this;
            self.MetaData = [{ Name: 'FirstName' }, { Name: 'LastName'}];
            self.FirstName = ko.observable('fname'); //Test data
            self.LastName = ko.observable('lname'); //Test data

parwej ahamad

unread,
Jun 19, 2013, 4:54:00 AM6/19/13
to knock...@googlegroups.com
For better understanding, here you can find all binding context:



folg...@gmail.com

unread,
Jun 19, 2013, 4:55:45 AM6/19/13
to knock...@googlegroups.com
Buddy, the solution you mentationed works! 

Here's comes with a more chelange one! if i remove below statements, is there  way to do that?

            self.FirstName = ko.observable('fname'); //Test data
            self.LastName = ko.observable('lname'); //Test data




parwej ahamad

unread,
Jun 19, 2013, 5:02:20 AM6/19/13
to knock...@googlegroups.com
Then what will be your binding value source?
if you want to set any custom value based on your column then go for computed observable.




--

folg...@gmail.com

unread,
Jun 19, 2013, 5:05:24 AM6/19/13
to knock...@googlegroups.com
<input type="text" data-bind='value: $parent[Name]'> will do, no need "()"

folg...@gmail.com

unread,
Jun 19, 2013, 5:13:34 AM6/19/13
to knock...@googlegroups.com
thanks for all the replies, buddy.
Reply all
Reply to author
Forward
0 new messages