Grails:从控制器调用 JS

Grails : Call JS from Controller

我有一个带有方法的控制器:

class teachController {

   def updateIndex () {

    // Do something here
    ----------
  }
}

现在在 GSP 中有一个 JavaScript 函数:

var drawIndex (var indexValues) {

  // Do something here
 -----------------
  }

如何从控制器函数内部调用上述 JavaScript 函数?

如果您想做的是使用来自控制器的动态数据更新 table。 在调用控制器操作 "updateIndex()".

之后,我会使用 ajax 来执行 javascript 函数 "drawIndex()"
class teachController {
    def updateIndex(){
         // Do something here
          withFormat {
                json {  
                    render indexValues as JSON
                }
          }
    }
}

然后从您的 gsp 中调用名称为 updateIndex.json 的控制器操作并使用 remoteFunction

function dynamicallyUpdateTable(){
    ${remoteFunction(
           action: 'updateIndex.json',
           onSuccess: 'drawIndex(data)' 
                    )
   }
}

这将调用您的 javascript 函数

 function drawIndex(indexValues){
        // Do something here
        //console.log(indexValues)
    }

这应该行得通。 (至少以我喜欢的方式)希望对您有所帮助。