使用java自定义数据和显示商店值

时间:2016-04-26 18:03:25

标签: javascript java jquery mysql autocomplete

我正在使用java进行jQuery自动完成。我正在尝试显示我的关卡和我的地址,但只有在我点击选项时才输入我的字段中的关卡,但是当我点击表单上的保存时,仅在后端商店中输入值。目前,当我执行搜索时,它只显示像Bob Smith这样的名字和姓氏但没有地址,但是当我点击它时。它只输入与bob smith相关的值62,或者可能是其他东西,因为可能有两个bob smith实例。目前,json正在归还:

  

[{“value”:10,“label”:“Bob Doe”,“address”:“”},{“value”:17,“label”:“Bob Doe”,“address”:“” },{“value”:62,“label”:“Bob Smith”,“address”:“1234 Test Site”},{“value”:63,“label”:“Bob Smith”,“address”:“ 1456测试场地二“}]

这是我的java代码:

public class Controller implements test {

    public Response execute(test t) throws App {

        TextResponse tr = t.createTextResponse();

        tr.setContentType("application/json");

        try {


            String term = t.getParameters().getSingle("term");
        //  String sqlJson = t.createSQL("Select c_first_name, c_last_name from t_ct").fetchJSON();

            //String sqlJson = t.createSQL("Select id AS value, c_first_name  + ' ' + c_last_name AS label, c_address_1 AS desc from t_ct where c_first_name like '%" + term + "%' order by c_first_name").fetchJSON();

            String sqlJson = etk.createSQL("Select id AS value, c_first_name + ' ' + c_last_name AS label, c_address_1 AS address from t_ct where c_first_name like '%" + term + "%' order by c_first_name ").fetchJSON();

            tr.put("aadata", sqlJson);


        }catch (Exception e ) {

            e.printStackTrace();
        }


        return tr;

    }

}

这是我的javascript代码:

    jQuery(document).ready(function() {
        jQuery(function() {
                jQuery("#CaseInput_ct, #CaseAttorney, #Cast_employer, #Caserney").autocomplete({

                  //  autoFocus: true,

                 // source: [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ]







                source : function (request, response) {
                jQuery.ajax({
                        url : "page.request.do?page=test",
                        type : "GET",
                        data : {
                                term : request.term
                        },
                        dataType : "json",
                        success : function(data) {  
                            /*
                            var dataArray = [];
                                for(var k = 0; k < data.length; k++) {
                                    // Add items to array
                                    dataArray.push(data[k].label + " " + data[k].description);
                                }
                             */
                                response(data);




                        }
                });
        }




});
});
});

我有没有办法显示标签和地址但是一旦我点击选择它只会输入标签但在后台存储值?

1 个答案:

答案 0 :(得分:1)

我明白了。这是我在控制器中必须做的事情:

public class Controller implements test {

    public Response execute(test t) throws A {

        TextResponse tr = t.createTextResponse();

        t.setContentType("application/json");

        try {


            String term = t.getParameters().getSingle("test");


            String sqlJson = t.createSQL("Select id AS value, c_first_name + ' ' + c_last_name AS label, c_address_1 AS [desc] from t_ct where c_first_name like '%" + term + "%' order by c_first_name ").fetchJSON();

            t.put("aadata", sqlJson);


        }catch (Exception e ) {

            e.printStackTrace();
        }


        return tr;

    }

}

这就是我用javascript做的:

function casetest {


    jQuery(document).ready(function() {
        jQuery(function() {
                jQuery("#Caant, #CaseInporney, #CaseInput_employer, #CaerAttorney").autocomplete({





                source : function (request, response) {
                jQuery.ajax({
                        url : "page.request.do?page=page.ajax.autocomplete",
                        type : "GET",
                        data : {
                                term : request.term
                        },
                        dataType : "json",
                        success : function(data) {  


                               console.log(data);

                                response(data);

                        }

                });
        }, 


      focus: function( event, ui ) {
          jQuery(this).val(ui.item.label);
          jQuery(this).val(ui.item.value);


        return false;
      },  
       select: function( event, ui ) {


          jQuery(this).val(ui.item.label);
          jQuery(this).val(ui.item.value);

        return false;
      }


})

jQuery.ui.autocomplete.prototype._renderItem =  function( ul, item ) 
       {
         return jQuery( "<li>" )
         .append( "<a>" + item.label + " " + item.desc + "</a>" )
         .appendTo( ul );
       };

});
});


}