Access Keys:
Skip to content (Access Key - 0)
Home (Access Key - 1)
All spaces... (Access Key - 3)
Log in (Access Key - 5)
Sign up (Access Key - 6)
Toggle Sidebar

Search In Table


Summary

Allow users to search the content of a table and display all rows that match.

Language

Jython

Dependencies

enbform.py

The Script

{scriptix:SPACE=ITMOVE|PAGE=New Phone Numbers|QUERYTEXT=Find People|TEXTFIELDSIZE=19}

from enbform import *
import re

def customCreateForm(output, form, header = True, showDescription = True):
  output += "\{formbegin\}\n"

  if header:
    if showDescription:
      output += "|| Description || Input ||\n"
    else:
      output += "|| Input ||\n"
  temp = []
  for f in form:
    if f['type'] != "hidden":
      if showDescription:
        output += "| " + f['description'] + "|"
      output += generatePreProcess(f['key'])
      if len(form) == 1:
        output += "\{submitbutton\}\n "
      else:
        output += "\n"
    else:
      temp.append(f)
  for x in temp:
    output += generatePreProcess(x['key'])

  if len(form) != 1:
    output += "\{submitbutton\}\{formend\}"
  else:
    output += "\{formend\}"
  output
  return output

def customPostProcess(form, data, submitText = "Accept"):
  for item in form:
    key = item['key']
    searchString = "{" + key + "}"
    replacestring = ""
    if item['type'] == "input":
      length = item['length']
      default = item['default']
      replaceString = '<input type="input" name="%s" size="%d" value="%s" />'%(key, length, default)

    elif item['type'] == "select":
      replaceString = '<select name="%s">'%(key,)
      for o in item['options']:
        replaceString += '<option value="%s">%s</option>'%(o,o)
      replaceString += "</select>"

    elif item['type'] == "checkbox":
      if item['checked']:
        replaceString = '<input type="checkbox" name="%s" value="%s" checked />'%(key,1)
      else:
        replaceString = '<input type="checkbox" name="%s" value="%s" />'%(key,0)

    elif item['type'] == "textarea":
      rows = int(item['rows'])
      cols = int(item['cols'])
      text = item['text']
      replaceString = '<textarea name="%s" rows="%d" cols="%d">%s</textarea>'%(key, rows, cols, text)

    elif item['type'] == "hidden":
      value = item['value']
      replaceString = '<input type="hidden" name="%s" value="%s" />'%(key, value)

    elif item['type'] == "hiddenshow":
      value = item['value']
      replaceString = '<input type="hidden" name="%s" value="%s" />%s'%(key, value, value)

    reg = re.compile(searchString, re.MULTILINE|re.DOTALL)
    data = reg.sub(replaceString, data)

  # replace our parameters with input fields
  reg = re.compile("{submitbutton}", re.MULTILINE|re.DOTALL)
  data = reg.sub("<input type=\"submit\" value=\"%s\"/>"%(submitText,), data)

  reg = re.compile("{formbegin}", re.MULTILINE|re.DOTALL)
  data = reg.sub("<form name=\"input\" action=\"\" method=\"get\"/>", data)

  reg = re.compile("{formend}", re.MULTILINE|re.DOTALL)
  data = reg.sub("</form>", data)

  return data



underline = True
showDescription = False

def search(text, page):
    results = []
    regex = re.compile(text, re.IGNORECASE)
    currentHeader = ""
    for line in page.split("\n"):
        if line.strip().find("||") != -1:
           currentHeader = line
           continue
        if line.strip().find("|") == 0 and regex.search(line, 1):
            if currentHeader != "":
                if len(results):
                    results.append("\n")
                results.append(currentHeader)
                currentHeader = ""
            if underline:
                results.append(line.replace(text, "{+}"+text+"{+}").strip())
            else:
                results.append(line.strip())
    return results

form = []
output = ""

underlinemode = macro.params.get("UNDERLINE")
if underlinemode and underlinemode.lower() == "false":
    underline = False

pagename = macro.params.get("PAGE")
if not pagename:
   pagename = macro.renderContext.pageTitle


spacename = macro.params.get("SPACE")
if not spacename:
    spacename = macro.renderContext.spaceKey

queryText = macro.params.get("QUERYTEXT")
if not queryText:
    queryText = "Search"

showDesc = macro.params.get("SHOWDESCRIPTION")
if not showDesc:
    showDescription = False
else:
    showDescription = True

textFieldSize = macro.params.get("TEXTFIELDSIZE")
if not textFieldSize:
    textFieldSize = 19
else:
    try:
        textFieldSize = int(textFieldSize)
    except:
        textFieldSize = 19

page = scriptix.pageManager.getPage(spacename, pagename)
if page != None:
    form = []

    form.append(textInput(queryText, "textsearch", textFieldSize, ""))

    output = customCreateForm(output, form, False, showDescription)
    if  scriptix.getQueryStringValue("textsearch"):
        t = scriptix.getQueryStringValue("textsearch")
        results = search(t, str(page.content))
        if len(results):
            if underline:
                output += "{tip:title=Results: Exact matches will be underlined}\n"
            else:
                output += "{tip:title=Results}\n"
            for result in results:
                output += result + "\n"
            output += "{tip}\n"
        else:
            output += "{warning}No results for '%s'{warning}\n"%(t,)

    output = scriptix.subRenderer.render(output, macro.renderContext)

    output = customPostProcess(form, output, "Go")
else:
    output = "{warning}Could not load page. Set the SPACE and PAGE parameters and try again{warning}"
    output = scriptix.subRenderer.render(output, macro.renderContext)

{scriptix}

Toggle Sidebar
Added by Gum Shoes on May 25, 2008 04:23, last edited by Gum Shoes on May 27, 2008 20:04

Hmmm, was able to create this page but I can't edit it; need to add the enbform library.


Adaptavist Theme Builder Powered by Atlassian Confluence