DynaTable - 将数据集记录更新为新数据集

时间:2015-09-15 07:36:05

标签: javascript jquery ajax dynatable

我有这个:MyModel:

function MyModel(title, status, user, lastUpdated, local_id) {
    this.title = title;
    this.status = status;
    this.reported_by = { username: user };
    this.utc_last_updated = lastUpdated;
    this.local_id = local_id;
    return this;
}

而且,我有这个render_and_update()函数:

function render_and_update(owner, newList, callBack){

    function tbodyWriter(id, MyModel) {

      var tRow = '<tr class="example-row"><th class="local-id">' + MyModel.local_id 
        + '</th><th class="title">' + MyModel.title +'</th><th class="status">'
        +MyModel.status +'</th><th class="reported-by">' + MyModel.reported_by.username 
        + '</th><th class="last-updated">' + MyModel.utc_lastUpdated + '</th><th class="display-none">' 
        + MyModel.utc_lastUpdated.getTime() + '</th></tr>';
      return tRow;

    }

    $('table-collection').dynatable({
        dataset: {
            records: newList,
            perPageDefault: 10,
            perPageOptions: [5, 10, 20]
        },
        writers: {
            _rowWriter: tbodyWriter
        }
    });
    callBack();
}


function MainController() {
    getUpdatedData(function(owner, updatedData) { /* make ajax call & returns updated records list on success*/
        render_and_update(owner, updatedData, function() { /* function having problem */
            console.log('end..');
        });
    });
}

$('my-button').click(MainController);

问题是:当我点击按钮时,它调用render_and_update()函数,第一次插入记录集,但第二次点击它不会将数据集更新为新数据集......

为什么DOM没有更新?

感谢。

1 个答案:

答案 0 :(得分:0)

我自己解决了这个问题..

解决了我的问题:

if(clicked) {
     dynatable.settings.dataset.originalRecords = issuesList;
     dynatable.process();
}

更新了update_and_render()函数和MainController()函数:

function render_and_update(clicked, owner, newList, callBack){

    function tbodyWriter(id, MyModel) {

      /* Nothing changed in this function. i.e., same as above in question. */

    }

    var dynatable = $('table-collection').dynatable({
        dataset: {
            records: newList,
            perPageDefault: 10,
            perPageOptions: [5, 10, 20]
        },
        writers: {
            _rowWriter: tbodyWriter
        }
    }).data('dynatable');

    if(clicked) {
        dynatable.settings.dataset.originalRecords = issuesList;
        dynatable.process();
    }

    callBack();
}

function MainController() {
    getUpdatedData(function(owner, updatedData) { /* make ajax call & returns updated records list on success*/
        render_and_update(true, owner, updatedData, function() { /* function having problem */
            console.log('end..');
        });
    });
}

希望将来能帮助别人!