Pregunta

I am getting json data as output and binding in knockout.

Json Data

{
"frequency": [
    {
        "freqid": "2",
        "freqname": "Monthly",
        "date": [
            "1",
            "7",
            "10",
            "14",
            "20",
            "21",
            "28"
        ],

        "ny": "6.0",
        "ty": "0"
    },
    {
        "freqid": "3",
        "freqname": "Quarterly",
        "date": [
            "1",
            "7",
            "10",
            "14",
            "20",
            "21",
            "28"
        ],

        "ny": "4.0",
        "ty": "0"
    }
]

}

This is the valid json and i am looping for frequency in my html using knockout binding and displaying the data like this.

<div data-bind="foreach: FreqList">
                <input type="hidden"  data-bind="attr: { id: 'hid_fr_' + freqid(), name: 'hid_freq_' + freqid()  } " >
</div>   

Here it is working fine. The problem is i want to have date loop which is in frequency loop and want to have date data by # separation like for first loop value should be.

<input id="hid_fr_2" name="hid_fr_2" value="1#7#10#14#20#21#28" />

How to achieve this using knockout loop. Please let me know.

¿Fue útil?

Solución

Your best option to achieve this would be to have a javascript function that populates the value attribute for your input. i.e.

<div data-bind="foreach: FreqList">
    <input type="hidden" data-bind="attr: { id: 'hid_fr_' + freqid(), name:'hid_freq_' + freqid(), value: createDateValue(date) }">
</div>

Then in Javascript you would need something along the lines of:

function createDateValue(dates) {
   return dates.join("#");
}

Also note that in your code 'freqid()' will not work as the value is not an observable, it should instead by 'freqid'.

Hope this helps point you in the right direction.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top