在每个ajax响应之后在div中显示json返回的消息,extjs4网格

发布于 2025-01-06 03:10:11 字数 682 浏览 2 评论 0原文

我有一个 gridpanel ,一个模型,一个带有 ajax 代理的自动同步存储,具有读取和更新功能以及 extjs4 中的 RowEditing 插件。

考虑以下 json:

{ "success": true,"Message":"Grid Data Successfully updated.", "users": {"uname":"jdoe","fname":"John","lname":"Doe","SSN":125874,"mail":"[email protected]"},{"uname":"jsmith","fname":"Jack","lname":"Smith","SSN":987456,"mail":"[email protected]"}} 

我想知道是否有办法在收到每个响应后将“Message”的值呈现到 HTML div 标签(例如 my_div)?

I have a gridpanel , a Model, an autosync Store with an ajax proxy with read and update functions and a RowEditing plugin in extjs4 .

Consider the following json:

{ "success": true,"Message":"Grid Data Successfully updated.", "users": {"uname":"jdoe","fname":"John","lname":"Doe","SSN":125874,"mail":"[email protected]"},{"uname":"jsmith","fname":"Jack","lname":"Smith","SSN":987456,"mail":"[email protected]"}} 

I want to know if there is a way to render the value of "Message" to an HTML div tag (my_div for example) after receiving each response?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风吹 2025-01-13 03:10:11

您可以使用 DOM Helper,请参阅 sencha api : http://docs.sencha.com/ext-js/4-0/#!/api/Ext.DomHelper

Ext.onReady(function(){
    Ext.DomHelper.insertHtml('beforeBegin', Ext.getDom('test'), "Prepend this string"); 
});​

上面的代码将获取 ID 为 test 的 HTML 元素,并且它将插入字符串在此 div 内容的前面添加此字符串 beforeBegin

查看要玩的小提琴: http://jsfiddle.net/PddU4/Prepend 这个字符串

编辑2012-02-16:

你需要监听你的代理成功失败:(你也可以在加载你的代理时实现一个监听器存储或更新)

listeners: {
    success: function( response, options ){     
       console.log(response);
    },
    failure: function( response, options ){
        console.log(response);
    },
}

根据您的评论进行编辑

首先确保您在阅读器中正确配置了 successPropertymessageProperty。然后在您想要的位置实现侦听器,更新、删除、添加、异常等:(

在代理对象中配置侦听器)

listeners   : {
    update : function(thisStore, record, operation ) {
        console.log('Update happened');
        console.log(record);
        console.log(operation);
    },
    save : function() {
        console.log('Save happened');
    },
    exception : function(dataproxy, type,  action, options,response,  arg) {
        console.log('Error happened');
        console.log(response);
                    doJSON(result.responseText);
    },
    remove : function() {
        console.log("Record removed");
    }
}

当您 console.log(response) 时,您将看到响应对象。这将是您实际的 JSON,因此您需要解析它(如 doJSON() 方法中所示):

 function doJSON(stringData) {
    try {
        var jsonData = Ext.util.JSON.decode(stringData);
        Ext.MessageBox.alert('Success', 'Your server msg:<br />jsonData.date = ' + jsonData.message);
    }
    catch (err) {
        Ext.MessageBox.alert('ERROR', 'Could not decode ' + stringData);
    }
  }

请查看此 AJAX 教程:http://www.sencha.com/learn/legacy/Manual:Core:Ext.Ajax

You can use the DOM Helper, see the sencha api : http://docs.sencha.com/ext-js/4-0/#!/api/Ext.DomHelper

Ext.onReady(function(){
    Ext.DomHelper.insertHtml('beforeBegin', Ext.getDom('test'), "Prepend this string"); 
});​

Above code will get the HTML element with ID test and it will insert the string Prepend this string beforeBegin of the content of this div.

See the fiddle to play around: http://jsfiddle.net/PddU4/Prepend this string

EDIT 2012-02-16:

You need to listen to your proxies success and failure: (you could also implement a listener when loading your store or on update)

listeners: {
    success: function( response, options ){     
       console.log(response);
    },
    failure: function( response, options ){
        console.log(response);
    },
}

EDIT BASED ON YOUR COMMENT:

First make sure you configured properly your successProperty and messageProperty in your reader. Then implement the listener where you want it to be, update, remove, add, exception etc. :

(configure the listener within your proxy object)

listeners   : {
    update : function(thisStore, record, operation ) {
        console.log('Update happened');
        console.log(record);
        console.log(operation);
    },
    save : function() {
        console.log('Save happened');
    },
    exception : function(dataproxy, type,  action, options,response,  arg) {
        console.log('Error happened');
        console.log(response);
                    doJSON(result.responseText);
    },
    remove : function() {
        console.log("Record removed");
    }
}

When you console.log(response), you will see the response object. This would be your actual JSON so you need to parse it (as in doJSON() method):

 function doJSON(stringData) {
    try {
        var jsonData = Ext.util.JSON.decode(stringData);
        Ext.MessageBox.alert('Success', 'Your server msg:<br />jsonData.date = ' + jsonData.message);
    }
    catch (err) {
        Ext.MessageBox.alert('ERROR', 'Could not decode ' + stringData);
    }
  }

Please check out this AJAX tutorial: http://www.sencha.com/learn/legacy/Manual:Core:Ext.Ajax

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文