Blame platform-demos/de/treeview_simple_liststore.js.page

Packit 1470ea
Packit 1470ea
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="treeview_simple_liststore.js" xml:lang="de">
Packit 1470ea
  <info>
Packit 1470ea
  <title type="text">TreeView with ListStore (JavaScript)</title>
Packit 1470ea
    <link type="guide" xref="beginner.js#treeview"/>
Packit 1470ea
    <link type="seealso" xref="GtkApplicationWindow.js"/>
Packit 1470ea
    <link type="seealso" xref="grid.js"/>
Packit 1470ea
    <link type="seealso" xref="label.js"/>
Packit 1470ea
    <revision version="0.1" date="2012-07-04" status="draft"/>
Packit 1470ea
Packit 1470ea
    <credit type="author copyright">
Packit 1470ea
      <name>Taryn Fox</name>
Packit 1470ea
      <email its:translate="no">jewelfox@fursona.net</email>
Packit 1470ea
      <years>2012</years>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <desc>A widget that shows a separate list of items</desc>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Mario Blättermann</mal:name>
Packit 1470ea
      <mal:email>mario.blaettermann@gmail.com</mal:email>
Packit 1470ea
      <mal:years>2011, 2013</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>TreeView with ListStore</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/treeview_simple_liststore_penguins.png"/>
Packit 1470ea
  

A TreeView is like a window onto the contents of either a ListStore or a TreeStore. A ListStore is like a spreadsheet: a "flat", two-dimensional list of things broken up into rows and columns. A TreeStore, meanwhile, can branch out in different directions like a tree can. In this example, we create a TreeView that shows the contents of a ListStore with (fictitious) names and phone numbers in it, and set it so that the <link xref="label.js">Label</link> at the bottom of the window shows more information about whichever name you click on.

Packit 1470ea
  

The TreeView is not just a single widget, but contains a number of smaller ones:

Packit 1470ea
  <list>
Packit 1470ea
    <item>

TreeViewColumn widgets show each (vertical) column of information from the ListStore. Each one has a title which can be shown at the top of the column, like in the screenshot.

</item>
Packit 1470ea
    <item>

CellRenderer widgets are "packed" into each TreeViewColumn, and contain the instructions for how to display each individual "cell", or item from the ListStore. There are multiple different types, including the CellRendererText used here and the CellRendererPixbuf, which displays a picture ("pixel buffer").

</item>
Packit 1470ea
  </list>
Packit 1470ea
  

Finally, we're going to use an object called a TreeIter, which isn't a widget so much as an invisible cursor which points to a (horizontal) row in the ListStore. Whenever you click on a name in the phonebook, for instance, we create a TreeIter pointing to the row that's selected, and then use that to tell the ListStore which entry we want the Label to show more information about.

Packit 1470ea
  <note>

The TreeView is probably the most complicated Gtk widget, because of how many parts it has and how they all have to work together. Give yourself time to learn how it works and experiment with it, or try something easier first if you're having trouble.

</note>
Packit 1470ea
    <links type="section"/>
Packit 1470ea
Packit 1470ea
  <section id="imports">
Packit 1470ea
    <title>Zu importierende Bibliotheken</title>
Packit 1470ea
    
Packit 1470ea
#!/usr/bin/gjs
Packit 1470ea
Packit 1470ea
const GObject = imports.gi.GObject;
Packit 1470ea
const Gtk = imports.gi.Gtk;
Packit 1470ea
const Lang = imports.lang;
Packit 1470ea
const Pango = imports.gi.Pango;
Packit 1470ea
Packit 1470ea
    

These are the libraries we need to import for this application to run. Remember that the line which tells GNOME that we're using Gjs always needs to go at the start.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="applicationwindow">
Packit 1470ea
    <title>Entwurf des Anwendungsfensters</title>
Packit 1470ea
    
Packit 1470ea
const TreeViewExample = new Lang.Class({
Packit 1470ea
    Name: 'TreeView Example with Simple ListStore',
Packit 1470ea
Packit 1470ea
    // Create the application itself
Packit 1470ea
    _init: function() {
Packit 1470ea
        this.application = new Gtk.Application({
Packit 1470ea
            application_id: 'org.example.jstreeviewsimpleliststore'
Packit 1470ea
        });
Packit 1470ea
Packit 1470ea
    // Connect 'activate' and 'startup' signals to the callback functions
Packit 1470ea
    this.application.connect('activate', Lang.bind(this, this._onActivate));
Packit 1470ea
    this.application.connect('startup', Lang.bind(this, this._onStartup));
Packit 1470ea
    },
Packit 1470ea
Packit 1470ea
    // Callback function for 'activate' signal presents window when active
Packit 1470ea
    _onActivate: function() {
Packit 1470ea
        this._window.present();
Packit 1470ea
    },
Packit 1470ea
Packit 1470ea
    // Callback function for 'startup' signal builds the UI
Packit 1470ea
    _onStartup: function() {
Packit 1470ea
        this._buildUI ();
Packit 1470ea
    },
Packit 1470ea
Packit 1470ea
    

All the code for this sample goes in the TreeViewExample class. The above code creates a <link href="http://www.roojs.com/seed/gir-1.2-gtk-3.0/gjs/Gtk.Application.html">Gtk.Application</link> for our widgets and window to go in.

Packit 1470ea
    
Packit 1470ea
    // Build the application's UI
Packit 1470ea
    _buildUI: function() {
Packit 1470ea
Packit 1470ea
        // Create the application window
Packit 1470ea
        this._window = new Gtk.ApplicationWindow({
Packit 1470ea
            application: this.application,
Packit 1470ea
            window_position: Gtk.WindowPosition.CENTER,
Packit 1470ea
            default_height: 250,
Packit 1470ea
            default_width: 100,
Packit 1470ea
            border_width: 20,
Packit 1470ea
            title: "My Phone Book"});
Packit 1470ea
Packit 1470ea
    

The _buildUI function is where we put all the code to create the application's user interface. The first step is creating a new <link xref="GtkApplicationWindow.js">Gtk.ApplicationWindow</link> to put all our widgets into.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="liststore">
Packit 1470ea
    <title>Creating the ListStore</title>
Packit 1470ea
    
Packit 1470ea
        // Create the underlying liststore for the phonebook
Packit 1470ea
        this._listStore = new Gtk.ListStore ();
Packit 1470ea
        this._listStore.set_column_types ([
Packit 1470ea
            GObject.TYPE_STRING,
Packit 1470ea
            GObject.TYPE_STRING,
Packit 1470ea
            GObject.TYPE_STRING,
Packit 1470ea
            GObject.TYPE_STRING]);
Packit 1470ea
Packit 1470ea
    

We first create the ListStore like we would any widget. Then we call its set_column_types method, and pass it an array of GObject data types. (We could have put the types all on one line, but here we are breaking them up to make it easier to read.)

Packit 1470ea
    

The GObject data types you can use include:

Packit 1470ea
    <list>
Packit 1470ea
      <item>

<file>GObject.TYPE_BOOLEAN</file> -- True or false

</item>
Packit 1470ea
      <item>

<file>GObject.TYPE_FLOAT</file> -- A floating point number (one with a decimal point)

</item>
Packit 1470ea
      <item>

<file>GObject.TYPE_STRING</file> -- A string of letters and numbers

</item>
Packit 1470ea
      <item>

<file>gtk.gdk.Pixbuf</file> -- Ein Bild

</item>
Packit 1470ea
    </list>
Packit 1470ea
    

In this case, we're making a ListStore of four columns, each one containing string values.

Packit 1470ea
    <note>

You need to put the line <file>const GObject = imports.gi.GObject;</file> at the start of your application's code, like we did in this example, if you want to be able to use GObject types.

</note>
Packit 1470ea
Packit 1470ea
    
Packit 1470ea
        // Data to go in the phonebook
Packit 1470ea
        this.phonebook =
Packit 1470ea
        let phonebook =
Packit 1470ea
            [{ name: "Jurg", surname: "Billeter", phone: "555-0123",
Packit 1470ea
                description: "A friendly person."},
Packit 1470ea
             { name: "Johannes", surname: "Schmid", phone: "555-1234",
Packit 1470ea
                description: "Easy phone number to remember."},
Packit 1470ea
             { name: "Julita", surname: "Inca", phone: "555-2345",
Packit 1470ea
                description: "Another friendly person."},
Packit 1470ea
             { name: "Javier", surname: "Jardon", phone: "555-3456",
Packit 1470ea
                description: "Bring fish for his penguins."},
Packit 1470ea
             { name: "Jason", surname: "Clinton", phone: "555-4567",
Packit 1470ea
                description: "His cake's not a lie."},
Packit 1470ea
             { name: "Random J.", surname: "Hacker", phone: "555-5678",
Packit 1470ea
                description: "Very random!"}];
Packit 1470ea
Packit 1470ea
    

Here we have the information to go in the ListStore. It's an array of objects, each one corresponding to a single entry in our phone book.

Packit 1470ea
    

Note that the TreeView in the screenshot doesn't actually show the data from the "description" properties. Instead, that information's shown in the Label beneath it, for whichever row that you click on. That's because the TreeView and ListStore are two separate things, and a TreeView can show all or part of a ListStore, and display what's in it in different ways. You can even have multiple widgets show things from the same ListStore, like the Label in our example or even a second TreeView.

Packit 1470ea
Packit 1470ea
    
Packit 1470ea
        for (i = 0; i < phonebook.length; i++ ) {
Packit 1470ea
            let contact = phonebook [i];
Packit 1470ea
            this._listStore.set (this._listStore.append(), [0, 1, 2, 3],
Packit 1470ea
                [contact.name, contact.surname, contact.phone, contact.description]);
Packit 1470ea
        }
Packit 1470ea
Packit 1470ea
    

This <file>for</file> loop puts the strings from our phonebook into our ListStore in order. In order, we pass the ListStore's set method the iter that points to the correct row, an array which says which columns we want to set, and an array which contains the data we want to put into those columns.

Packit 1470ea
    

A ListStore's <file>append</file> method adds a horizontal row onto it (it starts out with none), and returns a TreeIter pointing to that row like a cursor. So by passing <file>this._listStore.append()</file> to the ListStore as a property, we're creating a new row and telling the <file>set</file> method which row to set data for at the same time.

Packit 1470ea
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="treeview">
Packit 1470ea
    <title>Creating the TreeView</title>
Packit 1470ea
    
Packit 1470ea
        // Create the treeview
Packit 1470ea
        this._treeView = new Gtk.TreeView ({
Packit 1470ea
            expand: true,
Packit 1470ea
            model: this._listStore });
Packit 1470ea
Packit 1470ea
    

Here we create a basic TreeView widget, that expands both horizontally and vertically to use as much space as needed. We set it to use the ListStore we created as its "model", or the thing it'll show us stuff from.

Packit 1470ea
Packit 1470ea
    
Packit 1470ea
        // Spalten für das Adressbuch erstellen
Packit 1470ea
        let firstName = new Gtk.TreeViewColumn ({ title: "First Name" });
Packit 1470ea
        let lastName = new Gtk.TreeViewColumn ({ title: "Last Name" });
Packit 1470ea
        let phone = new Gtk.TreeViewColumn ({ title: "Phone Number" });
Packit 1470ea
Packit 1470ea
    

Now we create each of the vertical TreeViewColumns we'll see in the TreeView. The title for each one goes at the top, as you can see in the screenshot.

Packit 1470ea
Packit 1470ea
    
Packit 1470ea
        // Create a cell renderer for when bold text is needed
Packit 1470ea
        let bold = new Gtk.CellRendererText ({
Packit 1470ea
            weight: Pango.Weight.BOLD });
Packit 1470ea
Packit 1470ea
        // Create a cell renderer for normal text
Packit 1470ea
        let normal = new Gtk.CellRendererText ();
Packit 1470ea
Packit 1470ea
        // Pack the cell renderers into the columns
Packit 1470ea
        firstName.pack_start (bold, true);
Packit 1470ea
        lastName.pack_start (normal, true);
Packit 1470ea
        phone.pack_start (normal, true);
Packit 1470ea
Packit 1470ea
    

Here we create the CellRenderers that we'll use to display the text from our ListStore, and pack them into the TreeViewColumns. Each CellRendererText is used for all the entries in that column. Our normal CellRendererText just creates plain text, while our bold one uses heavier-weight text. We put it into the first name column, and tell the other two to use copies of the normal one. The "true" used as the second parameter for the <file>pack_start</file> method tells it to expand the cells when possible, instead of keeping them compact.

Packit 1470ea
    <note>

<link href="http://www.pygtk.org/docs/pygtk/pango-constants.html">Here is a list</link> of other text properties you can use. In order to use these Pango constants, make sure to put the line <file>const Pango = imports.gi.Pango;</file> at the beginning of your code like we did.

</note>
Packit 1470ea
Packit 1470ea
    
Packit 1470ea
        firstName.add_attribute (bold, "text", 0);
Packit 1470ea
        lastName.add_attribute (normal, "text", 1);
Packit 1470ea
        phone.add_attribute (normal, "text", 2);
Packit 1470ea
Packit 1470ea
        // Insert the columns into the treeview
Packit 1470ea
        this._treeView.insert_column (firstName, 0);
Packit 1470ea
        this._treeView.insert_column (lastName, 1);
Packit 1470ea
        this._treeView.insert_column (phone, 2);
Packit 1470ea
Packit 1470ea
    

Now that we've put the CellRenderers into the TreeViewColumns, we use the <file>add_attribute</file> method to tell each column to pull in text from the model our TreeView is set to use; in this case, the ListStore with the phonebook.

Packit 1470ea
    <list>
Packit 1470ea
      <item>

The first parameter is which CellRenderer we're going to use to render what we're pulling in.

</item>
Packit 1470ea
      <item>

The second parameter is what kind of information we're going to pull in. In this case, we're letting it know that we're rendering text.

</item>
Packit 1470ea
      <item>

The third parameter is which of the ListStore's columns we're pulling that information in from.

</item>
Packit 1470ea
    </list>
Packit 1470ea
    

After we've set that up, we use the TreeView's <file>insert_column</file> method to put our TreeViewColumns inside it in order. Our TreeView is now complete.

Packit 1470ea
    <note>

Normally, you might want to use a loop to initialize your TreeView, but in this example we're spelling things out step by step for the sake of making it easier to understand.

</note>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="ui">
Packit 1470ea
    <title>Building the rest of the UI</title>
Packit 1470ea
    
Packit 1470ea
        // Create the label that shows details for the name you select
Packit 1470ea
        this._label = new Gtk.Label ({ label: "" });
Packit 1470ea
Packit 1470ea
        // Get which item is selected
Packit 1470ea
        this.selection = this._treeView.get_selection();
Packit 1470ea
Packit 1470ea
        // When something new is selected, call _on_changed
Packit 1470ea
        this.selection.connect ('changed', Lang.bind (this, this._onSelectionChanged));
Packit 1470ea
Packit 1470ea
    

The TreeView's <file>get_selection</file> method returns an object called a TreeSelection. A TreeSelection is like a TreeIter in that it's basically a cursor that points at a particular row, except that the one it points to is the one that's visibly highlighted as selected.

Packit 1470ea
    

After we get the TreeSelection that goes with our TreeView, we ask it to tell us when it changes which row it's pointing to. We do this by connecting its <file>changed</file> signal to the _onSelectionChanged function we wrote. This function changes the text displayed by the Label we just made.

Packit 1470ea
Packit 1470ea
    
Packit 1470ea
        // Create a grid to organize everything in
Packit 1470ea
        this._grid = new Gtk.Grid;
Packit 1470ea
Packit 1470ea
        // Attach the treeview and label to the grid
Packit 1470ea
        this._grid.attach (this._treeView, 0, 0, 1, 1);
Packit 1470ea
        this._grid.attach (this._label, 0, 1, 1, 1);
Packit 1470ea
Packit 1470ea
        // Add the grid to the window
Packit 1470ea
        this._window.add (this._grid);
Packit 1470ea
Packit 1470ea
        // Show the window and all child widgets
Packit 1470ea
        this._window.show_all();
Packit 1470ea
    },
Packit 1470ea
Packit 1470ea
    

After we've gotten that out of the way, we create a <link xref="grid.js">Grid</link> to put everything in, then add it to our window and tell the window to show itself and its contents.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="function">
Packit 1470ea
    <title>Function which handles a changed selection</title>
Packit 1470ea
Packit 1470ea
    
Packit 1470ea
    _onSelectionChanged: function () {
Packit 1470ea
Packit 1470ea
        // Grab a treeiter pointing to the current selection
Packit 1470ea
        let [ isSelected, model, iter ] = this.selection.get_selected();
Packit 1470ea
Packit 1470ea
        // Set the label to read off the values stored in the current selection
Packit 1470ea
        this._label.set_label ("\n" +
Packit 1470ea
            this._listStore.get_value (iter, 0) + " " +
Packit 1470ea
            this._listStore.get_value (iter, 1) + " " +
Packit 1470ea
            this._listStore.get_value (iter, 2) + "\n" +
Packit 1470ea
            this._listStore.get_value (iter, 3));
Packit 1470ea
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
});
Packit 1470ea
Packit 1470ea
    

The line of code with the let statement is a little convoluted, but it's nonetheless the best way to get a TreeIter pointing to the same row as our TreeSelection. It has to create a couple of other object references, but <file>iter</file> is the only one we need.

Packit 1470ea
    

After we've done that, we call the Label's <file>set_label</file> function, and use the ListStore's <file>get_value</file> function a handful of times to fill in the data we want to put in it. Its parameters are a TreeIter pointing to the row we want to get data from, and the column.

Packit 1470ea
    

Here, we want to get data from all four columns, including the "hidden" one that's not part of the TreeView. This way, we can use our Label to show strings that are too large to fit in the TreeView, and that we don't need to see at a glance.

Packit 1470ea
Packit 1470ea
    
Packit 1470ea
// Run the application
Packit 1470ea
let app = new TreeViewExample ();
Packit 1470ea
app.application.run (ARGV);
Packit 1470ea
Packit 1470ea
    

Finally, we create a new instance of the finished TreeViewExample class, and set the application running.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="complete">
Packit 1470ea
    <title>Complete code sample</title>
Packit 1470ea
#!/usr/bin/gjs
Packit 1470ea
Packit 1470ea
imports.gi.versions.Gtk = '3.0';
Packit 1470ea
Packit 1470ea
const GObject = imports.gi.GObject;
Packit 1470ea
const Gtk = imports.gi.Gtk;
Packit 1470ea
const Pango = imports.gi.Pango;
Packit 1470ea
Packit 1470ea
class TreeViewExample {
Packit 1470ea
    // Create the application itself
Packit 1470ea
    constructor() {
Packit 1470ea
        this.application = new Gtk.Application({
Packit 1470ea
            application_id: 'org.example.jstreeviewsimpleliststore'
Packit 1470ea
        });
Packit 1470ea
Packit 1470ea
        // Connect 'activate' and 'startup' signals to the callback functions
Packit 1470ea
        this.application.connect('activate', this._onActivate.bind(this));
Packit 1470ea
        this.application.connect('startup', this._onStartup.bind(this));
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    // Callback function for 'activate' signal presents window when active
Packit 1470ea
    _onActivate() {
Packit 1470ea
        this._window.present();
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    // Callback function for 'startup' signal builds the UI
Packit 1470ea
    _onStartup() {
Packit 1470ea
        this._buildUI();
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    // Build the application's UI
Packit 1470ea
    _buildUI() {
Packit 1470ea
        // Create the application window
Packit 1470ea
        this._window = new Gtk.ApplicationWindow({
Packit 1470ea
            application: this.application,
Packit 1470ea
            window_position: Gtk.WindowPosition.CENTER,
Packit 1470ea
            default_height: 250,
Packit 1470ea
            default_width: 100,
Packit 1470ea
            border_width: 20,
Packit 1470ea
            title: "My Phone Book"});
Packit 1470ea
Packit 1470ea
        // Create the underlying liststore for the phonebook
Packit 1470ea
        this._listStore = new Gtk.ListStore ();
Packit 1470ea
        this._listStore.set_column_types ([
Packit 1470ea
            GObject.TYPE_STRING,
Packit 1470ea
            GObject.TYPE_STRING,
Packit 1470ea
            GObject.TYPE_STRING,
Packit 1470ea
            GObject.TYPE_STRING]);
Packit 1470ea
Packit 1470ea
        // Data to go in the phonebook
Packit 1470ea
        let phonebook =
Packit 1470ea
            [{ name: "Jurg", surname: "Billeter", phone: "555-0123",
Packit 1470ea
                description: "A friendly person."},
Packit 1470ea
             { name: "Johannes", surname: "Schmid", phone: "555-1234",
Packit 1470ea
                description: "Easy phone number to remember."},
Packit 1470ea
             { name: "Julita", surname: "Inca", phone: "555-2345",
Packit 1470ea
                description: "Another friendly person."},
Packit 1470ea
             { name: "Javier", surname: "Jardon", phone: "555-3456",
Packit 1470ea
                description: "Bring fish for his penguins."},
Packit 1470ea
             { name: "Jason", surname: "Clinton", phone: "555-4567",
Packit 1470ea
                description: "His cake's not a lie."},
Packit 1470ea
             { name: "Random J.", surname: "Hacker", phone: "555-5678",
Packit 1470ea
                description: "Very random!"}];
Packit 1470ea
Packit 1470ea
        // Put the data in the phonebook
Packit 1470ea
        for (let i = 0; i < phonebook.length; i++ ) {
Packit 1470ea
            let contact = phonebook [i];
Packit 1470ea
            this._listStore.set (this._listStore.append(), [0, 1, 2, 3],
Packit 1470ea
                [contact.name, contact.surname, contact.phone, contact.description]);
Packit 1470ea
        }
Packit 1470ea
Packit 1470ea
        // Create the treeview
Packit 1470ea
        this._treeView = new Gtk.TreeView ({
Packit 1470ea
            expand: true,
Packit 1470ea
            model: this._listStore });
Packit 1470ea
Packit 1470ea
        // Create the columns for the address book
Packit 1470ea
        let firstName = new Gtk.TreeViewColumn ({ title: "First Name" });
Packit 1470ea
        let lastName = new Gtk.TreeViewColumn ({ title: "Last Name" });
Packit 1470ea
        let phone = new Gtk.TreeViewColumn ({ title: "Phone Number" });
Packit 1470ea
Packit 1470ea
        // Create a cell renderer for when bold text is needed
Packit 1470ea
        let bold = new Gtk.CellRendererText ({
Packit 1470ea
            weight: Pango.Weight.BOLD });
Packit 1470ea
Packit 1470ea
        // Create a cell renderer for normal text
Packit 1470ea
        let normal = new Gtk.CellRendererText ();
Packit 1470ea
Packit 1470ea
        // Pack the cell renderers into the columns
Packit 1470ea
        firstName.pack_start (bold, true);
Packit 1470ea
        lastName.pack_start (normal, true);
Packit 1470ea
        phone.pack_start (normal, true);
Packit 1470ea
Packit 1470ea
        // Set each column to pull text from the TreeView's model
Packit 1470ea
        firstName.add_attribute (bold, "text", 0);
Packit 1470ea
        lastName.add_attribute (normal, "text", 1);
Packit 1470ea
        phone.add_attribute (normal, "text", 2);
Packit 1470ea
Packit 1470ea
        // Insert the columns into the treeview
Packit 1470ea
        this._treeView.insert_column (firstName, 0);
Packit 1470ea
        this._treeView.insert_column (lastName, 1);
Packit 1470ea
        this._treeView.insert_column (phone, 2);
Packit 1470ea
Packit 1470ea
        // Create the label that shows details for the name you select
Packit 1470ea
        this._label = new Gtk.Label ({ label: "" });
Packit 1470ea
Packit 1470ea
        // Get which item is selected
Packit 1470ea
        this.selection = this._treeView.get_selection();
Packit 1470ea
Packit 1470ea
        // When something new is selected, call _on_changed
Packit 1470ea
        this.selection.connect ('changed', this._onSelectionChanged.bind(this));
Packit 1470ea
Packit 1470ea
        // Create a grid to organize everything in
Packit 1470ea
        this._grid = new Gtk.Grid;
Packit 1470ea
Packit 1470ea
        // Attach the treeview and label to the grid
Packit 1470ea
        this._grid.attach (this._treeView, 0, 0, 1, 1);
Packit 1470ea
        this._grid.attach (this._label, 0, 1, 1, 1);
Packit 1470ea
Packit 1470ea
        // Add the grid to the window
Packit 1470ea
        this._window.add (this._grid);
Packit 1470ea
Packit 1470ea
        // Show the window and all child widgets
Packit 1470ea
        this._window.show_all();
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    _onSelectionChanged() {
Packit 1470ea
        // Grab a treeiter pointing to the current selection
Packit 1470ea
        let [ isSelected, model, iter ] = this.selection.get_selected();
Packit 1470ea
Packit 1470ea
        // Set the label to read off the values stored in the current selection
Packit 1470ea
        this._label.set_label ("\n" +
Packit 1470ea
            this._listStore.get_value (iter, 0) + " " +
Packit 1470ea
            this._listStore.get_value (iter, 1) + " " +
Packit 1470ea
            this._listStore.get_value (iter, 2) + "\n" +
Packit 1470ea
            this._listStore.get_value (iter, 3)
Packit 1470ea
        );
Packit 1470ea
    }
Packit 1470ea
};
Packit 1470ea
Packit 1470ea
// Run the application
Packit 1470ea
let app = new TreeViewExample ();
Packit 1470ea
app.application.run (ARGV);
Packit 1470ea
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="in-depth">
Packit 1470ea
    <title>Weiterführende Dokumentation</title>
Packit 1470ea

Packit 1470ea
  In this sample we used the following:
Packit 1470ea

Packit 1470ea
<list>
Packit 1470ea
  <item>

<link href="http://www.roojs.com/seed/gir-1.2-gtk-3.0/gjs/Gtk.Application.html">Gtk.Application</link>

</item>
Packit 1470ea
  <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkApplicationWindow.html">Gtk.ApplicationWindow</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.CellRendererText.html">Gtk.CellRendererText</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.ListStore.html">Gtk.ListStore</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.TreeIter.html">Gtk.TreeIter</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.TreeSelection.html">Gtk.TreeSelection</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.TreeView.html">Gtk.TreeView</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.TreeViewColumn.html">Gtk.TreeViewColumn</link>

</item>
Packit 1470ea
</list>
Packit 1470ea
  </section>
Packit 1470ea
</page>