How to use same form code for add,update

Martin

test.kv

:kivy 1.10.0

<CRUD>:
    title: self.mode + " State"
    size_hint: None, None
    size: 350, 350
    auto_dismiss: False

    BoxLayout:
        orientation: "vertical"
        GridLayout:
            cols: 2
            Label:
                text: root.label_rec_id
            Label:
                id: userid
                text: root.col_data[0]  # root.userid
            Label:
                text: "First Name"
            TextInput:
                id: fname
                text: root.col_data[1]  # root.fname
            Label:
                text: "Last Name"
            TextInput:
                id: lname
                text: root.col_data[2]  # root.lname
        Button:
            size_hint: 1, 0.4
            text: "Save Changes"
            on_release:
                root.package_changes(fname.text, lname.text)
                app.root.update_changes(root)
                root.dismiss()
        Button:
            size_hint: 1, 0.4
            text: "Cancel Changes"
            on_release: root.dismiss()
  1. when click on any row then edit form open.Can i use same form for add user.

At this time i click on add user then user.py file run and open a new form.how to use same form for both add update.

ikolim

Yes, you can use the same form to add city. In the example, I added a button, a method add_record, some variable e.g. mode of type StringProperty, and INSERT SQL command. Please refer to the example for details.

Example

main.py

import kivy

kivy.require('1.10.0')  # replace with your current kivy version !
import sqlite3 as lite
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty, ListProperty, StringProperty, ObjectProperty, NumericProperty
from kivy.lang import Builder

from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.button import Button
from kivy.uix.recyclegridlayout import RecycleGridLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.popup import Popup
from kivy.core.window import Window


Window.size = (500, 500)

MAX_TABLE_COLS = 3

con = lite.connect('company.db')
# con.text_factory = str
cur = con.cursor()


class CRUD(Popup):
    """CRUD - Create, Read, Update, Delete"""
    label_id_text = ObjectProperty(None)
    label_id_data = ObjectProperty(None)

    mode = StringProperty("")
    label_rec_id = StringProperty("UserID")
    start_point = NumericProperty(0)
    col_data = ListProperty(["", "", ""])

    def __init__(self, obj, **kwargs):
        super(CRUD, self).__init__(**kwargs)
        self.mode = obj.mode
        if obj.mode == "Add":
            self.label_id_text.opacity = 0  # invisible
            self.label_id_data.opacity = 0  # invisible
        else:
            self.label_id_text.opacity = 1  # visible
            self.label_id_data.opacity = 1  # visible
            self.start_point = obj.start_point
            self.col_data[0] = obj.rv_data[obj.start_point]["text"]
            self.col_data[1] = obj.rv_data[obj.start_point + 1]["text"]
            self.col_data[2] = obj.rv_data[obj.start_point + 2]["text"]

    def package_changes(self, fname, lname):
        self.col_data[1] = fname
        self.col_data[2] = lname


class SelectableRecycleGridLayout(FocusBehavior, LayoutSelectionBehavior,
                                  RecycleGridLayout):
    ''' Adds selection and focus behaviour to the view. '''


class SelectableButton(RecycleDataViewBehavior, Button):
    ''' Add selection support to the Button '''
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)
    rv_data = ObjectProperty(None)
    start_point = NumericProperty(0)
    mode = StringProperty("")

    def refresh_view_attrs(self, rv, index, data):
        ''' Catch and handle the view changes '''
        self.index = index
        return super(SelectableButton, self).refresh_view_attrs(rv, index, data)

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableButton, self).on_touch_down(touch):
            return True
        if self.collide_point(*touch.pos) and self.selectable:
            return self.parent.select_with_touch(self.index, touch)

    def apply_selection(self, rv, index, is_selected):
        ''' Respond to the selection of items in the view. '''
        self.selected = is_selected
        self.rv_data = rv.data

    def on_press(self):
        self.mode = "Update"
        self.start_point = 0
        end_point = MAX_TABLE_COLS
        rows = len(self.rv_data) // MAX_TABLE_COLS
        for row in range(rows):
            if self.index in list(range(end_point)):
                break
            self.start_point += MAX_TABLE_COLS
            end_point += MAX_TABLE_COLS

        popup = CRUD(self)
        popup.open()


class RV(BoxLayout):
    rv_data = ListProperty([])
    start_point = NumericProperty(0)
    mode = StringProperty("")

    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.get_users()

    def get_users(self):
        '''This result retrieve from database'''
        self.rv_data = []

        cur.execute("SELECT * FROM Users ORDER BY UserID ASC")
        rows = cur.fetchall()

        # create data_items
        for row in rows:
            for col in row:
                self.rv_data.append(col)

    def add_record(self):
        self.mode = "Add"
        popup = CRUD(self)
        popup.open()

    def update_changes(self, obj):
        if obj.mode == "Add":
            # insert record into Database Table
            cur.execute("INSERT INTO Users VALUES(NULL, ?, ?)",
                        (obj.col_data[1], obj.col_data[2]))
        else:
            # update Database Table
            cur.execute("UPDATE Users SET FirstName=?, LastName=? WHERE UserID=?",
                        (obj.col_data[1], obj.col_data[2], obj.col_data[0]))
        con.commit()
        self.get_users()


class ListUser(App):
    title = "Users"

    def build(self):
        self.root = Builder.load_file('main.kv')
        return RV()


if __name__ == '__main__':
    ListUser().run()

main.kv

#:kivy 1.10.0

<CRUD>:
    label_id_text: label_id_text
    label_id_data: label_id_data

    title: self.mode + " State"
    size_hint: None, None
    size: 350, 350
    auto_dismiss: False

    BoxLayout:
        orientation: "vertical"
        GridLayout:
            cols: 2
            Label:
                id: label_id_text
                text: "ID"
            Label:
                id: label_id_data
                text: root.col_data[0]  # root.userid
            Label:
                text: "First Name"
            TextInput:
                id: fname
                text: root.col_data[1]  # root.fname
            Label:
                text: "Last Name"
            TextInput:
                id: lname
                text: root.col_data[2]  # root.lname
        Button:
            size_hint: 1, 0.4
            text: "Confirm " + root.mode
            on_release:
                root.package_changes(fname.text, lname.text)
                app.root.update_changes(root)
                root.dismiss()
        Button:
            size_hint: 1, 0.4
            text: "Cancel " + root.mode
            on_release: root.dismiss()

<SelectableButton>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1)
        Rectangle:
            pos: self.pos
            size: self.size

<RV>:
    BoxLayout:
        orientation: "vertical"

        Button:
            size_hint: 1, 0.1
            text: "Add Record"
            on_press: root.add_record()

        GridLayout:
            size_hint: 1, None
            size_hint_y: None
            height: 25
            cols: 3

            Label:
                text: "ID"
            Label:
                text: "First Name"
            Label:
                text: "Last Name"

        BoxLayout:
            RecycleView:
                viewclass: 'SelectableButton'
                data: [{'text': str(x)} for x in root.rv_data]
                SelectableRecycleGridLayout:
                    cols: 3
                    default_size: None, dp(26)
                    default_size_hint: 1, None
                    size_hint_y: None
                    height: self.minimum_height
                    orientation: 'vertical'
                    multiselect: True
                    touch_multiselect: True

Output

Figure 1 - App Startup Figure 2 - SQLite3 Database Figure 3 - Add Record Joe Plummer Figure 4 - RecycleView Data Updated with Joe Plummer Figure 5 - SQLite3 Database Updated with Joe Plummer Figure 6 - Update John Doe to Jane Doe Figure 7 - RecycleView Data Updated with Jane Doe Figure 8 - SQLite3 Database Updated with Jane Doe

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use update and select form the same table ? mysql

From Dev

How to use the same form DTO, with different validation annotations? How to avoid double code?

From Dev

How to use the same line of code in all functions?

From Dev

How to use same modal code in all the pages?

From Dev

How to use same modal code in all the pages?

From Dev

How can i use jQuery to add up sectioned form fields and update when different values are added?

From Dev

how to insert the column and update them in the same code

From Dev

How add image into form with fields code Codeigniter

From Dev

How to use SQL procedure to add new attribute and update the value at the same time?

From Dev

How to use the same form for creating and editing in ruby on rails

From Dev

How to use Xamarin.Form Controls and Native Controls On the same page

From Dev

How to use value from input tag in the same form

From Dev

How to use data in same table on UPDATE query?

From Dev

Copy A form to use in the same project

From Dev

How to Use Rails Code for A Mailing List Signup Form

From Dev

How to use same code for two different jQuery events without repetition?

From Java

How to use the same C++ code for Android and iOS?

From Dev

How to use same Code base for multiple AngularJS projects

From Dev

ggplot, use both vjust and hjust for the same graph, how to code this?

From Dev

How can use 2 code same time like this

From Dev

How to use OOP to avoid repeating same code over and over?

From Dev

How to use targets to launch multiple apps using the same code base?

From Dev

How to use same lines of code in all components in react?

From Dev

how to use variables in the same file for a other piece of code

From Dev

How to make array for add multiple row in a sql table by same form

From Dev

How to add input field of a form with same class name

From Dev

How to add a class to multiple tags in a form at the same time

From Dev

symfony2 form: how to save entity and how to add more then one entity to the same form?

From Dev

How to add a form to a form?

Related Related

  1. 1

    How to use update and select form the same table ? mysql

  2. 2

    How to use the same form DTO, with different validation annotations? How to avoid double code?

  3. 3

    How to use the same line of code in all functions?

  4. 4

    How to use same modal code in all the pages?

  5. 5

    How to use same modal code in all the pages?

  6. 6

    How can i use jQuery to add up sectioned form fields and update when different values are added?

  7. 7

    how to insert the column and update them in the same code

  8. 8

    How add image into form with fields code Codeigniter

  9. 9

    How to use SQL procedure to add new attribute and update the value at the same time?

  10. 10

    How to use the same form for creating and editing in ruby on rails

  11. 11

    How to use Xamarin.Form Controls and Native Controls On the same page

  12. 12

    How to use value from input tag in the same form

  13. 13

    How to use data in same table on UPDATE query?

  14. 14

    Copy A form to use in the same project

  15. 15

    How to Use Rails Code for A Mailing List Signup Form

  16. 16

    How to use same code for two different jQuery events without repetition?

  17. 17

    How to use the same C++ code for Android and iOS?

  18. 18

    How to use same Code base for multiple AngularJS projects

  19. 19

    ggplot, use both vjust and hjust for the same graph, how to code this?

  20. 20

    How can use 2 code same time like this

  21. 21

    How to use OOP to avoid repeating same code over and over?

  22. 22

    How to use targets to launch multiple apps using the same code base?

  23. 23

    How to use same lines of code in all components in react?

  24. 24

    how to use variables in the same file for a other piece of code

  25. 25

    How to make array for add multiple row in a sql table by same form

  26. 26

    How to add input field of a form with same class name

  27. 27

    How to add a class to multiple tags in a form at the same time

  28. 28

    symfony2 form: how to save entity and how to add more then one entity to the same form?

  29. 29

    How to add a form to a form?

HotTag

Archive