MVC5项目中的页面使用bootstrap-table,其中一列是这样的:    { field: '', title: '委托', halign: 'center', align: 'center', valign: 'middle', width: 70, sortable: false, formatter: WWFormatter },其中formatter的功能:通过以下ajax语句从controller返回一个字符串,如果该字符串不为'0'就显示一个<i>元素,否则什么都不显示。        function WWFormatter(value, row, index) {
            $.ajax({
                type: "Get",
                url: "/Business/GetWWCount",
                data: { id: row.业务ID },
                async: true,
                success: function (data) {
                    if (data != '0') {
                        return '<i class="text-danger fa fa-check-circle fa-lg"></i>';
                    }
                    else
                    {
                        return '';
                    }
                },
                error: function () { return ''; }
            });
        }controller中的函数:        public string GetWWCount(int id)
        {
            return  "1";  //为方便调试,直接返回"1"
        }
运行结果:对应单元格什么也不显示。
感觉似乎formatter中的异步ajax的success回调函数中的结果不能显示到页面。通过console.info确定已经进入了 if (data != '0') {...}请教各位高手,我哪里没对?该如何处理?

解决方案 »

  1.   

    把ajax返回的 <i class="text-danger fa fa-check-circle fa-lg"></i> 拼到页面上的代码在哪?
      

  2.   

    不知道您为啥需要在foramtter中使用ajax服务请求,这不是foramtter要做的事情,建议您优化代码结构。
    您的代码应该是异步问题,async: true, 改成async:false
      

  3.   

    var data;
    $.ajax({
                    type: "Get",
                    url: "/Business/GetWWCount",
                    data: { id: row.业务ID },
                    async: true,
                    success: function (result) {
                      data=result;
                    },
                    error: function () { return ''; }
                });
      if (data != '0') {
                            return '<i class="text-danger fa fa-check-circle fa-lg"></i>';
                        }
                        else
                        {
                            return '';
                        }
      

  4.   

    把返回的参数提出去,在外面做判断,我给你写的直接放在formatter里面
      

  5.   

    不要放到formatter里取数据,放到外面用updateCell更新数据,formatter是即时返回的
    $("#table").bootstrapTable('updateCell', {
    index: 1, 
    field: 'name',
    value: 'xyz'
    });
      

  6.   

    我遇到这个问题了,后来解决了,简单来说 把return 放到外面,所以代码如下
    function WWFormatter(value, row, index) {
               var info;
                $.ajax({
                    type: "Get",
                    url: "/Business/GetWWCount",
                    data: { id: row.业务ID },
                    async: true,
                    success: function (data) {
                        if (data != '0') {
                            info= '<i class="text-danger fa fa-check-circle fa-lg"></i>';
                        }
                        else
                        {
                            return '';
                        }
                    },
                    error: function () { return ''; }
                });return info 
            }