铁数据表的问题

时间:2017-04-24 16:23:20

标签: javascript ajax polymer

我对Polymer的动态表组件有一些问题。这是我想要实现的:我想发送带有ajax请求的sql请求,并使用该请求的响应更新表。

到目前为止,我设法做的是发送我的sql请求,服务器然后用数据创建一个.json文件,然后我使用另一个请求获取该文件并将其放入表中。

问题是我必须刷新整个网页才能填满表格。

有没有办法直接用第一个ajax请求的响应更新表,而不是使用json文件?

这是我的代码:

<template>
    <iron-ajax 
    id="request" 
    url="/request" 
    method="POST" 
    on-response="ajaxResult"
    on-error="ajaxError"
    content-type="application/json"
    body: {id, Prio, Stat}></iron-ajax>

<iron-ajax url="../data.json" last-response="{{data}}" auto></iron-ajax>

<iron-data-table items="[[data]]" id="table123">
<data-table-column name="Title">
    <template>[[item.ID]]</template>
</data-table-column>
<data-table-column name="PRIORITY">
    <template>[[item.PRIORITY]]</template>
</data-table-column>
<data-table-column name="REQUEST_STATUS">
    <template>[[item.REQUEST_STATUS]]</template>
</data-table-column></iron-data-table>

</template>

<script>
    Polymer({
        is: 'gestion-ticket',

    ajaxResult: function(e) {
        //var Object=e.detail.response;
        document.getElementById("table123").clearCache();

    },
    f: function(e) {
        var body = {
        "id": this.querySelector("#comment").value,
        "Prio" : this.querySelector("#Priority").selected,
        "Stat" : this.querySelector("#Status").selected
        };
        this.$.request.body = body;
        this.$.request.generateRequest();
        },
    ajaxError: function(event) {
        console.log(event);
        console.log(event.detail);
        console.log(event.detail.request);
        console.log(event.detail.request.xhr.response);
    }
    });
</script>

我尝试了clearCache()函数来重置,但它不能正常工作,我觉得这是我应该使用的功能,但我在这里做了别的错事。非常感谢您花时间帮助我。

1 个答案:

答案 0 :(得分:0)

Here is how to properly send AJAX request, receive response and bind received data to iron data table:

 <iron-ajax 
        method="POST"
        handle-as="json"
        id="iron_ajax_element"
        on-response="recieve_responseHandler"
        ></iron-ajax>

 .....

    send_request: function() {

            console.log('send_request:');

            var iron_ajax_element = this.$.iron_ajax_element;


            //set AJAX URL point
            iron_ajax_element.url = this.some_url;

            //set AJAX params
            var aniArgs = {};
            aniArgs['param'] = this.some_param;
            iron_ajax_element.params = aniArgs;

            //run AJAX
            iron_ajax_element.generateRequest();
    },
    recieve_responseHandler: function(e) {

        console.log('recieve_responseHandler:');

        console.log(e.detail.response);
        var response  = e.detail.response;
        console.log(response);

       //Bind the response to iron-data-table here, as:
       this.iron_data = response;

   },