English 中文(简体)
Refresh KnockoutJS 数据
原标题:Refresh KnockoutJS Data In DataTables

页: 1

<table class="datatable zebra-striped">
    <thead>
        <tr>
            <th>Name</th>
            <th>Issue</th>
            <th></th>
        </tr>
    </thead>
    <tbody data-bind="foreach: Publications">
        <tr>
            <td><span data-bind="text: Name" /></td>
            <td><span data-bind="text: IssueNumber" /></td>
            <td><button type="button" class="btn" data-bind="click: $parent.DeletePublication">Delete</button></td>
        </tr>    
    </tbody>
</table>

这里是观点的表象:

function EditReaderViewModel() {
    var self = this;

    self.Publications = ko.observableArray([]);

    self.OnAddPublicationSaveButtonClicked = function () {
        //.. code omitted.    
        self.Publications.push(new Publication(publication.value, publication.label, publication.issueNumber));
    iii;
iii;

iii

和出版物对表格表示反对:

function Publication(id, name, issueNumber) {
    var self = this;

    self.Id = id;
    self.Name = name;
    self.IssueNumber = issueNumber;
    self.LoadedFromDatabase = false;
iii

当数据首次装入表格时,数据表的初始化就是如此。

$(".datatable").dataTable({ // blahh iii);

Everything works fine - the data is loaded in the table, sorting and filtering is working, etc.

Now when I add a new item to the Publications array in the viewmodel things just fall apart. For example, say I originally have 1 item in the Publications array, when I add another item, it will appear in the table until I click on a column heading to sort.

看来,该数据表有自己的内部数据清单,其中不包括新设立的出版物一。

考虑到数据来自KnockoutJS观点model,有人可以指导我如何重建数据表?

请注意,我研究了这一具有约束力的假想:

http://www.joshbuckley.co.uk/07/knockout-js-datatable- bindings/

问题是,当我使用时,我收到错误信息,例如:

Requested unknown parameter 0 from the data source for row 0.

EDIT:

我看着该守则,我认为我看到这个问题。 在j Query.DataTables.js中,有以下功能:_fnGetCellData(oSettings, iRow, iCol, sSpecific >

该职能如下:

if ( (sData=oCol.fnGetData( oData )) === undefined )

这反过来又要求:

function _fnGetObjectDataFn( mSource )

现在,在<条码>上,有以下简称:

else
{
    /* Array or flat object mapping */
    return function (data) {
         return data[mSource];
    iii;
iii 

data is not an array, it s a JSON object which means that the above code will fail (return data[mSource];).

I m really not sure what to do here.

EDIT - IMPORTANT:

As one of the commentators has noticed, and I ve just confirmed, accessing

将会发出“特洛伊”警告。 因此,请了解这艘船现在是一个大的NO-NO。

问题回答

I know that´s an older thread but since I found it when I had the same problem I feel the need to post the solution so others might find it quicker.

When refreshing the Table you can simple .clear() and then .destroy() the table. After that just reinitialize the table using the already known .DataTable()

尽管这给人留下了微薄的印象,因为你确实摧毁了结构并重建了结构,但这项工作非常顺利。

You just have to add a boolean identifier you set to true the first time you initialize the table. And of course you have to bind the Table to a variable to be able to work the DataTable API magic on the DOM Element.

    if (managementLoadedDone){
            userTable.clear();
            userTable.destroy();
        }

表10

    userTable = $("#userTable").DataTable({responsive: true});
            usersLoaded = true;

You mention that you used datatable plugin and get "Requested unknown parameter 0 from the data source for row 0" error.

In my previous experineces using datatable without knockout plugin i got this error only using more columns than the header definitions or vice versa. You have to check your tbody and thead section and make it sure that both of them is the same size.

或者,在你开始表态时,你可以采用“oColumnDefs”办法。 再次检查了该选项,并确保你有意达到不为人知的一栏:

举例来说,如果你有类似的定义的话,就应当如此。

"aoColumnDefs": [{
         "bSearchable": false,
         "bVisible": false,
         "aTargets": [0]
     }, {
         "bSortable": false,
         "bSearchable": false,
         "sWidth": "45px",
         "aTargets": [2]
     }]

如果只有两节,就会给你这个错误。 由于你达到具体目标的第3栏: 页: 1

我正经历着与你在这里描述的类似问题(尽管这确实是安全的),在Kaspersky警告之后,我无意探讨“具有约束力的”联系。

http://stackoverflow.com/questions/9836713/knockoutjs-how-to-use-datatables-with-based-table” Js - how to use datatable with existing limit table,我最后走了一条路,在桌上使用 k时,如果说可以观测到的Array有至少一行数据,我就增加了一个 k。

<!-- if Publications.length() > 0 -->
<table class="datatable zebra-striped">
    <thead>
        <tr>
            <th>Name</th>
            <th>Issue</th>
        </tr>
    </thead>
    <tbody>
        <!-- ko foreach: Publications -->
        <tr>
            <td><span data-bind="text: Name" /></td>
            <td><span data-bind="text: IssueNumber" /></td>
        </tr>  <!-- /ko -->
    </tbody>
</table>
<!-- /ko -->

When loading the View Model, I then called the dataTables function inside my .ajax success function.

Then the other part to making it work was to make use of dataTables "bStateSave": true, so that any sorting I had done prior to a refresh would not be lost.





相关问题
Get DynamicResource Binding in WPF

Can any one help me to get DynamicResource Binding in WPF by code? I have set binding Like follow, TextBlock Background={DynamicResource ColorA} Name="TB" in Xaml. and i need to get the - TB s ...

WPF Binding to specific items in collection

I m currently trying to bind to certain items in a collection in wpf. This is best explained by an example. My XAML is below: <Canvas Name="TaskCanvas" Width="467.667" Height="414"> <...

WPF Dynamic Binding X and Y Co-ordinates

I have a question on WPF dynamic positioning. I want to place Elipses on the screen based on X and Y co-ordinates that i have stored in a collection in C#. I have been made aware of the drawing ...

WPF Data Binding Error in ListBox

I have a ListBox: <ListBox x:Name="HistogramListBox" Grid.Column="1" Margin="8,2,8,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Template="{StaticResource ...

WCF binding error

So I got into work early today and got the latest from source control. When I try to launch our ASP.NET application, I get this exception: "The binding at system.serviceModel/bindings/wsHttpBinding ...

silverlight 3 collection binding

Someone please help me understand why this binding does not work... I have a class called SelectionManager with a property called dates which is populated by a WCF service. The property is an ...

热门标签