You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WinterCMS中jQuery请求后renderPartial无法重新渲染问题排查

问题解决方案

核心问题分析

  1. DOM选择器不匹配:onCompanies方法返回的更新目标是#tabs-1,但实际承载局部视图的容器是#tabs-2,导致AJAX无法找到正确的替换位置。
  2. AJAX响应未返回更新片段:onUserBlock调用onCompanies后,没有将生成的更新视图返回给前端,前端无法获取新的HTML内容进行替换。

具体修改步骤

1. 修正onCompanies方法的返回目标

修改Account.php中的onCompanies方法,将返回的DOM选择器改为实际容器#tabs-2:

public function onCompanies()
{
    $usr = Auth::User()->getAttributes();

    $list = Array();

    $all = Db::select('select * from users_allocation where slave_id = ? and blocked < 1', [$usr['id']]);

    if (count($all) > 0) {
        foreach ($all as $a) {
            $company = Db::select('select * from users where id = ?', [$a->owner_id]);
            if (count($company) > 0) {
                foreach ($company as $cp) {
                    $cid = md5($cp->id);

                    $list[$cid]['user_id'] = md5($a->owner_id);
                    $list[$cid]['name'] = $cp->name;
                    $list[$cid]['email'] = $cp->email;
                }
            }
        }
    }

    $this->page['companies'] = $list;
    // 修正选择器为实际容器#tabs-2
    return [
        '#tabs-2' => $this->renderPartial('companies')
    ];
}

2. 让onUserBlock返回更新后的视图

修改Account.php中的onUserBlock方法,在完成数据库操作后,返回onCompanies生成的更新片段:

public function onUserBlock()
{
    $data = post();
    $id = $data['alt'];
    $usr = Auth::User()->getAttributes();

    $company = Db::select('select * from users_allocation where  md5(owner_id) = ? and slave_id = ? and blocked = 1', [$id, $usr['id']]);
    
    if (count($company) > 0) {
        Flash::error('This company already blocked.');
        return false;
    }
    else
    {
        Db::update('update users_allocation set blocked = 1 where md5(owner_id) = ? and slave_id = ?', [$id, $usr['id']]);

        Flash::success(Lang::get('Company successfully blocked and will not able to send you invitation.'));
        
        // 返回更新后的局部视图
        return $this->onCompanies();
    }
}

原理说明

WinterCMS的AJAX请求(通过jQuery的request()方法触发)依赖后端返回的选择器-HTML内容键值对来自动更新页面DOM。只要确保返回的选择器与页面中承载局部视图的容器ID一致,并且在AJAX处理方法中返回该更新片段,前端就能自动完成局部视图的重新渲染。

内容的提问来源于stack exchange,提问作者Maris Janelsins

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 08:03:12