Welcome Guest, Not a member yet? Register   Sign In
New Tutorial: Extending JTaby - Creating new Form Elements
#1

[eluser]Majd Taby[/eluser]
Hey guys, I just created a new tutorial (inspired by a discussion on #codeigniter on irc.freenode.net). I created a new form element based on the DropDown element, which uses data from another table. This way, if you have a relational DB, you can pull data from multiple tables.

Watch the video
Again, please excuse the initial wait, all the video sites aren't good for code tutorials.

If you don't know what I'm talking about, checkout JTaby in this post and give it a try Smile

Here's the code that we create in the tutorial:

Add somewhere in backend/libraries/jtabyforms.php
Code:
class DbDropDown extends DropDown{
    var $CI;
    function DbDropDown($name,$params){
        $this->CI = &get;_instance();
        $this->CI->load->database();
        DropDown::DropDown($name,$params);
    }

    function getList(){
        $html = "";
        $field = $this->params['field'];
        $table = $this->params['table'];

        $result = $this->CI->db->query("SELECT $field FROM $table");
        foreach($result->result_array() as $row){
            if($row[$field] == $this->value)
                $html .= "<option value=\"".$row[$field]."\" selected>".$row[$field]."</option>\n";
            else
                $html .= "<option value=\"".$row[$field]."\">".$row[$field]."</option>\n";
        }

        return $html;
    }
}

And the updated advanced_form.yml definition:

backend/definitions/advanced_form.yml
Code:
name:
    class:TextBox
name_full:
    class:TextBox
    label: Full name
    attributes:
        onclick: "this.style.backgroundColor='black'"
date_added:
    class: Time
    label: Date of Addition
    attributes:
        width: 30
password:
    class: PasswordBox
short_description:
    class: TextArea
    attributes:
        cols: 45
        rows: 10
full_description:
    class: TextArea
    attributes:
        cols: 50
        rows: 20
    params:
        rich_text: true
        rich_text_theme: advanced
email:
    class:TextBox
pending:
    class: CheckBox
    label: Pending or not?
    value: 1
sold:
    class: CheckBox
class:
    class: DbDropDown
    label: Class
    params:
        table: simple_db_table
        field: name
category:
    class: DropDown
    params:
        list:
            1: One
            2: Two
            3: Three
        multiple: true
    value: 2
image:
    class:File
    label:Avatar
#2

[eluser]CI Lee[/eluser]
Hey Zaatar... again looks great.

As for the videos.. if you wanna host them on my server (Media Temple) I am more than willing to set up a sub domain for you.

-Lee




Theme © iAndrew 2016 - Forum software by © MyBB