如何从grails中的JSON对象获取数据库变量

时间:2016-08-19 12:17:36

标签: javascript json grails groovy

我正在尝试创建一个显示数据库中所有位置的地图。 我可以为一个对象做这个,但我不能为所有对象做这个。

在我的控制器中,我获得所有商店并发送给angularjs文件

public class tabActivity extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;

private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tab);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());


    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
}

public static class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if ( getArguments().getInt(ARG_SECTION_NUMBER) == 1){
            View rootView = inflater.inflate(R.layout.fragment_owner, container, false);
            return rootView;
        }


        else {
            View rootView = inflater.inflate(R.layout.fragment_tab, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }
}


public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Your Cars";
            case 1:
                return "Rented";
            case 2:
                return "GPS Tracker";
        }
        return null;
    }
}
}

javascriptfile; //适用于一家商店

 def getalllocation(){
    def shops=Shop.findAll()
    [shops:shops] as JSON
}

我可以使用多个标记创建一个地图,但不能创建数据库;

 $scope.getshopinfo = function () {
        $http.post("/shop/getshoplocation", data = {shopId: $("#shopId").val()}).success(function (json) {
            $scope.shop = json;
            //$scope.map = {center: {latitude: 45, longitude: -73}, zoom: 8};
            //$scope.map = {center: {latitude: $scope.shop.shopLattitude, longitude: $scope.shop.shopLongitude}, zoom: 8};
            $scope.map = {
                center: {latitude: $scope.shop.shopLattitude, longitude: $scope.shop.shopLongitude},
                zoom: 10,
                events: mapevents
            };
            $scope.searchbox = {
                template: "searchbox.tpl.html",
                events: searchevents
            };
            $scope.marker = {
                coords: {latitude: $scope.shop.shopLattitude, longitude: $scope.shop.shopLongitude},
                id: '2'
            };
            $("#shopLattitude").val($scope.shop.shopLattitude);
            $("#shopLongitude").val($scope.shop.shopLongitude);
           // $scope.$apply();
        }).finally(function () {
        });
    };
    if ($("#shopId").val()) {
        $scope.getshopinfo();
    }

}]);

我不知道如何组合这些代码并从数据库位置获取带有多个标记的地图。谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

我想我在这里理解这个问题。因为你得到了否定,我也提出了你的问题。我想这是因为你的问题实际上包含了很多代码,而不是实际问题的细节。

无论如何,我希望我所说的将会帮助你和其他人以不同的方式解决这类问题。

这有点矛盾:

创建一个显示数据库中所有位置的地图。我可以为一个对象执行此操作,但在查看控制器代码时,我无法做到这一点:

def getalllocation(){
    def shops=Shop.findAll()
    [shops:shops] as JSON
}

你的gsp实际上有

  

$ http.post(" / shop / getshoplocation",data = { shopId:   $(" #shopId")。val()})。success(function(json){

您正在发送一个参数但是然后在控制器中您正在查找所有对象。

 def getalllocation(){
        // Firstly when creating new methods always use debug 
        // like this to figure out what you are getting
        println "my params are $params"

        //I can see you are passing in an actual shopId so 
        //lets add some logic to either list all or a given shopId
        //No idea what the object here is
        def shops
        if (params.shopId) {
              shops=Shop.where{id==params.shopId}.findAll()
        } else {
             shops=Shop.findAll()
        }
        [shops:shops] as JSON
    }

有了这个,你应该希望它有效,因为在你的代码中它会在点击商店ID时显示它。

至于你的矛盾,你说你希望它显示所有商店,它只显示一个

  

"虽然您的代码已被定义为将shopId传递给控制器​​?"

无论如何,它无法显示或显示当前地图的原因是因为您正在返回所有商店条目的JSON地图,但是当您收到json时,您似乎试图解析包含json列表的列表一旦。我认为你需要迭代它。

Something like this

  $http.post("/shop/getshoplocation", data = {shopId: $("#shopId").val()}).success(function (json) {

        $scope.companies = json.forEach(function(entry) {
            center: {latitude: $entry.shopLattitude, longitude: $entry.shopLongitude},
                zoom: 10,
                events: mapevents
         }
}

我无法访问任何此类内容,所有上述内容都是猜测工作之后的所有内容,可能不是100%,只是让您了解它可能会发生什么变化错。