使用名字,姓氏和电子邮件搜索Angular 2

时间:2016-09-27 11:02:34

标签: angular typescript

详细

我正在尝试实现搜索管道,用户可以通过该搜索管道搜索电子邮件/名字/姓氏,但现在它完全可以使用电子邮件单一搜索项目。我想搜索相同的文字,输入电子邮件的名字和姓氏。

图像

enter image description here

搜索管道

public class MainActivity extends ActionBarActivity
{
// Declaring layout button, edit texts
Button login;
EditText username,password;
ProgressBar progressBar;
// End Declaring layout button, edit texts

// Declaring connection variables
Connection con;
String un,pass,db,ip;
//End Declaring connectaaion variables
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Getting values from button, texts and progress bar
    login = (Button) findViewById(R.id.button);
    username = (EditText) findViewById(R.id.editText);
    password = (EditText) findViewById(R.id.editText2);
    //progressBar = (ProgressBar) findViewById(R.id.progressBar);
    // End Getting values from button, texts and progress bar

    // Declaring Server ip, username, database name and password
    ip = "yourIP";
    db = "DatabaseName";
    un = "userName";
    pass = "password";
    // Declaring Server ip, username, database name and password

    // Setting up the function when button login is clicked
    login.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            CheckLogin checkLogin = new CheckLogin();// this is the Asynctask, which is used to process in background to reduce load on app process
            checkLogin.execute(username.getText().toString(),password.getText().toString());
        }
    });
    //End Setting up the function when button login is clicked
}

public class CheckLogin extends AsyncTask<String,String,String>
{
    String z = "";
    Boolean isSuccess = false;

    @Override
    protected void onPreExecute()
    {
        //progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onPostExecute(String r)
    {
        //progressBar.setVisibility(View.GONE);
        Toast.makeText(MainActivity.this, r, Toast.LENGTH_SHORT).show();
        if(isSuccess)
        {
            Toast.makeText(MainActivity.this , "Login Successfull" , Toast.LENGTH_LONG).show();
            //finish();
            Intent intent = new Intent(MainActivity.this, HomePage.class);

            startActivity(intent);
            finish();
        }
    }
    @Override
    protected String doInBackground(String... params)
    {
        String usernam = params[0];
        String passwordd = params[1];
        if(usernam.trim().equals("")|| passwordd.trim().equals(""))
            z = "Please enter Username and Password";
        else
        {
            try
            {
                con = connectionclass(un, pass, db, ip);        // Connect to database
                if (con == null)
                {
                    z = "Check Your Internet Access!";
                }
                else
                {
                    // Change below query according to your own database.
                    //String query ="select * from dbo.AA_UserMaster where UserName= 'test' and Password = 'admin1*'";
                    String query = "select UserId from [AA_UserMaster] where UserName= '" + usernam.toString() + "' and Password = '"+ passwordd.toString() +"'  ";
                    Statement stmt = con.createStatement();
                    ResultSet rs = stmt.executeQuery(query);
                    if(rs.next())
                    {
                        z = "Login successful";
                        isSuccess=true;
                        con.close();
                    }
                    else
                    {
                        z = "Invalid Credentials!";
                        isSuccess = false;
                    }
                }
            }
            catch (Exception ex)
            {
                isSuccess = false;
                z = ex.getMessage();
            }
        }
        return z;
    }
}


@SuppressLint("NewApi")
public Connection connectionclass(String user, String password, String database, String server)
{
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Connection connection = null;
    String ConnectionURL = null;
    try
    {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";" + database + ";user=" + user+ ";password=" + password + ";";
        connection = DriverManager.getConnection(ConnectionURL);
    }
    catch (SQLException se)
    {
        Log.e("error here 1 : ", se.getMessage());
    }
    catch (ClassNotFoundException e)
    {
        Log.e("error here 2 : ", e.getMessage());
    }
    catch (Exception e)
    {
        Log.e("error here 3 : ", e.getMessage());
    }
    return connection;
}

响应

import {Pipe} from '@angular/core';


@Pipe({
    name: "search"
})

export class SearchPipe {

    transform(value, term) {
        console.log(term + ' ' + value);
        if (term == null) {
            return null;
        }
        return value.filter((item) => item.Email.includes(term));
    }
}

1 个答案:

答案 0 :(得分:2)

您已经完成了最难的部分,只需根据电子邮件名字姓氏

更改管道进行过滤
import {Pipe} from '@angular/core';


@Pipe({
    name: "search"
})

export class SearchPipe {

    transform(value, term) {
        console.log(term + ' ' + value);
        if (term == null) {
            return null;
        }
        return value.filter((item) => {
            return item.Email.includes(term) || item.FirstName.includes(term) || item.LastName.includes(term);
        });
    }
}