Grails:从Controller调用JS

时间:2015-03-19 02:47:44

标签: grails

我有一个控制器,里面有一个方法:

class teachController {

   def updateIndex () {

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

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

var drawIndex (var indexValues) {

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

如何在控制器功能中调用上述JavaScript函数?

1 个答案:

答案 0 :(得分:1)

如果您要执行的操作是从控制器动态更新包含数据的表。 我会用ajax来执行javascript函数" drawIndex()"在它调用控制器动作" updateIndex()"。

之后
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)
    }

这应该有效。 (至少在我喜欢的方式)希望它有所帮助。