2017-04-24 87 views
0

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

到目前为止,我设法做的是发送我的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()函数来重置,但它不工作,我觉得这是我应该使用的功能,但我做的事这里别的错了。非常感谢您花时间帮助我。

回答

0

下面是如何正确地发送AJAX请求,得到响应和接收数据绑定到铁的数据表:

<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; 

    }, 
+0

它的工作完美,谢谢了很多! :) – Exo