0

Split the data binding of KnockoutJS into table C#

This is my previous topic about getting the value which is displayed as JSON and parse them into table rows and columns, the answer could solve my question thanks to @GoTo however, because I have to split the structure of KnockOut JS into js files, therefore create another question for me

I have splitted them into 3 files, 2 js files and one cshtml file which contains the table

The first js is :

biz.js

var mapDictionaryToArray = 

    function (dictionary) {
        var result = [];
        for (var key in dictionary) {
            if (dictionary.hasOwnProperty(key)) {
                result.push({
                    key: key,
                    value: dictionary[key]
                });
            }
        }
        return result;

};

The second js is :

config.js

function ConfigViewModel() {

    self.testParams = mapDictionaryToArray;
    self.contents = ko.observable({
        "reference": "2Z94",
        "car_id": "9861"
    });

}
$(document).ready(function () {
    ko.applyBindings(new ConfigViewModel());
});

The cshtml file :

HTML cshtml :

<table class="table table-hover">
                        table test:
                        <tbody data-bind="foreach: testParams(contents())">
                            <tr class="data-hover">
                                <td>
                                    <span id="textKey" data-bind="text: key"></span>

                                </td>
                                <td>
                                    <span id="textValue" data-bind="text: value"></span>

                                </td>
                            </tr>
                    </tbody>
                </table>

The result from fiddler in my previous topic worked great (FiddlerKnockout), but then it doesn't work for the split of new structure

Community
  • 1
  • 1
TrangZinita
  • 107
  • 1
  • 9
  • You need to add `biz.js` before `config.js` if it not already the case. What error do you get? – GôTô Jun 04 '14 at 08:59
  • @GôTô : the data is null in my case for the table, I guess the data from contents cannot be read in the cshtml file – TrangZinita Jun 04 '14 at 09:16
  • Why did you replace `$data.key` with `key`? – GôTô Jun 04 '14 at 09:41
  • @GôTô : I replaced them for testing only, as $data as I know is the prefix and we can ignore them if the context is bounded ? in your example it also works without $data – TrangZinita Jun 04 '14 at 09:46
  • True... What happens if you put a breakpoint at `return result`? What does `result` contain? – GôTô Jun 04 '14 at 12:24
  • @GôTô : I did debug it last time but the breakpoint was never be reached in js file :( is there any simpler way that I can embed C# code in data-bind then I can convert them directly by C# code Dictionary<> ? I don't have the control on the 'contents' just the value in JSON format. Thanks GoTo – TrangZinita Jun 04 '14 at 12:36
  • You are sure the script tags are included in the right order? `biz.js` and `knockout.js` should be before `config.js` – GôTô Jun 04 '14 at 12:39
  • @GôTô : Yes I have verified they are in the correct order. Thanks – TrangZinita Jun 04 '14 at 12:41
  • Join me in chat http://chat.stackoverflow.com/rooms/55062/knockout-bistro – GôTô Jun 04 '14 at 12:49

0 Answers0